Added maintance mode middleware.
authorŁukasz Rekucki <lrekucki@gmail.com>
Sat, 26 Sep 2009 09:55:16 +0000 (11:55 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Sat, 26 Sep 2009 09:55:16 +0000 (11:55 +0200)
14 files changed:
apps/api/handlers/library_handlers.py
apps/api/response.py
apps/maintenancemode/._middleware.py [new file with mode: 0644]
apps/maintenancemode/__init__.py [new file with mode: 0644]
apps/maintenancemode/conf/._settings.py [new file with mode: 0644]
apps/maintenancemode/conf/__init__.py [new file with mode: 0644]
apps/maintenancemode/conf/settings.py [new file with mode: 0644]
apps/maintenancemode/conf/urls/__init__.py [new file with mode: 0644]
apps/maintenancemode/conf/urls/defaults.py [new file with mode: 0644]
apps/maintenancemode/http.py [new file with mode: 0644]
apps/maintenancemode/middleware.py [new file with mode: 0644]
apps/maintenancemode/views/__init__.py [new file with mode: 0644]
apps/maintenancemode/views/defaults.py [new file with mode: 0644]
project/settings.py

index 45dff83..18dd3e8 100644 (file)
@@ -35,6 +35,7 @@ class BasicLibraryHandler(AnonymousBaseHandler):
             'name': docid } for docid in lib.documents() ]
 
         return {'documents' : document_list}
             'name': docid } for docid in lib.documents() ]
 
         return {'documents' : document_list}
+        
 
 class LibraryHandler(BaseHandler):
     allowed_methods = ('GET', 'POST')
 
 class LibraryHandler(BaseHandler):
     allowed_methods = ('GET', 'POST')
@@ -48,7 +49,7 @@ class LibraryHandler(BaseHandler):
             'url': reverse('document_view', args=[docid]),
             'name': docid } for docid in lib.documents() ]
 
             'url': reverse('document_view', args=[docid]),
             'name': docid } for docid in lib.documents() ]
 
-        return {'documents' : document_list }
+        return {'documents' : document_list }        
 
     @validate_form(forms.DocumentUploadForm, 'POST')
     def create(self, request, form):
 
     @validate_form(forms.DocumentUploadForm, 'POST')
     def create(self, request, form):
@@ -137,6 +138,10 @@ class DocumentHandler(BaseHandler):
 
         return result
 
 
         return result
 
+    def update(self, request, docid):
+        """Update information about the document, like display not"""
+        return
+
 #
 # Document Text View
 #
 #
 # Document Text View
 #
index 322118a..6b68562 100644 (file)
@@ -22,7 +22,7 @@ class ResponseObject(object):
         elif self._mime == MIME_JSON:
             data = json.dumps(body, default=lambda o: repr(o) )
         else:
         elif self._mime == MIME_JSON:
             data = json.dumps(body, default=lambda o: repr(o) )
         else:
