Make it possible to put the site in maintenance mode
authorSverre Rabbelier <srabbelier@gmail.com>
Sun, 01 Mar 2009 20:39:21 +0000
changeset 1585 06fb5950cb03
parent 1584 d8ba8c917f37
child 1586 8a6caee3c9d6
Make it possible to put the site in maintenance mode By specifying the UTC date from which the maintenance shoulds start, developers can put the site in 'maintenance' mode. Also redirect to /maintenance if we get a CapabilityDisabledError. Patch by: Sverre Rabbelier
app/soc/models/site.py
app/soc/views/helper/decorators.py
app/soc/views/helper/responses.py
--- a/app/soc/models/site.py	Sun Mar 01 20:20:47 2009 +0000
+++ b/app/soc/models/site.py	Sun Mar 01 20:39:21 2009 +0000
@@ -46,6 +46,9 @@
   site_notice = db.StringProperty(verbose_name=ugettext('Site Notice'))
   site_notice.help_text = ugettext('A notice that will be displayed site-wide')
 
+  maintenance_start = db.DateTimeProperty(
+      verbose_name=ugettext('Maintenance start date'))
+
   #: Valid Google Analytics tracking number, if entered every page
   #: is going to have Google Analytics JS initialization code in 
   #: the footer with the given tracking number.
--- a/app/soc/views/helper/decorators.py	Sun Mar 01 20:20:47 2009 +0000
+++ b/app/soc/views/helper/decorators.py	Sun Mar 01 20:39:21 2009 +0000
@@ -27,7 +27,10 @@
 
 from functools import wraps
 
+from google.appengine.api import users
 from google.appengine.runtime import DeadlineExceededError
+from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
+
 
 from django import http
 
@@ -41,13 +44,29 @@
 def view(func):
   """Decorator that insists that exceptions are handled by view.
   """
+
+  from soc.logic.helper import timeline
+  from soc.logic.models.site import logic as site_logic
+
   @wraps(func)
-  def view_wrapper(*args, **kwds):
+  def view_wrapper(request, *args, **kwds):
+    site = site_logic.getSingleton()
+
+    # don't redirect admins, or if we're at /maintenance already
+    no_redirect = users.is_current_user_admin() or request.path == '/maintenance'
+
+    if (not no_redirect) and timeline.isAfterEvent(site, 'maintenance_start'):
+      return http.HttpResponseRedirect('/maintenance')
+
     try:
-      return func(*args, **kwds)
+      return func(request, *args, **kwds)
     except DeadlineExceededError, e:
       logging.exception(e)
       return http.HttpResponseRedirect('/soc/content/deadline_exceeded.html')
+    except CapabilityDisabledError, e:
+      logging.exception(e)
+      # assume the site is in maintenance if we get CDE
+      return http.HttpResponseRedirect('/maintenance')
     except MemoryError, e:
       logging.exception(e)
       return http.HttpResponseRedirect('/soc/content/memory_error.html')
--- a/app/soc/views/helper/responses.py	Sun Mar 01 20:20:47 2009 +0000
+++ b/app/soc/views/helper/responses.py	Sun Mar 01 20:39:21 2009 +0000
@@ -20,6 +20,7 @@
 __authors__ = [
   '"Todd Larsen" <tlarsen@google.com>',
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
   ]