Exemple #1
0
def token_kwargs_better(bits, parser, support_legacy=False):
    """
    Parse token keyword arguments and return a dictionary of the arguments
    retrieved from the ``bits`` token list.

    `bits` is a list containing the remainder of the token (split by spaces)
    that is to be checked for arguments. Valid arguments are removed from this
    list.

    `support_legacy` - if True, the legacy format ``1 as foo`` is accepted.
    Otherwise, only the standard ``foo=1`` format is allowed.

    There is no requirement for all remaining token ``bits`` to be keyword
    arguments, so return the dictionary as soon as an invalid argument format
    is reached.

    NEW:
    **kwargs

    """
    if not bits:
        return {}
    match = kwarg_re.match(bits[0])
    kwarg_format = match and match.group(1)
    kwargs_match = kwargs_re.match(bits[0])
    kwargs_format = kwargs_match and kwargs_match.group(1)
    if not kwarg_format and not kwargs_format:
        if not support_legacy:
            return {}
        if len(bits) < 3 or bits[1] != 'as':
            return {}
        # end if
    # end if

    kwargs = {}
    while bits:
        if kwarg_format or kwargs_format:
            match = kwarg_re.match(bits[0])
            kwargs_match = kwargs_re.match(bits[0])
            if not match or not match.group(1):
                return kwargs
            key, value = match.groups()
            del bits[:1]
        else:
            if len(bits) < 3 or bits[1] != 'as':
                return kwargs
            key, value = bits[2], bits[0]
            del bits[:3]
        kwargs[key] = parser.compile_filter(value)
        if bits and not kwarg_format:
            if bits[0] != 'and':
                return kwargs
            del bits[:1]
    return kwargs
Exemple #2
0
def riff_url(parser, token):
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'{0}' takes at least two arguments: a riff "
                                  "and a view path.".format(bits[0]))

    riff = parser.compile_filter(bits[1])
    view_name = parser.compile_filter(bits[2])

    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if bits:
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to {0} tag"
                                          "".format(bits[0]))
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return RiffURLNode(riff, view_name, args, kwargs, asvar)
Exemple #3
0
def backend_url(parser, token):
    from django.template.base import kwarg_re
    from django.template.defaulttags import URLNode
    from django_backend import state

    bits = token.split_contents()
    if len(bits) < 2:
        raise template.TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise template.TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    kwargs.setdefault('site', template.Variable('"%s"' % Site.objects.get_current().pk))
    kwargs.setdefault('language', template.Variable('"%s"' % state.language.active))

    return URLNode(viewname, args, kwargs, asvar)
Exemple #4
0
def do_thumbnail(parser, token):
    bits = token.split_contents()
    tag_name = bits.pop(0)

    try:
        if bits.pop(-2) != 'as':
            raise ValueError()
        var_name = bits.pop()
        file_instance = parser.compile_filter(bits.pop(0))
    except ValueError:
        raise template.TemplateSyntaxError(
            '{0} tag syntax is "file [params] as varname".'.format(tag_name)
        )

    params = []
    while bits:
        match = kwarg_re.match(bits.pop(0))
        if match and match.group(1):
            k, v = match.groups()
            params.append((k, parser.compile_filter(v)))
        else:
            params.append((None, parser.compile_filter(match.group(2))))

    nodelist = parser.parse(('endthumbnail',))
    parser.delete_first_token()
    return ThumbnailNode(nodelist, tag_name, file_instance, var_name, params)
def navitem(parser, token):
    bits = token.split_contents()
    template_tag = bits[0]

    if len(bits) < 3:
        raise TemplateSyntaxError("%r tag requires at least two argument" % template_tag)

    try:
        label = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ".  The label argument has be be in single quotes.",)
        raise

    try:
        viewname = parser.compile_filter(bits[2])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ".  The url argument has be be in single quotes, like the url tag in Django 1.5.",)
        raise

    bits = bits[3:]

    args = []
    kwargs = {}
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return NavItemNode(label, viewname, args, kwargs)
Exemple #6
0
def get_url_args(parser, token):
    """Helper function, returns the arguments necessary to construct a URL node.
    This code is copied directly from `url` in django/template/defaulttags.py,
    except that instead of constructing a URL node with the identified
    parameters, it simply returns the parameters in a tuple.
    """

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument, the name of a url()." % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return viewname, args, kwargs, asvar
Exemple #7
0
def navitem(parser, token):
    bits = token.split_contents()
    template_tag = bits[0]

    if len(bits) < 3:
        raise TemplateSyntaxError("%r tag requires at least two argument" % template_tag)

    try:
        label = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ".  The label argument has be be in single quotes.",)
        raise

    try:
        viewname = parser.compile_filter(bits[2])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ".  The url argument has be be in single quotes, like the url tag in Django 1.5.",)
        raise

    bits = bits[3:]

    args = []
    kwargs = {}
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return NavItemNode(label, viewname, args, kwargs)
def urlnext(parser, token):
    """
    {% url %} copied from Django 1.7.
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise template.TemplateSyntaxError("'%s' takes at least one argument"
                                           " (path to a view)" % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == "as":
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits) > 0:
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise template.TemplateSyntaxError(
                    "Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNextNode(viewname, args, kwargs, asvar)
Exemple #9
0
def parse_token_contents(parser, token):
    bits = token.split_contents()
    tag = bits.pop(0)
    args = []
    kwargs = {}
    asvar = None
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError(
                    'Malformed arguments to tag "{}"'.format(tag))
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))
    return {
        'tag': tag,
        'args': args,
        'kwargs': kwargs,
        'asvar': asvar,
    }
Exemple #10
0
def eventurl(parser, token):
    """
    Similar to {% url %} in the same way that eventreverse() is similar to reverse().

    Takes an event or organizer object, an url name and optional keyword arguments
    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'%s' takes at least two arguments, an event and the name of a url()." % bits[0])
    viewname = parser.compile_filter(bits[2])
    event = parser.compile_filter(bits[1])
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == "as":
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to eventurl tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                raise TemplateSyntaxError("Event urls only have keyword arguments.")

    return EventURLNode(event, viewname, kwargs, asvar)
