Exemple #1
0
def custom_links(obj):
    """
    Render all applicable links for the given object.
    """
    content_type = ContentType.objects.get_for_model(obj)
    custom_links = CustomLink.objects.filter(content_type=content_type)
    if not custom_links:
        return ''

    context = {
        'obj': obj,
    }
    template_code = ''
    group_names = OrderedDict()

    for cl in custom_links:

        # Organize custom links by group
        if cl.group_name and cl.group_name in group_names:
            group_names[cl.group_name].append(cl)
        elif cl.group_name:
            group_names[cl.group_name] = [cl]

        # Add non-grouped links
        else:
            try:
                text_rendered = render_jinja2(cl.text, context)
                if text_rendered:
                    link_rendered = render_jinja2(cl.url, context)
                    link_target = ' target="_blank"' if cl.new_window else ''
                    template_code += LINK_BUTTON.format(
                        link_rendered, link_target, cl.button_class,
                        text_rendered)
            except Exception as e:
                template_code += '<a class="btn btn-sm btn-default" disabled="disabled" title="{}">' \
                                 '<i class="fa fa-warning"></i> {}</a>\n'.format(e, cl.name)

    # Add grouped links to template
    for group, links in group_names.items():

        links_rendered = []

        for cl in links:
            try:
                text_rendered = render_jinja2(cl.text, context)
                if text_rendered:
                    link_target = ' target="_blank"' if cl.new_window else ''
                    links_rendered.append(
                        GROUP_LINK.format(cl.url, link_target, cl.text))
            except Exception as e:
                links_rendered.append(
                    '<li><a disabled="disabled" title="{}"><span class="text-muted">'
                    '<i class="fa fa-warning"></i> {}</span></a></li>'.format(
                        e, cl.name))

        if links_rendered:
            template_code += GROUP_BUTTON.format(links[0].button_class, group,
                                                 ''.join(links_rendered))

    return mark_safe(template_code)
Exemple #2
0
 def render_body(self, context):
     """
     Render the body template, if defined. Otherwise, jump the context as a JSON object.
     """
     if self.body_template:
         return render_jinja2(self.body_template, context)
     else:
         return json.dumps(context, cls=JSONEncoder)
Exemple #3
0
    def render(self, context):
        """
        Render the CustomLink given the provided context, and return the text, link, and link_target.

        :param context: The context passed to Jinja2
        """
        text = render_jinja2(self.link_text, context)
        if not text:
            return {}
        link = render_jinja2(self.link_url, context)
        link_target = ' target="_blank"' if self.new_window else ''

        return {
            'text': text,
            'link': link,
            'link_target': link_target,
        }
Exemple #4
0
    def embed_url(self, obj):
        context = {'obj': obj}

        if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
            template = Template(self.source)
            return template.render(Context(context))

        elif self.template_language == TemplateLanguageChoices.LANGUAGE_JINJA2:
            return render_jinja2(self.source, context)
Exemple #5
0
    def render(self, queryset):
        """
        Render the contents of the template.
        """
        context = {'queryset': queryset}
        output = render_jinja2(self.template_code, context)

        # Replace CRLF-style line terminators
        output = output.replace('\r\n', '\n')

        return output
Exemple #6
0
 def render_headers(self, context):
     """
     Render additional_headers and return a dict of Header: Value pairs.
     """
     if not self.additional_headers:
         return {}
     ret = {}
     data = render_jinja2(self.additional_headers, context)
     for line in data.splitlines():
         header, value = line.split(':')
         ret[header.strip()] = value.strip()
     return ret
Exemple #7
0
    def embed_link(self, obj):
        if self.link is None:
            return ''

        context = {'obj': obj}

        if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
            template = Template(self.link)
            return template.render(Context(context))

        elif self.template_language == TemplateLanguageChoices.LANGUAGE_JINJA2:
            return render_jinja2(self.link, context)
Exemple #8
0
    def render(self, queryset):
        """
        Render the contents of the template.
        """
        context = {'queryset': queryset}

        if self.template_language == TemplateLanguageChoices.LANGUAGE_DJANGO:
            template = Template(self.template_code)
            output = template.render(Context(context))

        elif self.template_language == TemplateLanguageChoices.LANGUAGE_JINJA2:
            output = render_jinja2(self.template_code, context)

        else:
            return None

        # Replace CRLF-style line terminators
        output = output.replace('\r\n', '\n')

        return output
Exemple #9
0
def custom_links(context, obj):
    """
    Render all applicable links for the given object.
    """
    content_type = ContentType.objects.get_for_model(obj)
    custom_links = CustomLink.objects.filter(content_type=content_type)
    if not custom_links:
        return ''

    # Pass select context data when rendering the CustomLink
    link_context = {
        'obj': obj,
        'debug':
        context.get('debug',
                    False),  # django.template.context_processors.debug
        'request':
        context['request'],  # django.template.context_processors.request
        'user': context['user'],  # django.contrib.auth.context_processors.auth
        'perms':
        context['perms'],  # django.contrib.auth.context_processors.auth
    }
    template_code = ''
    group_names = OrderedDict()

    for cl in custom_links:

        # Organize custom links by group
        if cl.group_name and cl.group_name in group_names:
            group_names[cl.group_name].append(cl)
        elif cl.group_name:
            group_names[cl.group_name] = [cl]

        # Add non-grouped links
        else:
            try:
                text_rendered = render_jinja2(cl.link_text, link_context)
                if text_rendered:
                    link_rendered = render_jinja2(cl.link_url, link_context)
                    link_target = ' target="_blank"' if cl.new_window else ''
                    template_code += LINK_BUTTON.format(
                        link_rendered, link_target, cl.button_class,
                        text_rendered)
            except Exception as e:
                template_code += '<a class="btn btn-sm btn-default" disabled="disabled" title="{}">' \
                                 '<i class="mdi mdi-alert"></i> {}</a>\n'.format(e, cl.name)

    # Add grouped links to template
    for group, links in group_names.items():

        links_rendered = []

        for cl in links:
            try:
                text_rendered = render_jinja2(cl.link_text, link_context)
                if text_rendered:
                    link_target = ' target="_blank"' if cl.new_window else ''
                    link_rendered = render_jinja2(cl.link_url, link_context)
                    links_rendered.append(
                        GROUP_LINK.format(link_rendered, link_target,
                                          text_rendered))
            except Exception as e:
                links_rendered.append(
                    '<li><a disabled="disabled" title="{}"><span class="text-muted">'
                    '<i class="mdi mdi-alert"></i> {}</span></a></li>'.format(
                        e, cl.name))

        if links_rendered:
            template_code += GROUP_BUTTON.format(links[0].button_class, group,
                                                 ''.join(links_rendered))

    return mark_safe(template_code)