Esempio n. 1
0
                def render(self, context):
                    resolved_args, resolved_kwargs = self.get_resolved_arguments(
                        context)
                    _dict = func(*resolved_args, **resolved_kwargs)

                    if not getattr(self, 'nodelist', False):
                        from my_django.template.loader import get_template, select_template
                        if isinstance(file_name, Template):
                            t = file_name
                        elif not isinstance(
                                file_name,
                                basestring) and is_iterable(file_name):
                            t = select_template(file_name)
                        else:
                            t = get_template(file_name)
                        self.nodelist = t.nodelist
                    new_context = context_class(
                        _dict, **{
                            'autoescape': context.autoescape,
                            'current_app': context.current_app,
                            'use_l10n': context.use_l10n,
                            'use_tz': context.use_tz,
                        })
                    # Copy across the CSRF token, if present, because
                    # inclusion tags are often used for forms, and we need
                    # instructions for using CSRF protection to be as simple
                    # as possible.
                    csrf_token = context.get('csrf_token', None)
                    if csrf_token is not None:
                        new_context['csrf_token'] = csrf_token
                    return self.nodelist.render(new_context)
Esempio n. 2
0
def render_flatpage(request, f):
    """
    Internal interface to the flat page view.
    """
    # If registration is required for accessing this page, and the user isn't
    # logged in, redirect to the login page.
    if f.registration_required and not request.user.is_authenticated():
        from my_django.contrib.auth.views import redirect_to_login
        return redirect_to_login(request.path)
    if f.template_name:
        t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
    else:
        t = loader.get_template(DEFAULT_TEMPLATE)

    # To avoid having to always use the "|safe" filter in flatpage templates,
    # mark the title and content as already safe (since they are raw HTML
    # content in the first place).
    f.title = mark_safe(f.title)
    f.content = mark_safe(f.content)

    c = RequestContext(request, {
        'flatpage': f,
    })
    response = HttpResponse(t.render(c))
    populate_xheaders(request, response, FlatPage, f.id)
    return response
def admin_list_filter(cl, spec):
    tpl = get_template(spec.template)
    return tpl.render(
        Context({
            'title': spec.title,
            'choices': list(spec.choices(cl)),
            'spec': spec,
        }))
 def resolve_template(self, template):
     "Accepts a template object, path-to-template or list of paths"
     if isinstance(template, (list, tuple)):
         return loader.select_template(template)
     elif isinstance(template, basestring):
         return loader.get_template(template)
     else:
         return template
Esempio n. 5
0
 def render(self, context):
     try:
         template_name = self.template_name.resolve(context)
         template = get_template(template_name)
         return self.render_template(template, context)
     except:
         if settings.TEMPLATE_DEBUG:
             raise
         return ''
Esempio n. 6
0
 def __init__(self, template_path, *args, **kwargs):
     super(ConstantIncludeNode, self).__init__(*args, **kwargs)
     try:
         t = get_template(template_path)
         self.template = t
     except:
         if settings.TEMPLATE_DEBUG:
             raise
         self.template = None
Esempio n. 7
0
 def get_parent(self, context):
     parent = self.parent_name.resolve(context)
     if not parent:
         error_msg = "Invalid template name in 'extends' tag: %r." % parent
         if self.parent_name.filters or\
                 isinstance(self.parent_name.var, Variable):
             error_msg += " Got this from the '%s' variable." %\
                 self.parent_name.token
         raise TemplateSyntaxError(error_msg)
     if hasattr(parent, 'render'):
         return parent  # parent is a Template object
     return get_template(parent)
    def email(self, comment, content_object, request):
        """
        Send email notification of a new comment to site staff when email
        notifications have been requested.

        """
        if not self.email_notification:
            return
        recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
        t = loader.get_template('comments/comment_notification_email.txt')
        c = Context({ 'comment': comment,
                      'content_object': content_object })
        subject = '[%s] New comment posted on "%s"' % (Site.objects.get_current().name,
                                                          content_object)
        message = t.render(c)
        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
