Example #1
0
    def handle_token(cls, parser, token):
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError(
                "Second argument in %r tag must be 'for'" % (tokens[0], ))

        if len(tokens) < 7:
            # Default get_comment_form code
            return super(CommentFormNode, cls).handle_token(parser, token)
        elif len(tokens) == 7:
            # {% get_comment_form for [object] as [varname] with [parent_id] %}
            if tokens[-2] != 'with':
                raise template.TemplateSyntaxError(
                    "%r tag must have a 'with' as the last but one argument." %
                    (tokens[0], ))
            return cls(
                object_expr=parser.compile_filter(tokens[2]),
                as_varname=tokens[4],
                parent=parser.compile_filter(tokens[6]),
            )
        elif len(tokens) == 8:
            # {% get_comment_form for [app].[model] [object_id] as [varname] with [parent_id] %}
            if tokens[-2] != 'with':
                raise template.TemplateSyntaxError(
                    "%r tag must have a 'with' as the last but one argument." %
                    (tokens[0], ))
            return cls(
                ctype=BaseCommentNode.lookup_content_type(
                    tokens[2], tokens[0]),
                object_pk_expr=parser.compile_filter(tokens[3]),
                as_varname=tokens[5],
                parent=parser.compile_filter(tokens[7]),
            )
    def handle_token(cls, parser, token):
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        extra_kw = {}
        if tokens[-2] == 'limit':
            if tokens[-1].isdigit():
                extra_kw['limit'] = tokens.pop(-1)  # removes limit integer
                tokens.pop(-1)  # removes 'limit'
            else:
                raise template.TemplateSyntaxError(
                    "When using 'limit' with %r tag, it needs to be followed by a positive integer" % (tokens[0],))
        if tokens[-1] in ('flat', 'root_only', 'newest', 'admin'):
            extra_kw[str(tokens.pop())] = True

        if len(tokens) == 3:
            # {% render_comment_list for obj %}
            return cls(
                object_expr=parser.compile_filter(tokens[2]),
                **extra_kw
            )
        elif len(tokens) == 4:
            # {% render_comment_list for app.models pk %}
            return cls(
                ctype=BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr=parser.compile_filter(tokens[3]),
                **extra_kw
            )
        else:
            raise template.TemplateSyntaxError("%r tag takes either 2 or 3 arguments" % (tokens[0],))