Exemple #11
0
def answer(parser, token):
    bits = token.split_contents()

    if len(bits) < 2:
        raise TemplateSyntaxError("%r tag requires at least one argument" \
                                      % bits[0])

    answer_code = parser.compile_filter(bits[1])
    answer_code_string = bits[1]

    kwargs = {} 
    bits = bits[2:]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to %r tag" \
                                              % str(bits[0]))
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)


    return AnswerNode(answer_code, answer_code_string, kwargs)
def urlnext(parser, token):
    """
    {% url %} copied from Django 1.7.
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise template.TemplateSyntaxError(
            "'%s' takes at least one argument"
            " (path to a view)" % bits[0]
        )
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == "as":
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise template.TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNextNode(viewname, args, kwargs, asvar)
Exemple #13
0
def parse_tag(token, parser):
    """Parses template tag for name, arguments and keyword arguments.

    :param token: Template token containing all the tag contents
    :type token: django.template.base.Token
    :param parser: Template parser
    :type parser: django.template.base.Parser
    :return: Tuple with tag name, arguments and keyword arguments
    :rtype: tuple

    """
    # Split the tag content into words, respecting quoted strings.
    bits = token.split_contents()

    # Pull out the tag name.
    tag_name = bits.pop(0)

    # Parse the rest of the args, and build FilterExpressions from them so that
    # we can evaluate them later.
    args = []
    kwargs = {}
    for bit in bits:
        # Is this a kwarg or an arg?
        match = kwarg_re.match(bit)
        kwarg_format = match and match.group(1)
        if kwarg_format:
            key, value = match.groups()
            kwargs[key] = FilterExpression(value, parser)
        else:
            args.append(FilterExpression(bit, parser))

    return (tag_name, args, kwargs)
Exemple #14
0
def medoco_url(parser, token):
    """
    Returns an URL relative to the medoco dash for hash based dash navigation
    """

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = bits[1]
    args = []
    kwargs = {}
    bits = bits[2:]

    for bit in bits:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError(
                "Malformed arguments to medoco url tag")
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            args.append(parser.compile_filter(value))

    return MedocoURLNode(viewname, args, kwargs)
def url(parser, token):
    """
    (todo) find a better solution
    Copied from django's url tag and changed which kind of Node it
    returns
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return SchemaURLNode(viewname, args, kwargs, asvar)
Exemple #16
0
def get_url_args(parser, token):
    """Helper function, returns the arguments necessary to construct a URL node.
    This code is copied directly from `url` in django/template/defaulttags.py,
    except that instead of constructing a URL node with the identified
    parameters, it simply returns the parameters in a tuple.
    """

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError(
            "'%s' takes at least one argument, the name of a url()." % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return viewname, args, kwargs, asvar
def has_permission_to_url(parser, token):
    bits = list(token.split_contents())
    if len(bits) < 2:
        raise TemplateSyntaxError(
            '"has_permission_to_url" takes minimal one argument')

    end_tag = 'end' + bits[0]

    kwargs = {}
    for bit in bits[2:]:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError(
                'Malformed arguments to has_permission_to_url tag')
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            raise TemplateSyntaxError(
                'Malformed arguments to has_permission_to_url tag')

    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end_tag, ))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return PermissionURLNode(parser.compile_filter(bits[1]), kwargs,
                             nodelist_true, nodelist_false)
