Permalink
Browse files

Updates based on django-roundup feedback

  • Loading branch information...
1 parent 73621fe commit 5879143d658a799798cc8b5d14b037ce57348edb @rossp committed Jul 19, 2013
Showing with 38 additions and 13 deletions.
  1. +2 −0 README.md
  2. +36 −13 readonly/middleware.py
View
@@ -5,6 +5,8 @@ Take parts of your Django website offline by changing `settings.SITE_READ_ONLY =
It does this by rendering a static template for your data-modifying views. I use this for my accounts page & signup page, so I can effectively freeze the site during big changes or migrations.
+Note that django-readonly-site doesn't prevent any database updates. If you need to do this, then you should look at tools that override your database cursor, such as [django-db-readonly](https://github.com/streeter/django-db-readonly) or by doing it at the database level with Postgres slaves or similar. I don't require this for my purposes, and there's no point adding duplicate functionality to this app.
+
Pre-Requisites
--------------
@@ -1,33 +1,56 @@
+from django.shortcuts import render
+
class ReadOnlySiteMiddleware(object):
def process_request(self, request):
+
+ """
+ If we are in read-only mode, figure out whether or not to render the request.
+ We read in settings at the last possible minute sure that any dynamic
+ settings are honored (eg django-dbsettings or similar)
+ """
from django.conf import settings
- from django.shortcuts import render
readonly = getattr(settings, 'SITE_READ_ONLY', False)
- exempt_path_starts = getattr(settings, 'READ_ONLY_EXEMPT_PATH_STARTS', [])
- exempt_paths = getattr(settings, 'READ_ONLY_EXEMPT_PATHS', [])
- path_starts = getattr(settings, 'READ_ONLY_PATH_STARTS', [])
- paths = getattr(settings, 'READ_ONLY_PATHS', [])
- template = getattr(settings, 'READ_ONLY_TEMPLATE', None)
if not readonly:
+ # Shortcut out if we aren't in read-only mode
return None
+
- if exempt_path_starts or exempt_paths:
+ exempt_path_starts = getattr(settings, 'READ_ONLY_EXEMPT_PATH_STARTS', [])
+ exempt_paths = getattr(settings, 'READ_ONLY_EXEMPT_PATHS', [])
+ blacklist_path_starts = getattr(settings, 'READ_ONLY_PATH_STARTS', [])
+ blacklist_paths = getattr(settings, 'READ_ONLY_PATHS', [])
+ template = getattr(settings, 'READ_ONLY_TEMPLATE', None)
+
+
+ if request.method <> 'GET':
@kennethlove
kennethlove Jul 19, 2013

I don't think I've ever seen the <> operator used in Python before.

@rossp
rossp Jul 20, 2013 Owner

grr - too many languages going on in my head when I updated this. Will fix in a moment, thanks.

+ # Shortcut - will skip rest of this if statement
+ # and just render the 'read only' template.
+ pass
+
+ elif exempt_path_starts or exempt_paths:
+ # Deal with whitelisted items
for path in exempt_path_starts:
if request.path.startswith(path):
return None
- for path in exempt_paths:
- if request.path == path:
- return None
+ if request.path in exempt_paths:
+ return None
else:
- is_black_listed_path = (request.path in paths) or \
- any(request.path.startswith(path) for path in path_starts)
+ # Deal with blacklist
+ is_blacklisted = False
+
+ if request.path in blacklist_paths:
+ is_blacklisted = True
+
+ for path in blacklist_path_starts:
+ if request.path.startswith(path):
+ is_blacklisted = True
- if not is_black_listed_path:
+ if not is_blacklisted:
return None
return render(request, [template, 'readonly/readonly.html'])

0 comments on commit 5879143

Please sign in to comment.