Example #3
0
    def handle_token(cls, parser, token):
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError(
                "Second argument in %r tag must be 'for'" % tokens[0])

        extra_kw = {}
        if tokens[-2] == 'limit':
            if tokens[-1].isdigit():
                extra_kw['limit'] = tokens.pop(-1)  # removes limit integer
                tokens.pop(-1)  # removes 'limit'
            else:
                raise template.TemplateSyntaxError(
                    "When using 'limit' with %r tag, it needs to be followed by a positive integer"
                    % (tokens[0], ))
        if tokens[-1] in ('flat', 'root_only', 'newest', 'admin'):
            extra_kw[str(tokens.pop())] = True

        if len(tokens) == 3:
            # {% render_comment_list for obj %}
            return cls(object_expr=parser.compile_filter(tokens[2]),
                       **extra_kw)
        elif len(tokens) == 4:
            # {% render_comment_list for app.models pk %}
            return cls(ctype=BaseCommentNode.lookup_content_type(
                tokens[2], tokens[0]),
                       object_pk_expr=parser.compile_filter(tokens[3]),
                       **extra_kw)
        else:
            raise template.TemplateSyntaxError(
                "%r tag takes either 2 or 3 arguments" % (tokens[0], ))
    def handle_token(cls, parser, token):
        """
            Class method to parse get_comment_list/count/form and return a Node.

            Forked from django_comments.templatetags. with_parent, 
            root-only concepts borrowed from django-threadedcomments.
        """
        tokens = token.contents.split()
        
        with_parent = None
        extra_kw = {}
        extra_possible_kw = ('root_only', 'flat', 'reverse', 'sort=mostcommented', 'sort=mostrecentreplies')
        for dummy in extra_possible_kw:
            if tokens[-1] in extra_possible_kw:
                split = str(tokens.pop()).split('=')
                key = split[0]
                extra_kw[key] = len(split) > 1 and split[1] or True

        if tokens[1] != 'for':
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        # {% get_whatever for obj as varname %}
        # {% get_whatever for obj as varname with parent %}
        if len(tokens) == 5 or len(tokens) == 7:
            if tokens[3] != 'as':
                raise template.TemplateSyntaxError("Third argument in %r must be 'as'" % tokens[0])
            if len(tokens) == 7:
                if tokens[5] != 'with':
                    raise template.TemplateSyntaxError("When 6 arguments are given, fifth argument in %r must be 'with' followed by the parent commment wanted" % tokens[0])
                with_parent = tokens[6]
            return cls(
                object_expr = parser.compile_filter(tokens[2]),
                as_varname = tokens[4],
                with_parent = with_parent,
                **extra_kw
            )

        # {% get_whatever for app.model pk as varname %}
        # {% get_whatever for app.model pk as varname with parent %}
        elif len(tokens) == 6 or len(tokens) == 8:
            if tokens[4] != 'as':
                raise template.TemplateSyntaxError("Fourth argument in %r must be 'as'" % tokens[0])
            if len(tokens) == 8:
                if tokens[6] != 'with':
                    raise template.TemplateSyntaxError("When 6 arguments are given, fifth argument in %r must be 'with' followed by the parent commment wanted" % tokens[0])
                with_parent = tokens[7]
            return cls(
                ctype = BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr = parser.compile_filter(tokens[3]),
                as_varname = tokens[5],
                with_parent = with_parent,
                **extra_kw
            )

        else:
            raise template.TemplateSyntaxError("%r tag requires 4, 5, 6 or 7 arguments" % tokens[0])
    def handle_token(cls, parser, token):
        """Class method to parse render_comment_form and return a Node."""
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        # {% render_comment_form for obj %}
        if len(tokens) == 3:
            return cls(object_expr=parser.compile_filter(tokens[2]))

        # {% render_comment_form for app.models pk %}
        elif len(tokens) == 4:
            return cls(
                ctype = BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr = parser.compile_filter(tokens[3])
            )
    def handle_token(cls, parser, token):
        """Class method to parse render_comment_form and return a Node."""
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError(
                "Second argument in %r tag must be 'for'" % tokens[0])

        # {% render_comment_form for obj %}
        if len(tokens) == 3:
            return cls(object_expr=parser.compile_filter(tokens[2]))

        # {% render_comment_form for app.models pk %}
        elif len(tokens) == 4:
            return cls(ctype=BaseCommentNode.lookup_content_type(
                tokens[2], tokens[0]),
                       object_pk_expr=parser.compile_filter(tokens[3]))
    def handle_token(cls, parser, token):
        """
        Class method to parse render_comment_form and return a Node.
        """
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError(
                "Second argument in %r tag must be 'for'" % tokens[0])

        if len(tokens) == 3:
            # {% render_comment_form for obj %}
            return cls(object_expr=parser.compile_filter(tokens[2]))
        elif len(tokens) == 4:
            # {% render_comment_form for app.model object_pk %}
            return cls(
                ctype=BaseCommentNode.lookup_content_type(
                    tokens[2], tokens[0]),
                object_pk_expr=parser.compile_filter(tokens[3]))
        elif len(tokens) == 5:
            # {% render_comment_form for obj with parent_id %}
            if tokens[-2] != u'with':
                raise template.TemplateSyntaxError(
                    "%r tag must have 'with' as the last but one argument" %
                    (tokens[0], ))
            return cls(
                object_expr=parser.compile_filter(tokens[2]),
                parent=parser.compile_filter(tokens[4]))
        elif len(tokens) == 6:
            # {% render_comment_form for app.model object_pk with parent_id %}
            if tokens[-2] != u'with':
                raise template.TemplateSyntaxError(
                    "%r tag must have 'with' as the last but one argument" %
                    (tokens[0], ))
            return cls(
                ctype=BaseThreadedCommentNode.lookup_content_type(
                    tokens[2], tokens[0]),
                object_pk_expr=parser.compile_filter(tokens[3]),
                parent=parser.compile_filter(tokens[5]))
        else:
            raise template.TemplateSyntaxError(
                "%r tag takes 2 to 5 arguments" % (tokens[0], ))
    def handle_token(cls, parser, token):
        tokens = token.contents.split()
        if tokens[1] != "for":
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        extra_kw = {}
        if tokens[-1] in ("flat", "root_only"):
            extra_kw[str(tokens.pop())] = True

        if len(tokens) == 3:
            # {% render_comment_list for obj %}
            return cls(object_expr=parser.compile_filter(tokens[2]), **extra_kw)
        elif len(tokens) == 4:
            # {% render_comment_list for app.models pk %}
            return cls(
                ctype=BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr=parser.compile_filter(tokens[3]),
                **extra_kw
            )
        else:
            raise template.TemplateSyntaxError("%r tag takes either 2 or 3 arguments" % (tokens[0],))
