if a prefix was specified add a filter if the provided user is not authenticated or no user was provided filter the list to only public flatpages retrieve all flatpage objects available for the current site and visible to the specific user or visible to all users if no user is specified populate the template context with them in a variable whose name is defined by the as clause an optional for clause controls the user whose permissions are used in determining which flatpages are visible an optional argument startswith limits the returned flatpages to those beginning with a particular base url this argument can be a variable or a string as it resolves from the template context syntax getflatpages urlstartswith for user as contextname example usage getflatpages as flatpages getflatpages for someuser as flatpages getflatpages about as aboutpages getflatpages prefix as aboutpages getflatpages about for someuser as aboutpages must have at 36 bits in the tag if there s an even number of bits there s no prefix the very last bit must be the context name if there are 5 or 6 bits there is a user defined if a prefix was specified add a filter if the provided user is not authenticated or no user was provided filter the list to only public flatpages retrieve all flatpage objects available for the current site and visible to the specific user or visible to all users if no user is specified populate the template context with them in a variable whose name is defined by the as clause an optional for clause controls the user whose permissions are used in determining which flatpages are visible an optional argument starts_with limits the returned flatpages to those beginning with a particular base url this argument can be a variable or a string as it resolves from the template context syntax get_flatpages url_starts_with for user as context_name example usage get_flatpages as flatpages get_flatpages for someuser as flatpages get_flatpages about as about_pages get_flatpages prefix as about_pages get_flatpages about for someuser as about_pages must have at 3 6 bits in the tag if there s an even number of bits there s no prefix the very last bit must be the context name if there are 5 or 6 bits there is a user defined
from django import template
from django.conf import settings
from django.contrib.flatpages.models import FlatPage
from django.contrib.sites.shortcuts import get_current_site

register = template.Library()


class FlatpageNode(template.Node):
    def __init__(self, context_name, starts_with=None, user=None):
        self.context_name = context_name
        if starts_with:
            self.starts_with = template.Variable(starts_with)
        else:
            self.starts_with = None
        if user:
            self.user = template.Variable(user)
        else:
            self.user = None

    def render(self, context):
        if "request" in context:
            site_pk = get_current_site(context["request"]).pk
        else:
            site_pk = settings.SITE_ID
        flatpages = FlatPage.objects.filter(sites__id=site_pk)
        
        if self.starts_with:
            flatpages = flatpages.filter(
                url__startswith=self.starts_with.resolve(context)
            )

        
        
        if self.user:
            user = self.user.resolve(context)
            if not user.is_authenticated:
                flatpages = flatpages.filter(registration_required=False)
        else:
            flatpages = flatpages.filter(registration_required=False)

        context[self.context_name] = flatpages
        return ""


@register.tag
def get_flatpages(parser, token):
    
    bits = token.split_contents()
    syntax_message = (
        "%(tag_name)s expects a syntax of %(tag_name)s "
        "['url_starts_with'] [for user] as context_name" % {"tag_name": bits[0]}
    )
    
    if 3 <= len(bits) <= 6:
        
        if len(bits) % 2 == 0:
            prefix = bits[1]
        else:
            prefix = None

        
        if bits[-2] != "as":
            raise template.TemplateSyntaxError(syntax_message)
        context_name = bits[-1]

        
        if len(bits) >= 5:
            if bits[-4] != "for":
                raise template.TemplateSyntaxError(syntax_message)
            user = bits[-3]
        else:
            user = None

        return FlatpageNode(context_name, starts_with=prefix, user=user)
    else:
        raise template.TemplateSyntaxError(syntax_message)