def direct_to_template(request,
                       template,
                       extra_context=None,
                       mimetype=None,
                       **kwargs):
    """
    Render a given template with any extra URL parameters in the context as
    ``{{ params }}``.
    """
    if extra_context is None: extra_context = {}
    dictionary = {'params': kwargs}
    for key, value in extra_context.items():
        if callable(value):
            dictionary[key] = value()
        else:
            dictionary[key] = value
    c = RequestContext(request, dictionary)
    t = loader.get_template(template)
    return HttpResponse(t.render(c), content_type=mimetype)
Esempio n. 10
0
    def get_feed(self, obj, request):
        """
        Returns a feedgenerator.DefaultFeed object, fully populated, for
        this feed. Raises FeedDoesNotExist for invalid parameters.
        """
        current_site = get_current_site(request)

        link = self.__get_dynamic_attr('link', obj)
        link = add_domain(current_site.domain, link, request.is_secure())

        feed = self.feed_type(
            title=self.__get_dynamic_attr('title', obj),
            subtitle=self.__get_dynamic_attr('subtitle', obj),
            link=link,
            description=self.__get_dynamic_attr('description', obj),
            language=settings.LANGUAGE_CODE.decode(),
            feed_url=add_domain(
                current_site.domain,
                self.__get_dynamic_attr('feed_url', obj) or request.path,
                request.is_secure(),
            ),
            author_name=self.__get_dynamic_attr('author_name', obj),
            author_link=self.__get_dynamic_attr('author_link', obj),
            author_email=self.__get_dynamic_attr('author_email', obj),
            categories=self.__get_dynamic_attr('categories', obj),
            feed_copyright=self.__get_dynamic_attr('feed_copyright', obj),
            feed_guid=self.__get_dynamic_attr('feed_guid', obj),
            ttl=self.__get_dynamic_attr('ttl', obj),
            **self.feed_extra_kwargs(obj))

        title_tmp = None
        if self.title_template is not None:
            try:
                title_tmp = loader.get_template(self.title_template)
            except TemplateDoesNotExist:
                pass

        description_tmp = None
        if self.description_template is not None:
            try:
                description_tmp = loader.get_template(
                    self.description_template)
            except TemplateDoesNotExist:
                pass

        for item in self.__get_dynamic_attr('items', obj):
            if title_tmp is not None:
                title = title_tmp.render(
                    RequestContext(request, {
                        'obj': item,
                        'site': current_site
                    }))
            else:
                title = self.__get_dynamic_attr('item_title', item)
            if description_tmp is not None:
                description = description_tmp.render(
                    RequestContext(request, {
                        'obj': item,
                        'site': current_site
                    }))
            else:
                description = self.__get_dynamic_attr('item_description', item)
            link = add_domain(
                current_site.domain,
                self.__get_dynamic_attr('item_link', item),
                request.is_secure(),
            )
            enc = None
            enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
            if enc_url:
                enc = feedgenerator.Enclosure(
                    url=smart_unicode(enc_url),
                    length=smart_unicode(
                        self.__get_dynamic_attr('item_enclosure_length',
                                                item)),
                    mime_type=smart_unicode(
                        self.__get_dynamic_attr('item_enclosure_mime_type',
                                                item)))
            author_name = self.__get_dynamic_attr('item_author_name', item)
            if author_name is not None:
                author_email = self.__get_dynamic_attr('item_author_email',
                                                       item)
                author_link = self.__get_dynamic_attr('item_author_link', item)
            else:
                author_email = author_link = None

            pubdate = self.__get_dynamic_attr('item_pubdate', item)
            if pubdate and is_naive(pubdate):
                ltz = tzinfo.LocalTimezone(pubdate)
                pubdate = pubdate.replace(tzinfo=ltz)

            feed.add_item(
                title=title,
                link=link,
                description=description,
                unique_id=self.__get_dynamic_attr('item_guid', item, link),
                enclosure=enc,
                pubdate=pubdate,
                author_name=author_name,
                author_email=author_email,
                author_link=author_link,
                categories=self.__get_dynamic_attr('item_categories', item),
                item_copyright=self.__get_dynamic_attr('item_copyright', item),
                **self.item_extra_kwargs(item))
        return feed