Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates based on django-roundup feedback
- Loading branch information
Showing
2 changed files
with
38 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rossp
Author
Owner
|
||
# 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']) | ||
|
I don't think I've ever seen the
<>
operator used in Python before.