From: Ɓukasz Rekucki Date: Sat, 26 Sep 2009 09:55:16 +0000 (+0200) Subject: Added maintance mode middleware. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/a8e1fae76ff2277a387f403600fe31bef4b2387e?ds=inline Added maintance mode middleware. --- diff --git a/apps/api/handlers/library_handlers.py b/apps/api/handlers/library_handlers.py index 45dff83b..18dd3e81 100644 --- a/apps/api/handlers/library_handlers.py +++ b/apps/api/handlers/library_handlers.py @@ -35,6 +35,7 @@ class BasicLibraryHandler(AnonymousBaseHandler): 'name': docid } for docid in lib.documents() ] return {'documents' : document_list} + 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() ] - return {'documents' : document_list } + return {'documents' : document_list } @validate_form(forms.DocumentUploadForm, 'POST') def create(self, request, form): @@ -137,6 +138,10 @@ class DocumentHandler(BaseHandler): return result + def update(self, request, docid): + """Update information about the document, like display not""" + return + # # Document Text View # diff --git a/apps/api/response.py b/apps/api/response.py index 322118a0..6b685624 100644 --- a/apps/api/response.py +++ b/apps/api/response.py @@ -22,7 +22,7 @@ class ResponseObject(object): 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, \ @@ -60,7 +60,6 @@ class SuccessNoContent(ResponseObject): def django_response(self): return ResponseObject.django_response(self, body=None) - # # Client errors # @@ -96,6 +95,14 @@ class EntityConflict(ResponseObject): 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 diff --git a/apps/maintenancemode/._middleware.py b/apps/maintenancemode/._middleware.py new file mode 100644 index 00000000..d25f019d 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 index 00000000..e69de29b diff --git a/apps/maintenancemode/conf/._settings.py b/apps/maintenancemode/conf/._settings.py new file mode 100644 index 00000000..a8981627 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 index 00000000..e69de29b diff --git a/apps/maintenancemode/conf/settings.py b/apps/maintenancemode/conf/settings.py new file mode 100644 index 00000000..4d0b1a51 --- /dev/null +++ b/apps/maintenancemode/conf/settings.py @@ -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 index 00000000..e69de29b diff --git a/apps/maintenancemode/conf/urls/defaults.py b/apps/maintenancemode/conf/urls/defaults.py new file mode 100644 index 00000000..20029db5 --- /dev/null +++ b/apps/maintenancemode/conf/urls/defaults.py @@ -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 index 00000000..4b3023b6 --- /dev/null +++ b/apps/maintenancemode/http.py @@ -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 index 00000000..404b1a05 --- /dev/null +++ b/apps/maintenancemode/middleware.py @@ -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 index 00000000..e69de29b diff --git a/apps/maintenancemode/views/defaults.py b/apps/maintenancemode/views/defaults.py new file mode 100644 index 00000000..f0bf48fe --- /dev/null +++ b/apps/maintenancemode/views/defaults.py @@ -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 diff --git a/project/settings.py b/project/settings.py index 1a064762..05518193 100644 --- a/project/settings.py +++ b/project/settings.py @@ -77,6 +77,8 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'explorer.middleware.EditorSettingsMiddleware', 'django.middleware.doc.XViewMiddleware', + + 'maintenancemode.middleware.MaintenanceModeMiddleware', ) ROOT_URLCONF = 'urls'