-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tup-706 conditionally serve raw content
- Loading branch information
1 parent
ba50574
commit 12c02ea
Showing
2 changed files
with
50 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
CUSTOM_APPS = ['apps.custom_example'] | ||
CUSTOM_MIDDLEWARE = [] | ||
CUSTOM_MIDDLEWARE = ['taccsite_cms.middleware.cms_template_middleware.CMSTemplateMiddleware'] | ||
STATICFILES_DIRS = () |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Change CMS page template at runtime""" | ||
import os | ||
|
||
from cms.middleware.toolbar import ToolbarMiddleware | ||
from cms.models.pagemodel import Page as CMS_Page | ||
|
||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates') | ||
|
||
class CMSTemplateMiddleware(ToolbarMiddleware): | ||
""" | ||
Use a different CMS template than is set for the current page | ||
Usage: | ||
http://0.0.0.0:8000/news/?raw | ||
http://0.0.0.0:8000/news/?template=raw.html | ||
Applies `raw.html` template | ||
http://0.0.0.0:8000/news/?template=fullwidth.html | ||
Applies `fulwidth.html` template | ||
http://0.0.0.0:8000/news/?template=misspelling.html | ||
Raises `TemplateDoesNotExist` error | ||
http://0.0.0.0:8000/news/?rawng | ||
http://0.0.0.0:8000/news/?template | ||
http://0.0.0.0:8000/news/?template=s | ||
No effect | ||
""" | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
if ( | ||
hasattr(request, 'current_page') and | ||
isinstance(request.current_page, CMS_Page) | ||
): | ||
page = request.current_page | ||
query_params = request.GET | ||
query_template = query_params.get('template', '') | ||
|
||
if 'raw' in query_params: | ||
page.template = os.path.join(TEMPLATE_DIR, 'raw.html') | ||
if query_template.endswith('.html'): | ||
page.template = os.path.join(TEMPLATE_DIR, query_template) | ||
|
||
response = self.get_response(request) | ||
|
||
return response |