def url_aff(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    try:
        viewname = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ". "
                "The syntax of 'url' changed in Django 1.5, see the docs."),
        raise
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return AffiliateURLNode(viewname, args, kwargs, asvar)
def cross_url(parser, token):
    """
    Resolves an URL of a remote site in a template.
    Usage::
        {% load cross_site_urls %}
        {% cross_url 'site-identifier' "url_name" arg1 arg2' %}
        or
        {% cross_url "url_name" name1=value1 name2=value2 %}
    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'%s' takes at least two argument, a site name and the name of a url()." % bits[0])
    site_identifier = parser.compile_filter(bits[1])
    viewname = parser.compile_filter(bits[2])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))
    return URLNode(site_identifier, viewname, args, kwargs, asvar)
def url(parser, token):
    """
    (todo) find a better solution
    Copied from django's url tag and changed which kind of Node it
    returns
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return SchemaURLNode(viewname, args, kwargs, asvar)
Exemple #21
0
def get_node_extra_arguments(parser, bits, tag_name, max_args):
    args = []
    kwargs = {}
    asvar = None
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        if len(bits) <= max_args:
            for bit in bits:
                match = kwarg_re.match(bit)
                if not match:
                    err_msg = "Malformed arguments in '%s'" % tag_name
                    raise TemplateSyntaxError(err_msg)
                name, val = match.groups()
                if name:
                    kwargs[name] = parser.compile_filter(val)
                else:
                    args.append(parser.compile_filter(val))
        else:
            err_msg = "'%s' tag takes at most %d extra arguments." \
                % (tag_name, max_args)
            raise TemplateSyntaxError(err_msg)

    return [args, kwargs, asvar]
Exemple #22
0
def expr(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("%r tag requires at least one argument" \
                                      % str(bits[0]))
    expression = parser.compile_filter(bits[1])

    asvar = None
    kwargs = {}
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to %r tag" \
                                              % str(bits[0]))
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)

    
    return ExprNode(expression, asvar, kwargs)
def cross_url(parser, token):
    """
    Resolves an URL of a remote site in a template.
    Usage::
        {% load cross_site_urls %}
        {% cross_url 'site-identifier' "url_name" arg1 arg2' %}
        or
        {% cross_url "url_name" name1=value1 name2=value2 %}
    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError(
            "'%s' takes at least two argument, a site name and the name of a url()."
            % bits[0])
    site_identifier = parser.compile_filter(bits[1])
    viewname = parser.compile_filter(bits[2])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))
    return URLNode(site_identifier, viewname, args, kwargs, asvar)
Exemple #24
0
def parse_token_contents(parser, token):
    """
    Parse template tag contents
    """
    bits = token.split_contents()
    tag = bits.pop(0)
    args = []
    kwargs = {}
    asvar = None
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError(
                    'Malformed arguments to tag "{}"'.format(tag))
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))
    return {
        'tag': tag,
        'args': args,
        'kwargs': kwargs,
        'asvar': asvar,
    }
Exemple #25
0
def generate_embed(parser, token):
    """
    Includes a template suitable for generating embed code for the given url.
    Additional kwargs may be supplied, which will be passed on as parameters
    for embedding - for example, autoplay values.

        {% generate_embed "http://www.youtube.com/watch?v=J_DV9b0x7v4" autoplay=1 %}

    """
    bits = token.split_contents()
    tag_name = bits[0]
    if len(bits) < 2:
        raise TemplateSyntaxError(
            '{0} tag requires at least 1 argument'.format(tag_name))

    url = parser.compile_filter(bits[1])
    kwargs = {}

    bits = bits[2:]

    for bit in bits:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError(
                    'Malformed arguments to {0} tag.'.format(tag_name))
        key, value = match.groups()
        if key:
            kwargs[key] = parser.compile_filter(value)
        else:
            raise TemplateSyntaxError('{0} tag only takes one positional '
                                      'argument.'.format(tag_name))
    return EmbedGeneratorNode(url, kwargs)
Exemple #26
0
def acc_url(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    try:
        viewname = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ". "
                                  "The syntax of 'url' changed in Django 1.5, see the docs."),
        raise
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(viewname, args, kwargs, asvar)
Exemple #27
0
def riff_url(parser, token):
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'{0}' takes at least two arguments: a riff "
                                  "and a view path.".format(bits[0]))

    riff = parser.compile_filter(bits[1])
    view_name = parser.compile_filter(bits[2])

    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if bits:
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to {0} tag"
                                          "".format(bits[0]))
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return RiffURLNode(riff, view_name, args, kwargs, asvar)
Exemple #28
0
def route_url(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError(
            "'%s' takes at least one argument, a URL pattern name." % bits[0])
    route_name = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    for bit in bits:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError("Malformed arguments to url tag")
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            args.append(parser.compile_filter(value))

    return RouteURLNode(route_name, args, kwargs, asvar)
Exemple #29
0
def eventurl(parser, token):
    """
    Similar to {% url %} in the same way that eventreverse() is similar to reverse().

    Takes an event or organizer object, an url name and optional keyword arguments
    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError(
            "'%s' takes at least one argument, an event and the name of a url()."
            % bits[0])
    viewname = parser.compile_filter(bits[2])
    event = parser.compile_filter(bits[1])
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError(
                    "Malformed arguments to eventurl tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                raise TemplateSyntaxError(
                    'Event urls only have keyword arguments.')

    return EventURLNode(event, viewname, kwargs, asvar)
