Skip to content

Commit

Permalink
Merge pull request #32 from pbs/LUN-1869
Browse files Browse the repository at this point in the history
LUN-1869: Since we are managing the state of the current site id, it is ...
  • Loading branch information
Laura Feier committed Oct 16, 2014
2 parents c5ec494 + 7f98450 commit deb9ef2
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions cms_templates/middleware.py
Expand Up @@ -38,6 +38,34 @@ def _import_function(func_path):
return list(get_user_sites_queryset(user).values_list('id', flat=True))


def _set_cms_templates_for_request(request):
'''Sets the CMS_TEMPLATES to the list of templates for the current site.
current site == session['cms_admin_site']
Thus this function makes sure CMS_TEMPLATES's value is correct
with respect to the current request.
'''
CMS_TEMPLATES = settings.__class__.CMS_TEMPLATES
CMS_TEMPLATES.value = [('dummy', 'Please create a template first.'),
(settings.CMS_TEMPLATE_INHERITANCE_MAGIC,
CMS_TEMPLATE_INHERITANCE_TITLE)
]
site_id = request.session.get('cms_admin_site', settings.SITE_ID)
try:
templates = get_site_templates(site_id)
except (Site.DoesNotExist, ValueError):
logger.error('Current site not found: %d. '
'It was probably deleted' % site_id)
raise Http404
CMS_TEMPLATES = settings.__class__.CMS_TEMPLATES
CMS_TEMPLATES.value = [(templ.name, templ.name) for templ in templates]
if not CMS_TEMPLATES.value:
CMS_TEMPLATES.value = [('dummy',
'Please create a template first.')]
CMS_TEMPLATES.value.append((settings.CMS_TEMPLATE_INHERITANCE_MAGIC,
CMS_TEMPLATE_INHERITANCE_TITLE))


class SiteIDPatchMiddleware(object):
""" This middleware works together with DynamicSiteIDMiddleware
from djangotoolbox and patches the site id based on the
Expand All @@ -56,7 +84,11 @@ def process_request(self, request):
logger.warning("SiteIDPatchMiddleware is raising {0}\n\n. "
"Using {1} and bubble up".format(e, self.fallback))
self.fallback.process_request(request)
# try to set the correct value for CMS_TEMPLATES.
# this ensures we are able to correctly display the CMS error page
_set_cms_templates_for_request(request)
raise e

user = getattr(request, 'user', None)

if (match.app_name == 'admin'
Expand Down Expand Up @@ -114,18 +146,7 @@ def get_site_templates(site_id=None):

class DBTemplatesMiddleware(object):
def process_request(self, request):
site_id = request.session.get('cms_admin_site', settings.SITE_ID)
try:
templates = get_site_templates(site_id)
except Site.DoesNotExist:
logger.error('Current site not found: %d. '
'It was probably deleted' % site_id)
raise Http404
CMS_TEMPLATES = settings.__class__.CMS_TEMPLATES
CMS_TEMPLATES.value = [(templ.name, templ.name) for templ in templates]
if not CMS_TEMPLATES.value:
CMS_TEMPLATES.value = [('dummy',
'Please create a template first.')]
_set_cms_templates_for_request(request)

# This is a huge hack.
# Expand the model choices field to contain all templates.
Expand All @@ -135,5 +156,3 @@ def process_request(self, request):
choices += [(settings.CMS_TEMPLATE_INHERITANCE_MAGIC,
CMS_TEMPLATE_INHERITANCE_TITLE)]
Page._meta.get_field_by_name('template')[0].choices[:] = choices
CMS_TEMPLATES.value.append((settings.CMS_TEMPLATE_INHERITANCE_MAGIC,
CMS_TEMPLATE_INHERITANCE_TITLE))

0 comments on commit deb9ef2

Please sign in to comment.