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