Exemple #30
0
def get_available_transitions(parser, token):
    """
    Usage:

        {% get_available_transitions <object> <field_name> [<args>] [<kwargs>] as state_transitions %}

    Example:

        {% get_available_transitions purchase "state" as state_transitions %}
        {% for new_state, transition_function in state_transitions %}
            <button class="btn" type="submit" data-action="{{ transition_function.func_name }}">
                Transition to {{ new_state }}
            </button>
        {% endfor %}

    If the transition and condition functions take arguments you can pass these as well:

        {% get_available_transitions purchase "state" request.user as state_transitions %}

    In fact if the transition and condition functions don't take any arguments you can just call the
    get_available_FIELD_transitions method directly:

        {% for new_state, transition_function in purchase.get_available_state_transitions %}

    This template tag is necessary only if there are any arguments to pass to the transition
    and condition functions.
    """

    bits = token.split_contents()
    tag_name = bits[0]
    AS, asvar = bits[-2:]

    if len(bits) < 5 or AS != 'as':
        raise TemplateSyntaxError("'%s' takes at least 4 arguments like this:\n"
                                  "<object> <field_name> [args] [kwargs] as <as_var>"
                                  % tag_name)

    obj = parser.compile_filter(bits[1])
    field_name = parser.compile_filter(bits[2])
    bits = bits[3:-2]

    args = []
    kwargs = {}

    for bit in bits:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError("Malformed arguments to '%s' tag" % tag_name)
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            args.append(parser.compile_filter(value))

    return GetAvailableTransitionsNode(obj, field_name, args, kwargs, asvar)
Exemple #31
0
def interpolate(parser, tokens):
    ''' make django.template.Variable's from the input tokens '''
    args = []
    kwargs = {}
    for token in tokens:
        match = kwarg_re.match(token)
        if match:
            kwargs[match.group(1)] = FilterExpression(match.group(2), parser)
        else:
            args.append(FilterExpression(token, parser))

    return (args, kwargs)
def parse_tag_args(parser, parts):
    args = []
    kwargs = {}
    for part in parts:
        match = kwarg_re.match(part)
        kwarg_format = match and match.group(1)
        if kwarg_format:
            key, value = match.groups()
            kwargs[key] = FilterExpression(value, parser)
        else:
            args.append(FilterExpression(part, parser))

    return args, kwargs
