Source code for mezzanine.pages.templatetags.pages_tags

from __future__ import unicode_literals
from future.builtins import str

from collections import defaultdict

from django.core.exceptions import ImproperlyConfigured
from django.template import TemplateSyntaxError, Variable
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _

from mezzanine.pages.models import Page
from mezzanine.utils.urls import home_slug
from mezzanine import template


register = template.Library()





[docs]@register.as_tag def models_for_pages(*args): """ Create a select list containing each of the models that subclass the ``Page`` model. """ from warnings import warn warn("template tag models_for_pages is deprectaed, use " "PageAdmin.get_content_models instead") from mezzanine.pages.admin import PageAdmin return PageAdmin.get_content_models()
[docs]@register.render_tag def set_model_permissions(context, token): """ Assigns a permissions dict to the given model, much like Django does with its dashboard app list. Used within the change list for pages, to implement permission checks for the navigation tree. """ model = context[token.split_contents()[1]] opts = model._meta perm_name = opts.app_label + ".%s_" + opts.object_name.lower() request = context["request"] setattr(model, "perms", {}) for perm_type in ("add", "change", "delete"): model.perms[perm_type] = request.user.has_perm(perm_name % perm_type) return ""
[docs]@register.render_tag def set_page_permissions(context, token): """ Assigns a permissions dict to the given page instance, combining Django's permission for the page's model and a permission check against the instance itself calling the page's ``can_add``, ``can_change`` and ``can_delete`` custom methods. Used within the change list for pages, to implement permission checks for the navigation tree. """ page = context[token.split_contents()[1]] model = page.get_content_model() try: opts = model._meta except AttributeError: if model is None: error = _("Could not load the model for the following page, " "was it removed?") obj = page else: # A missing inner Meta class usually means the Page model # hasn't been directly subclassed. error = _("An error occured with the following class. Does " "it subclass Page directly?") obj = model.__class__.__name__ raise ImproperlyConfigured(error + " '%s'" % obj) perm_name = opts.app_label + ".%s_" + opts.object_name.lower() request = context["request"] setattr(page, "perms", {}) for perm_type in ("add", "change", "delete"): perm = request.user.has_perm(perm_name % perm_type) perm = perm and getattr(model, "can_%s" % perm_type)(request) page.perms[perm_type] = perm return ""