def editable(parsed, context, token): """ Add the required HTML to the parsed content for in-line editing, such as the icon and edit form if the object is deemed to be editable - either it has an ``editable`` method which returns ``True``, or the logged in user has change permissions for the model. """ def parse_field(field): field = field.split(".") obj = context[field.pop(0)] attr = field.pop() while field: obj = getattr(obj, field.pop(0)) return obj, attr fields = [parse_field(f) for f in token.split_contents()[1:]] if fields: fields = [f for f in fields if len(f) == 2 and f[0] is fields[0][0]] if not parsed.strip(): try: parsed = "".join([unicode(getattr(*field)) for field in fields]) except AttributeError: pass if fields: obj = fields[0][0] if isinstance(obj, Model) and is_editable(obj, context["request"]): field_names = ",".join([f[1] for f in fields]) context["form"] = get_edit_form(obj, field_names) context["original"] = parsed t = get_template("includes/editable_form.html", context) return t.render(Context(context)) return parsed
def editable(parsed, context, token): """ Add the required HTML to the parsed content for in-line editing, such as the icon and edit form if the object is deemed to be editable - either it has an ``editable`` method which returns ``True``, or the logged in user has change permissions for the model. """ def parse_field(field): field = field.split(".") obj = context[field.pop(0)] attr = field.pop() while field: obj = getattr(obj, field.pop(0)) return obj, attr fields = [parse_field(f) for f in token.split_contents()[1:]] if fields: fields = [f for f in fields if len(f) == 2 and f[0] is fields[0][0]] if not parsed.strip(): try: parsed = "".join([unicode(getattr(*field)) for field in fields]) except AttributeError: pass if fields and "request" in context: obj = fields[0][0] if isinstance(obj, Model) and is_editable(obj, context["request"]): field_names = ",".join([f[1] for f in fields]) context["form"] = get_edit_form(obj, field_names) context["original"] = parsed t = get_template("includes/editable_form.html", context) return t.render(Context(context)) return parsed
def server_error(request, template_name='500.html'): """ Mimics Django's error handler but adds ``MEDIA_URL`` to the context. """ context = RequestContext(request, {"MEDIA_URL": settings.MEDIA_URL}) t = get_template(template_name, context) return http.HttpResponseServerError(t.render(context))
def editable_loader(context): """ Set up the required JS/CSS for the in-line editing toolbar and controls. """ t = get_template("includes/editable_toolbar.html", context) context["REDIRECT_FIELD_NAME"] = REDIRECT_FIELD_NAME context["toolbar"] = t.render(Context(context)) context["richtext_media"] = RichTextField().formfield().widget.media return context
def editable_loader(context): """ Set up the required JS/CSS for the in-line editing toolbar and controls. """ t = get_template("includes/editable_toolbar.html", context) context["REDIRECT_FIELD_NAME"] = REDIRECT_FIELD_NAME context["toolbar"] = t.render(Context(context)) context["html_editor_js"] = HtmlField().formfield().widget.Media.js return context
def page_menu(context, token): """ Return a list of child pages for the given parent, storing all pages in a dict in the context when first called using parents as keys for retrieval on subsequent recursive calls from the menu template. """ # First arg could be the menu template file name, or the parent page. # Also allow for both to be used. template_name = None parent_page = None parts = token.split_contents()[1:] for part in parts: part = Variable(part).resolve(context) if isinstance(part, unicode): template_name = part elif isinstance(part, Page): parent_page = part if template_name is None: try: template_name = context["menu_template_name"] except KeyError: error = "No template found for page_menu in: %s" % parts raise TemplateSyntaxError(error) context["menu_template_name"] = template_name if "menu_pages" not in context: pages = defaultdict(list) try: user = context["request"].user slug = context["request"].path except KeyError: user = None slug = "" num_children = lambda id: lambda: len(context["menu_pages"][id]) has_children = lambda id: lambda: num_children(id)() > 0 published = Page.objects.published(for_user=user) for page in published.select_related(depth=2).order_by("_order"): page.set_menu_helpers(slug) setattr(page, "num_children", num_children(page.id)) setattr(page, "has_children", has_children(page.id)) pages[page.parent_id].append(page) context["menu_pages"] = pages context["on_home"] = slug == reverse("home") # ``branch_level`` must be stored against each page so that the # calculation of it is correctly applied. This looks weird but if we do # the ``branch_level`` as a separate arg to the template tag with the # addition performed on it, the addition occurs each time the template # tag is called rather than once per level. context["branch_level"] = 0 if parent_page is not None: context["branch_level"] = getattr(parent_page, "branch_level", 0) + 1 parent_page = parent_page.id context["page_branch"] = context["menu_pages"].get(parent_page, []) for i, page in enumerate(context["page_branch"]): context["page_branch"][i].branch_level = context["branch_level"] t = get_template(template_name, context) return t.render(context)
def superfish_submenu(context, token): """ Return a list of child pages for the given parent, storing all pages in a dict in the context when first called using parents as keys for retrieval on subsequent recursive calls from the menu template. """ # First arg could be the menu template file name, or the parent page. # Also allow for both to be used. # logging.debug('superfish_submenu') template_name = None parent_page = None parts = token.split_contents()[1:] for part in parts: part = Variable(part).resolve(context) if isinstance(part, unicode): template_name = part elif isinstance(part, Page): parent_page = part if template_name is None: try: template_name = "pages/menus/superfishtree.html" # template_name = context["menu_template_name"] except KeyError: error = "No template found for page_menu in: %s" % parts raise TemplateSyntaxError(error) context["menu_template_name"] = template_name # logging.debug("context['menu_template_name'] is " + context["menu_template_name"]) if "menu_pages" not in context: pages = defaultdict(list) try: user = context["request"].user slug = context["request"].path except KeyError: user = None slug = "" for page in Page.objects.published(for_user=user).select_related( depth=2).order_by("_order"): page.set_menu_helpers(slug) pages[page.parent_id].append(page) context["menu_pages"] = pages context["on_home"] = slug == reverse("home") # ``branch_level`` must be stored against each page so that the # calculation of it is correctly applied. This looks weird but if we do # the ``branch_level`` as a separate arg to the template tag with the # addition performed on it, the addition occurs each time the template # tag is called rather than once per level. context["branch_level"] = 0 if parent_page is not None: context["branch_level"] = parent_page.branch_level + 1 parent_page = parent_page.id context["page_branch"] = context["menu_pages"].get(parent_page, []) for i, page in enumerate(context["page_branch"]): context["page_branch"][i].branch_level = context["branch_level"] t = get_template(template_name, context) # logging.debug(context["page_branch"]) return t.render(context)
def get_parent(self, context): """ Override to use Mezzanine's context-aware ``get_template``. """ if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = self.parent_name if not parent: error_msg = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: error_msg += (" Got this from the '%s' variable." % self.parent_name_expr.token) raise TemplateSyntaxError(error_msg) if hasattr(parent, "render"): return parent # parent is a Template object return get_template(parent, context)
def render_to_response(template_name, dictionary=None, context_instance=None, mimetype=None): """ Mimics ``django.shortcuts.render_to_response`` but uses Mezzanine's ``get_template`` which handles device specific template directories. """ dictionary = dictionary or {} if context_instance: context_instance.update(dictionary) else: context_instance = Context(dictionary) if isinstance(template_name, (list, tuple)): t = select_template(template_name, context_instance) else: t = get_template(template_name, context_instance) return HttpResponse(t.render(context_instance), mimetype=mimetype)
def render(self, context): if not getattr(self, "nodelist", False): if not isinstance(file_name, basestring) and \ is_iterable(file_name): t = select_template(file_name, context) else: t = get_template(file_name, context) self.nodelist = t.nodelist parts = [template.Variable(part).resolve(context) for part in token.split_contents()[1:]] if takes_context: parts.insert(0, context) result = tag_func(*parts) autoescape = context.autoescape context = context_class(result, autoescape=autoescape) return self.nodelist.render(context)
def get_parent(self, context): """ Override to use Mezzanine's context-aware ``get_template``. """ if self.parent_name_expr: self.parent_name = self.parent_name_expr.resolve(context) parent = self.parent_name if not parent: error = "Invalid template name in 'extends' tag: %r." % parent if self.parent_name_expr: token = self.parent_name_expr.token error += " Got this from the '%s' variable." % token raise TemplateSyntaxError(error) if hasattr(parent, "render"): return parent # parent is a Template object return get_template(parent, context)
def include(context, token): """ Loads a template and renders it with the current context. """ bits = token.split_contents() if len(bits) != 2: raise TemplateSyntaxError("%r tag takes one argument: the name of " "the template to be included" % bits[0]) template = bits[1] if template[0] in ('"', "'") and template[-1] == template[0]: template = template[1:-1] else: template = Variable(template).resolve(context) try: t = get_template(template, context) return t.render(context) except TemplateSyntaxError: if settings.TEMPLATE_DEBUG: raise return ""