Exemple #33
0
def package_url(parser, token):
    r"""
    Return an absolute URL matching the given view with its parameters.
    This is a way to define links that aren't tied to a particular URL
    configuration::
        {% url "url_name" arg1 arg2 %}
        or
        {% url "url_name" name1=value1 name2=value2 %}
    The first argument is a URL pattern name. Other arguments are
    space-separated values that will be filled in place of positional and
    keyword arguments in the URL. Don't mix positional and keyword arguments.
    All arguments for the URL must be present.
    For example, if you have a view ``app_name.views.client_details`` taking
    the client's id and the corresponding line in a URLconf looks like this::
        path('client/<int:id>/', views.client_details, name='client-detail-view')
    and this app's URLconf is included into the project's URLconf under some
    path::
        path('clients/', include('app_name.urls'))
    then in a template you can create a link for a certain client like this::
        {% url "client-detail-view" client.id %}
    The URL will look like ``/clients/client/123/``.
    The first argument may also be the name of a template variable that will be
    evaluated to obtain the view name or the URL name, e.g.::
        {% with url_name="client-detail-view" %}
        {% url url_name client.id %}
        {% endwith %}
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError(
            "'%s' takes at least one argument, a URL pattern name." % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    for bit in bits:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError("Malformed arguments to url tag")
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            args.append(parser.compile_filter(value))

    return PackageURLNode(viewname, args, kwargs, asvar)
Exemple #34
0
def parse_args_kwargs(parser, bits):
    # Parse the rest of the args, and build FilterExpressions from them so that
    # we can evaluate them later.
    args = []
    kwargs = {}
    for bit in bits:
        # Is this a kwarg or an arg?
        match = kwarg_re.match(bit)
        kwarg_format = match and match.group(1)
        if kwarg_format:
            key, value = match.groups()
            kwargs[key] = FilterExpression(value, parser)
        else:
            args.append(FilterExpression(bit, parser))
    return args, kwargs
def parse_tag(token, parser):
    bits = token.split_contents()
    tag_name = bits.pop(0)
    args = []
    kwargs = {}
    for bit in bits:
        match = kwarg_re.match(bit)
        kwarg_format = match and match.group(1)
        if kwarg_format:
            key, value = match.groups()
            kwargs[key] = FilterExpression(value, parser)
        else:
            args.append(FilterExpression(bit, parser))

    return (tag_name, args, kwargs)
Exemple #36
0
def reverse_app(parser, token):
    """
    Reverse app URLs, preferring the active language.

    Usage::

        {% load feincms3 %}
        {% reverse_app 'blog' 'detail' [args] [kw=args] [fallback='/'] %}

    ``namespaces`` can either be a list or a comma-separated list of
    namespaces. ``NoReverseMatch`` exceptions can be avoided by providing a
    ``fallback`` as a keyword argument or by saving the result in a variable,
    similar to ``{% url 'view' as url %}`` does::

        {% reverse_app 'newsletter' 'subscribe-form' fallback='/newsletter/' %}

    Or::

        {% reverse_app 'extranet' 'login' as login_url %}
    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError(
            "'reverse_app' takes at least two arguments, a namespace and"
            " a URL pattern name.")
    namespaces = parser.compile_filter(bits[1])
    viewname = parser.compile_filter(bits[2])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == "as":
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError(
                    "Malformed arguments to reverse_app tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return ReverseAppNode(namespaces, viewname, args, kwargs, asvar)
Exemple #37
0
    def parse_arguments(cls, parser, tag_name, bits):
        args = []
        kwargs = {}
        for bit in bits:

            match = KWARG_RE.match(bit)
            if not match:
                msg = '"{0}" is not a valid argument.'
                raise TemplateSyntaxError(msg.format(bit))

            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

        return (args, kwargs)
Exemple #38
0
def i18nurl(parser, token):
    """
    Returns an absolute URL matching given view with its parameters in
    the good language.

        {% i18nurl "path.to.some_view" "language" arg1 arg2 %}

        or

        {% i18nurl "path.to.some_view" "language" name1=value1 name2=value2 %}

    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'%s' takes at least two argument"
                                  " (path to a view and language)" % bits[0])
    try:
        viewname = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ". The syntax of 'url' "
                    "changed in Django 1.5, see the docs."),
        raise

    language = parser.compile_filter(bits[2])

    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(viewname, language, args, kwargs, asvar)
Exemple #39
0
def i18nurl(parser, token):
    """
    Returns an absolute URL matching given view with its parameters in
    the good language.

        {% i18nurl "path.to.some_view" "language" arg1 arg2 %}

        or

        {% i18nurl "path.to.some_view" "language" name1=value1 name2=value2 %}

    """
    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'%s' takes at least two argument"
                                  " (path to a view and language)" % bits[0])
    try:
        viewname = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ". The syntax of 'url' "
                    "changed in Django 1.5, see the docs."),
        raise

    language = parser.compile_filter(bits[2])

    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(viewname, language, args, kwargs, asvar)
Exemple #40
0
def murl(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname_str = bits[1]
    viewname = parser.compile_filter(viewname_str)
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    request = current_request()
    site_id_val = parser.compile_filter(str(request.site_id))
    if hasattr(request,'site_id'):
        if args is None and kwargs is None:
            kwargs = {'site_id': site_id_val}
        elif args is None and kwargs is not None:
            kwargs['site_id'] = site_id_val
        elif args is not None and kwargs is None:
            args = [site_id_val]+args
        elif len(args) == 0:
            kwargs['site_id'] = site_id_val
        elif len(kwargs) == 0:
            args = [site_id_val]+list(args)
        else:
            kwargs['site_id'] = site_id_val

    return URLNode(viewname, args, kwargs, asvar)
Exemple #41
0
def urlwithgetparam(parser, token):
    bits = token.split_contents()
    viewname = bits[1]
    args = []
    kwargs = {}
    bits = bits[2:]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise Exception("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return MyUrlNode(viewname, args, kwargs, legacy_view_name=True)
Exemple #42
0
def urlwithgetparam(parser, token):
    bits = token.split_contents()
    viewname = bits[1]
    args = []
    kwargs = {}
    bits = bits[2:]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise Exception("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return MyUrlNode(viewname, args, kwargs, legacy_view_name=True)
Exemple #43
0
def dynamictext(parser, token):

    bits = token.split_contents()

    kwargs = {}
    bits = bits[1:]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to %s tag" % bits[0])
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
  

    nodelist = parser.parse(('enddynamictext',))
    parser.delete_first_token()
    return DynamicTextNode(kwargs, nodelist)
Exemple #44
0
def parse_token_contents(parser, token):
    """Parse template tag contents."""
    bits = token.split_contents()
    tag = bits.pop(0)
    args = []
    kwargs = {}
    asvar = None
    if len(bits) >= 2 and bits[-2] == "as":
        asvar = bits[-1]
        bits = bits[:-2]
    for bit in bits:
        match = kwarg_re.match(bit)
        if not match:
            raise TemplateSyntaxError('Malformed arguments to tag "{}"'.format(tag))
        name, value = match.groups()
        if name:
            kwargs[name] = parser.compile_filter(value)
        else:
            args.append(parser.compile_filter(value))
    return {"tag": tag, "args": args, "kwargs": kwargs, "asvar": asvar}
Exemple #45
0
    def parse_arguments(cls, parser, tag_name, bits):
        args = []
        kwargs = {}
        if bits:

            if len(bits) == 1 or bits[0] != 'with':
                raise TagSyntaxError(cls, tag_name)

            for bit in bits[1:]:
                match = KWARG_RE.match(bit)
                if not match:
                    msg = '"{0}" is not a valid argument.'
                    raise TemplateSyntaxError(msg.format(bit))

                name, value = match.groups()
                if name:
                    kwargs[name] = parser.compile_filter(value)
                else:
                    kwargs[value] = Variable(value)

        return (args, kwargs)
Exemple #46
0
def query(parser, token):
    bits = token.split_contents()
    args = []
    asvar = None
    bits = bits[1:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                args.append({name: parser.compile_filter(value)})
            else:
                args.append(parser.compile_filter(value))

    return QueryNode(args, asvar)
Exemple #47
0
def url(parser, token):
    """
    Overrides Django's built-in url template tag to accomodate for custom
    url patterns (i.e. for Main Calendar views.)

    Code is copied directly from django.template.defaulttags url function
    but replaces the return value.
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    try:
        viewname = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ". "
                "The syntax of 'url' changed in Django 1.5, see the docs."),
        raise
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return EventsURLNode(viewname, args, kwargs, asvar)
Exemple #48
0
def parse_tag(token, parser):
    """
    Generic template tag parser.

    Returns a three-tuple: (tag_name, args, kwargs)

    tag_name is a string, the name of the tag.

    args is a list of FilterExpressions, from all the arguments that didn't look like kwargs,
    in the order they occurred, including any that were mingled amongst kwargs.

    kwargs is a dictionary mapping kwarg names to FilterExpressions, for all the arguments that
    looked like kwargs, including any that were mingled amongst args.

    (At rendering time, a FilterExpression f can be evaluated by calling f.resolve(context).)
    """
    # Split the tag content into words, respecting quoted strings.
    bits = token.split_contents()

    # Pull out the tag name.
    tag_name = bits.pop(0)

    # Parse the rest of the args, and build FilterExpressions from them so that
    # we can evaluate them later.
    args = []
    kwargs = {}
    for bit in bits:
        # Is this a kwarg or an arg?
        match = kwarg_re.match(bit)
        kwarg_format = match and match.group(1)
        if kwarg_format:
            key, value = match.groups()
            kwargs[key] = FilterExpression(value, parser)
        else:
            args.append(FilterExpression(bit, parser))

    return (tag_name, args, kwargs)
