PageRenderTime 57ms CodeModel.GetById 46ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/flatpages/templatetags/flatpages.py

https://code.google.com/p/mango-py/
Python | 98 lines | 88 code | 7 blank | 3 comment | 6 complexity | 6ae6fe1172ec2f0454dc2e30ab51eacb MD5 | raw file
 1from django import template
 2from django.conf import settings
 3from django.contrib.flatpages.models import FlatPage
 4
 5
 6register = template.Library()
 7
 8
 9class FlatpageNode(template.Node):
10    def __init__(self, context_name, starts_with=None, user=None):
11        self.context_name = context_name
12        if starts_with:
13            self.starts_with = template.Variable(starts_with)
14        else:
15            self.starts_with = None
16        if user:
17            self.user = template.Variable(user)
18        else:
19            self.user = None
20
21    def render(self, context):
22        flatpages = FlatPage.objects.filter(sites__id=settings.SITE_ID)
23        # If a prefix was specified, add a filter
24        if self.starts_with:
25            flatpages = flatpages.filter(
26                url__startswith=self.starts_with.resolve(context))
27
28        # If the provided user is not authenticated, or no user
29        # was provided, filter the list to only public flatpages.
30        if self.user:
31            user = self.user.resolve(context)
32            if not user.is_authenticated():
33                flatpages = flatpages.filter(registration_required=False)
34        else:
35            flatpages = flatpages.filter(registration_required=False)
36
37        context[self.context_name] = flatpages
38        return ''
39
40
41def get_flatpages(parser, token):
42    """
43    Retrieves all flatpage objects available for the current site and
44    visible to the specific user (or visible to all users if no user is
45    specified). Populates the template context with them in a variable
46    whose name is defined by the ``as`` clause.
47
48    An optional ``for`` clause can be used to control the user whose
49    permissions are to be used in determining which flatpages are visible.
50
51    An optional argument, ``starts_with``, can be applied to limit the
52    returned flatpages to those beginning with a particular base URL.
53    This argument can be passed as a variable or a string, as it resolves
54    from the template context.
55
56    Syntax::
57
58        {% get_flatpages ['url_starts_with'] [for user] as context_name %}
59
60    Example usage::
61
62        {% get_flatpages as flatpages %}
63        {% get_flatpages for someuser as flatpages %}
64        {% get_flatpages '/about/' as about_pages %}
65        {% get_flatpages prefix as about_pages %}
66        {% get_flatpages '/about/' for someuser as about_pages %}
67    """
68    bits = token.split_contents()
69    syntax_message = ("%(tag_name)s expects a syntax of %(tag_name)s "
70                       "['url_starts_with'] [for user] as context_name" %
71                       dict(tag_name=bits[0]))
72   # Must have at 3-6 bits in the tag
73    if len(bits) >= 3 and len(bits) <= 6:
74
75        # If there's an even number of bits, there's no prefix
76        if len(bits) % 2 == 0:
77            prefix = bits[1]
78        else:
79            prefix = None
80
81        # The very last bit must be the context name
82        if bits[-2] != 'as':
83            raise template.TemplateSyntaxError(syntax_message)
84        context_name = bits[-1]
85
86        # If there are 5 or 6 bits, there is a user defined
87        if len(bits) >= 5:
88            if bits[-4] != 'for':
89                raise template.TemplateSyntaxError(syntax_message)
90            user = bits[-3]
91        else:
92            user = None
93
94        return FlatpageNode(context_name, starts_with=prefix, user=user)
95    else:
96        raise template.TemplateSyntaxError(syntax_message)
97
98register.tag('get_flatpages', get_flatpages)