Example #9
0
    def handle_token(cls, parser, token):
        """
        Class method to parse render_comment_form and return a Node.
        """
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError(
                "Second argument in %r tag must be 'for'" % tokens[0])

        if len(tokens) == 3:
            # {% render_comment_form for obj %}
            return cls(object_expr=parser.compile_filter(tokens[2]))
        elif len(tokens) == 4:
            # {% render_comment_form for app.model object_pk %}
            return cls(ctype=BaseCommentNode.lookup_content_type(
                tokens[2], tokens[0]),
                       object_pk_expr=parser.compile_filter(tokens[3]))
        elif len(tokens) == 5:
            # {% render_comment_form for obj with parent_id %}
            if tokens[-2] != u'with':
                raise template.TemplateSyntaxError(
                    "%r tag must have 'with' as the last but one argument" %
                    (tokens[0], ))
            return cls(object_expr=parser.compile_filter(tokens[2]),
                       parent=parser.compile_filter(tokens[4]))
        elif len(tokens) == 6:
            # {% render_comment_form for app.model object_pk with parent_id %}
            if tokens[-2] != u'with':
                raise template.TemplateSyntaxError(
                    "%r tag must have 'with' as the last but one argument" %
                    (tokens[0], ))
            return cls(ctype=BaseThreadedCommentNode.lookup_content_type(
                tokens[2], tokens[0]),
                       object_pk_expr=parser.compile_filter(tokens[3]),
                       parent=parser.compile_filter(tokens[5]))
        else:
            raise template.TemplateSyntaxError(
                "%r tag takes 2 to 5 arguments" % (tokens[0], ))
Example #10
0
    def handle_token(cls, parser, token):
        tokens = token.contents.split()
        if tokens[1] != 'for':
            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

        extra_kw = {}
        if tokens[-1] in ('flat', 'root_only'):
            extra_kw[str(tokens.pop())] = True

        if len(tokens) == 3:
            # {% render_comment_list for obj %}
            return cls(
                object_expr=parser.compile_filter(tokens[2]),
                **extra_kw
            )
        elif len(tokens) == 4:
            # {% render_comment_list for app.models pk %}
            return cls(
                ctype = BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
                object_pk_expr = parser.compile_filter(tokens[3]),
                **extra_kw
            )
        else:
            raise template.TemplateSyntaxError("%r tag takes either 2 or 3 arguments" % (tokens[0],))
Example #11
0
    def handle_token(cls, parser, token):
        """
            Class method to parse get_comment_list/count/form and return a Node.

            Forked from django_comments.templatetags. with_parent,
            root-only concepts borrowed from django-threadedcomments.
        """
        tokens = token.contents.split()

        with_parent = None
        extra_kw = {}
        extra_possible_kw = ('root_only', 'flat', 'reverse',
                             'sort=mostcommented', 'sort=mostrecentreplies')
        for dummy in extra_possible_kw:
            if tokens[-1] in extra_possible_kw:
                split = str(tokens.pop()).split('=')
                key = split[0]
                extra_kw[key] = len(split) > 1 and split[1] or True

        if tokens[1] != 'for':
            raise template.TemplateSyntaxError(
                "Second argument in %r tag must be 'for'" % tokens[0])

        # {% get_whatever for obj as varname %}
        # {% get_whatever for obj as varname with parent %}
        if len(tokens) == 5 or len(tokens) == 7:
            if tokens[3] != 'as':
                raise template.TemplateSyntaxError(
                    "Third argument in %r must be 'as'" % tokens[0])
            if len(tokens) == 7:
                if tokens[5] != 'with':
                    raise template.TemplateSyntaxError(
                        "When 6 arguments are given, fifth argument in %r must be 'with' followed by the parent commment wanted"
                        % tokens[0])
                with_parent = tokens[6]
            return cls(object_expr=parser.compile_filter(tokens[2]),
                       as_varname=tokens[4],
                       with_parent=with_parent,
                       **extra_kw)

        # {% get_whatever for app.model pk as varname %}
        # {% get_whatever for app.model pk as varname with parent %}
        elif len(tokens) == 6 or len(tokens) == 8:
            if tokens[4] != 'as':
                raise template.TemplateSyntaxError(
                    "Fourth argument in %r must be 'as'" % tokens[0])
            if len(tokens) == 8:
                if tokens[6] != 'with':
                    raise template.TemplateSyntaxError(
                        "When 6 arguments are given, fifth argument in %r must be 'with' followed by the parent commment wanted"
                        % tokens[0])
                with_parent = tokens[7]
            return cls(ctype=BaseCommentNode.lookup_content_type(
                tokens[2], tokens[0]),
                       object_pk_expr=parser.compile_filter(tokens[3]),
                       as_varname=tokens[5],
                       with_parent=with_parent,
                       **extra_kw)

        else:
            raise template.TemplateSyntaxError(
                "%r tag requires 4, 5, 6 or 7 arguments" % tokens[0])