Exemple #49
0
def i18n_url(parser, token):
    """
    Returns an absolute URL matching given view with its parameters.

    This is a way to define links that aren't tied to a particular URL
    configuration::

        {% i18n_url language path.to.some_view arg1 arg2 %}

        or

        {% i18n_url language path.to.some_view name1=value1 name2=value2 %}

    The first argument is the language code.

    The second argument is a path to a view. It can be an absolute python path
    or just ``app_name.view_name`` without the project name if the view is
    located inside the project.  Other arguments are comma-separated values
    that will be filled in place of positional and keyword arguments in the
    URL. All arguments for the URL should be present.

    For example if you have a view ``app_name.client`` taking client's id and
    the corresponding line in a URLconf looks like this::

        ('^client/(\d+)/$', 'app_name.client')

    and this app's URLconf is included into the project's URLconf under some
    path::

        ('^clients/', include('project_name.app_name.urls'))

    then in a template you can create a link for a certain client like this::

        {% url app_name.client client.id %}

    The URL will look like ``/clients/client/123/``.
    """

    import warnings
    warnings.warn('The syntax for the url template tag is changing. Load the `url` tag from the `future` tag library to start using the new behavior.',
                  category=DeprecationWarning)

    bits = token.split_contents()
    if len(bits) < 3:
        raise TemplateSyntaxError("'%s' takes at least two arguments"
                                  " (language) (path to a view)" % bits[0])

    language = parser.compile_filter(bits[1])
    viewname = bits[2]
    args = []
    kwargs = {}
    asvar = None
    bits = bits[3:]
    if len(bits) >= 3 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    # Now all the bits are parsed into new format,
    # process them as template vars
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to i18n_url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return I18NURLNode(language, viewname, args, kwargs, asvar,
                       legacy_view_name=True)