-            data = u"%s\n%s" % (self.MESSAGE, unicode(self._info))
+            data = u"%s\n%s" % (self.MESSAGE, unicode(body))
             data = data.encode('utf-8')
             
         return HttpResponse(content=data, status=self._code, \
             data = data.encode('utf-8')
             
         return HttpResponse(content=data, status=self._code, \
@@ -60,7 +60,6 @@ class SuccessNoContent(ResponseObject):
     def django_response(self):
         return ResponseObject.django_response(self, body=None)
 
     def django_response(self):
         return ResponseObject.django_response(self, body=None)
 
-
 #
 # Client errors
 #
 #
 # Client errors
 #
@@ -96,6 +95,14 @@ class EntityConflict(ResponseObject):
         ResponseObject.__init__(self, 409, **kwargs)
 
 
         ResponseObject.__init__(self, 409, **kwargs)
 
 
+#
+# Server side errors
+#
+class NotImplemented(ResponseObject):
+
+    def __init__(self, **kwargs):
+        ResponseObject.__init__(self, 501, **kwargs)
+
 def validate_form(formclass, source='GET'):
     from functools import wraps
 
 def validate_form(formclass, source='GET'):
     from functools import wraps
 
diff --git a/apps/maintenancemode/._middleware.py b/apps/maintenancemode/._middleware.py
new file mode 100644 (file)
index 0000000..d25f019
Binary files /dev/null and b/apps/maintenancemode/._middleware.py differ
diff --git a/apps/maintenancemode/__init__.py b/apps/maintenancemode/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/apps/maintenancemode/conf/._settings.py b/apps/maintenancemode/conf/._settings.py
new file mode 100644 (file)
index 0000000..a898162
Binary files /dev/null and b/apps/maintenancemode/conf/._settings.py differ
diff --git a/apps/maintenancemode/conf/__init__.py b/apps/maintenancemode/conf/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/apps/maintenancemode/conf/settings.py b/apps/maintenancemode/conf/settings.py
new file mode 100644 (file)
index 0000000..4d0b1a5
--- /dev/null
@@ -0,0 +1,3 @@
+from django.conf import settings
+
+MAINTENANCE_MODE = getattr(settings, 'MAINTENANCE_MODE', False)
\ No newline at end of file
diff --git a/apps/maintenancemode/conf/urls/__init__.py b/apps/maintenancemode/conf/urls/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/apps/maintenancemode/conf/urls/defaults.py b/apps/maintenancemode/conf/urls/defaults.py
new file mode 100644 (file)
index 0000000..20029db
--- /dev/null
@@ -0,0 +1,3 @@
+__all__ = ['handler503']
+
+handler503 = 'maintenancemode.views.defaults.temporary_unavailable'
\ No newline at end of file
diff --git a/apps/maintenancemode/http.py b/apps/maintenancemode/http.py
new file mode 100644 (file)
index 0000000..4b3023b
--- /dev/null
@@ -0,0 +1,4 @@
+from django.http import HttpResponse
+
+class HttpResponseTemporaryUnavailable(HttpResponse):
+    status_code = 503
diff --git a/apps/maintenancemode/middleware.py b/apps/maintenancemode/middleware.py
new file mode 100644 (file)
index 0000000..404b1a0
--- /dev/null
@@ -0,0 +1,31 @@
+from django.conf import settings
+from django.core import urlresolvers
+
+# This is django-maintancemode v. 0.9.2
+
+from django.conf.urls import defaults
+defaults.handler503 = 'maintenancemode.views.defaults.temporary_unavailable'
+defaults.__all__.append('handler503')
+
+from maintenancemode.conf.settings import MAINTENANCE_MODE
+
+class MaintenanceModeMiddleware(object):
+    def process_request(self, request):
+        # Allow access if middleware is not activated
+        if not MAINTENANCE_MODE:
+            return None
+
+        # Allow access if remote ip is in INTERNAL_IPS
+        if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
+            return None
+        
+        # Allow acess if the user doing the request is logged in and a
+        # staff member.
+        if hasattr(request, 'user') and request.user.is_staff:
+            return None
+        
+        # Otherwise show the user the 503 page
+        resolver = urlresolvers.get_resolver(None)
+        
+        callback, param_dict = resolver._resolve_special('503')
+        return callback(request, **param_dict)
\ No newline at end of file
diff --git a/apps/maintenancemode/views/__init__.py b/apps/maintenancemode/views/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/apps/maintenancemode/views/defaults.py b/apps/maintenancemode/views/defaults.py
new file mode 100644 (file)
index 0000000..f0bf48f
--- /dev/null
@@ -0,0 +1,16 @@
+from django.template import Context, loader
+
+from maintenancemode import http
+
+def temporary_unavailable(request, template_name='503.html'):
+    """
+    Default 503 handler, which looks for the requested URL in the redirects
+    table, redirects if found, and displays 404 page if not redirected.
+    
+    Templates: `503.html`
+    Context:
+        request_path
+            The path of the requested URL (e.g., '/app/pages/bad_page/')
+    """
+    t = loader.get_template(template_name) # You need to create a 503.html template.
+    return http.HttpResponseTemporaryUnavailable(t.render(Context({})))
\ No newline at end of file
index 1a06476..0551819 100644 (file)
@@ -77,6 +77,8 @@ MIDDLEWARE_CLASSES = (
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'explorer.middleware.EditorSettingsMiddleware',
     'django.middleware.doc.XViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'explorer.middleware.EditorSettingsMiddleware',
     'django.middleware.doc.XViewMiddleware',
+
+    'maintenancemode.middleware.MaintenanceModeMiddleware',
 )
 
 ROOT_URLCONF = 'urls'
 )
 
 ROOT_URLCONF = 'urls'