You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flatpages.py 3.5KB

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