Exemple #50
0
def url(parser, token):
    """
    Returns an absolute URL matching given view with its parameters.

    This is a way to define links that aren't tied to a particular URL
    configuration::

        {% url "path.to.some_view" arg1 arg2 %}

        or

        {% url "path.to.some_view" name1=value1 name2=value2 %}

    The first argument is a path to a view. It can be an absolute Python path
    or just ``app_name.view_name`` without the project name if the view is
    located inside the project.

    Other arguments are space-separated values that will be filled in place of
    positional and keyword arguments in the URL. Don't mix positional and
    keyword arguments.

    All arguments for the URL should be present.

    For example if you have a view ``app_name.client`` taking client's id and
    the corresponding line in a URLconf looks like this::

        ('^client/(\d+)/$', 'app_name.client')

    and this app's URLconf is included into the project's URLconf under some
    path::

        ('^clients/', include('project_name.app_name.urls'))

    then in a template you can create a link for a certain client like this::

        {% url "app_name.client" client.id %}

    The URL will look like ``/clients/client/123/``.

    The first argument can also be a named URL instead of the Python path to
    the view callable. For example if the URLconf entry looks like this::

        url('^client/(\d+)/$', name='client-detail-view')

    then in the template you can use::

        {% url "client-detail-view" client.id %}

    There is even another possible value type for the first argument. It can be
    the name of a template variable that will be evaluated to obtain the view
    name or the URL name, e.g.::

        {% with view_path="app_name.client" %}
        {% url view_path client.id %}
        {% endwith %}

        or,

        {% with url_name="client-detail-view" %}
        {% url url_name client.id %}
        {% endwith %}

    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = parser.compile_filter(bits[1])
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(viewname, args, kwargs, asvar)
Exemple #51
0
def flowurl(parser, token):
    """
    Return a flow url.

    Usage::

        {% flowurl ref [urlname] [user=]  [ns=] [ns_map=] [as varname]%}

    Examples::

        {% flowurl 'app_label/FlowCls' 'index' %}
        {% flowurl flow_class 'index' as index_url %}
        {% flowurl process 'index' %}
        {% flowurl process 'detail' %}
        {% flowurl task 'assign' user=request.user %}
        {% flowurl task user=request.user %}

    Examples to use links outside of flow views::

        {% flowurl task 'detail' ns='viewflow:helloworld' %}
        {% flowurl task 'detail' ns=request.resolver_match.namespace ns_map=view.ns_map %}

    """
    def geturl(ns, ref, url_name=None, user=None, ns_map=None):
        if isinstance(ref, Flow):
            namespace = get_flow_namespace(ref, ns, ns_map)
            url_ref = '{}:{}'.format(namespace, url_name if url_name else 'index')
            return reverse(url_ref)
        elif isinstance(ref, AbstractProcess):
            namespace = get_flow_namespace(ref.flow_class, ns, ns_map)
            kwargs, url_ref = {}, '{}:{}'.format(namespace, url_name if url_name else 'index')
            if url_name in ['detail', 'action_cancel']:
                kwargs['process_pk'] = ref.pk
            return reverse(url_ref, kwargs=kwargs)
        elif isinstance(ref, AbstractTask):
            namespace = get_flow_namespace(ref.flow_task.flow_class, ns, ns_map)
            return ref.flow_task.get_task_url(
                ref, url_type=url_name if url_name else 'guess',
                user=user, namespace=namespace)
        else:
            try:
                app_label, flow_class_path = ref.split('/')
            except ValueError:
                raise TemplateSyntaxError(
                    "Flow reference string should  looks like 'app_label/FlowCls' but '{}'".format(ref))

            app_package = get_app_package(app_label)
            if app_package is None:
                raise TemplateSyntaxError("{} app not found".format(app_label))

            flow_class = import_string('{}.flows.{}'.format(app_package, flow_class_path))
            namespace = get_flow_namespace(flow_class, ns, ns_map)
            url_ref = '{}:{}'.format(namespace, url_name if url_name else 'index')
            return reverse(url_ref)

    class URLNode(Node):
        def __init__(self, args, kwargs, target_var):
            self.args = args
            self.kwargs = kwargs
            self.target_var = target_var

        def render(self, context):
            request = context['request']  # TODO Check that request template context installed

            resolved_args = [arg.resolve(context) for arg in self.args]
            resolved_kwargs = {k: v.resolve(context) for k, v in self.kwargs.items()}

            base_namespace = resolved_kwargs.pop('ns', None)
            ns_map = resolved_kwargs.get('ns_map', None)

            if base_namespace is None and ns_map is None:
                base_namespace = request.resolver_match.namespace

            url = geturl(base_namespace, *resolved_args, **resolved_kwargs)
            if self.target_var:
                context[self.target_var] = url
                return ''
            else:
                return url

    bits = token.split_contents()[1:]

    args = []
    kwargs = {}
    target_var = None
    if bits[-2] == 'as':
        target_var = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(args, kwargs, target_var)
Exemple #52
0
def url(parser, token):
    """
    Returns an absolute URL matching given view with its parameters.

    This is a way to define links that aren't tied to a particular URL
    configuration::

        {% url path.to.some_view arg1 arg2 %}

        or

        {% url path.to.some_view name1=value1 name2=value2 %}

    The first argument is a path to a view. It can be an absolute python path
    or just ``app_name.view_name`` without the project name if the view is
    located inside the project.  Other arguments are comma-separated values
    that will be filled in place of positional and keyword arguments in the
    URL. All arguments for the URL should be present.

    For example if you have a view ``app_name.client`` taking client's id and
    the corresponding line in a URLconf looks like this::

        ('^client/(\d+)/$', 'app_name.client')

    and this app's URLconf is included into the project's URLconf under some
    path::

        ('^clients/', include('project_name.app_name.urls'))

    then in a template you can create a link for a certain client like this::

        {% url app_name.client client.id %}

    The URL will look like ``/clients/client/123/``.
    """

    import warnings
    warnings.warn('The syntax for the url template tag is changing. Load the `url` tag from the `future` tag library to start using the new behavior.',
                  category=DeprecationWarning)

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = bits[1]
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    # Backwards compatibility: check for the old comma separated format
    # {% url urlname arg1,arg2 %}
    # Initial check - that the first space separated bit has a comma in it
    if bits and ',' in bits[0]:
        check_old_format = True
        # In order to *really* be old format, there must be a comma
        # in *every* space separated bit, except the last.
        for bit in bits[1:-1]:
            if ',' not in bit:
                # No comma in this bit. Either the comma we found
                # in bit 1 was a false positive (e.g., comma in a string),
                # or there is a syntax problem with missing commas
                check_old_format = False
                break
    else:
        # No comma found - must be new format.
        check_old_format = False

    if check_old_format:
        # Confirm that this is old format by trying to parse the first
        # argument. An exception will be raised if the comma is
        # unexpected (i.e. outside of a static string).
        match = kwarg_re.match(bits[0])
        if match:
            value = match.groups()[1]
            try:
                parser.compile_filter(value)
            except TemplateSyntaxError:
                bits = ''.join(bits).split(',')

    # Now all the bits are parsed into new format,
    # process them as template vars
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(viewname, args, kwargs, asvar, legacy_view_name=True)
def urlx(parser, token):
    """
    Returns an absolute URL matching given view with its parameters. The
    ``urlx`` expands the Django ``url`` tag functionality in order to allow 
    partial URL handling.

    This expanded URL handling allows one to define partial URL construction,
    like in the following example. Consider the following Django URL:

        url(r'^test/(?P<id>\d+)/(?P<slug>[\w-]+)$', 'demo.views.view', name='test-url'),

	In the template (kwargs)::

        {% load partialurls %}	
		...
		{% urlx 'test-url' slug='test' as partial_url %}
        ...
        {% for element in elements %}
            <a href="{% urlx partial_url id=element.id %}">Link</a>
        {% endfor %}

    .. warning:
        The ONLY way to create or complete a partial URL is to set it as a variable, using ``as var_name``.

    .. warning:
        Like Django's ``url``, you can't mix args and kwargs on the same tag.

    .. warning:
        If you use a arg or kwarg in a initial case of a partial URL, you **must** use 
        the same kind on the following ``urlx`` tags involving it.

    """
	# Because the xyntax of the urlx tag is the same as the Django url tag,
    # the parser bit is reused.
    
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    try:
        viewname = parser.compile_filter(bits[1])
    except TemplateSyntaxError as exc:
        exc.args = (exc.args[0] + ". "
                "The syntax of 'url' changed in Django 1.5, see the docs."),
        raise
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLXNode(viewname, args, kwargs, asvar)
Exemple #54
0
def url_site(parser, token):
    """
    """
    import warnings
    warnings.warn('The syntax for the url template tag is changing. Load the `url` tag from the `future` tag library to start using the new behavior.',
                  category=DeprecationWarning)

    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    viewname = bits[1]
    args = []
    kwargs = {}
    asvar = None
    bits = bits[2:]
    if len(bits) >= 2 and bits[-2] == 'as':
        asvar = bits[-1]
        bits = bits[:-2]

    # Backwards compatibility: check for the old comma separated format
    # {% url urlname arg1,arg2 %}
    # Initial check - that the first space separated bit has a comma in it
    if bits and ',' in bits[0]:
        check_old_format = True
        # In order to *really* be old format, there must be a comma
        # in *every* space separated bit, except the last.
        for bit in bits[1:-1]:
            if ',' not in bit:
                # No comma in this bit. Either the comma we found
                # in bit 1 was a false positive (e.g., comma in a string),
                # or there is a syntax problem with missing commas
                check_old_format = False
                break
    else:
        # No comma found - must be new format.
        check_old_format = False

    if check_old_format:
        # Confirm that this is old format by trying to parse the first
        # argument. An exception will be raised if the comma is
        # unexpected (i.e. outside of a static string).
        match = kwarg_re.match(bits[0])
        if match:
            value = match.groups()[1]
            try:
                parser.compile_filter(value)
            except TemplateSyntaxError:
                bits = ''.join(bits).split(',')

    # Now all the bits are parsed into new format,
    # process them as template vars
    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to url tag")
            name, value = match.groups()
            if name:
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return URLNode(viewname, args, kwargs, asvar, legacy_view_name=True)