Skip to content

Commit

Permalink
Updates based on django-roundup feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rossp committed Jul 19, 2013
1 parent 73621fe commit 5879143
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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
--------------

Expand Down
49 changes: 36 additions & 13 deletions readonly/middleware.py
@@ -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':

This comment has been minimized.

Copy link
@kennethlove

kennethlove Jul 19, 2013

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

This comment has been minimized.

Copy link
@rossp

rossp Jul 20, 2013

Author 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'])
Expand Down

0 comments on commit 5879143

Please sign in to comment.