def toggle_one_from_querystring(context, key, value, remove_values=None):
    """
    {% toggle_one_in_querystring "label" "foo" %}
        => remove foo from labels if present, or add it if not
    {% toggle_one_in_querystring "label" "foo" remove_values=somelist %}
        => remove foo from labels if present, or add it if not, and remove
           all values from remove_values, except the the one defined as value
    """
    qs_parts = _get_qs_parts_from_context(context)
    value = _coerce_value(value)

    if value is not None:
        _set_part_as_list(qs_parts, key)
        if value in qs_parts[key]:
            qs_parts[key].remove(value)
        else:
            qs_parts[key].append(value)

    if remove_values:
        for val in remove_values:
            if val != value:
                try:
                    qs_parts[key].remove(val)
                except ValueError:
                    pass

    if not len(qs_parts[key]):
        del qs_parts[key]
    return make_querystring(qs_parts)
def remove_from_querystring(context, key, *values):
    """
    {% remove_from_querystring "labels" "foo" %}
        => remove foo from labels
    {% remove_from_querystring "labels" "foo" "bar" %}
        => remove foo and bar from labels
    {% remove_from_querystring "labels" %}
        => remove the labels key
    """
    qs_parts = _get_qs_parts_from_context(context)
    if key in qs_parts:
        if not values:
            del qs_parts[key]
        else:
            _set_part_as_list(qs_parts, key)
            if isinstance(values[0], (list, tuple)):
                values = values[0]
            for value in values:
                value = _coerce_value(value)
                try:
                    qs_parts[key].remove(value)
                except ValueError:
                    pass
            if not len(qs_parts[key]):
                del qs_parts[key]
    return make_querystring(qs_parts)
def add_to_querystring(context, key, value):
    """
    {% add_to_querystring "labels" "foo" %}
        => add foo to labels
    """
    qs_parts = _get_qs_parts_from_context(context)
    value = _coerce_value(value)
    _set_part_as_list(qs_parts, key)
    qs_parts[key].append(value)
    return make_querystring(qs_parts)
Ejemplo n.º 4
0
    def prepare_issues_filter_context(self, filter_context):
        """
        Prepare a dict to use in the template, with many informations about the
        current filter: parts (as found in the querystring), objects (to use for
        display in the template), the base querystring (without user information
        ), and the full querystring (with user informations if a assigned/created
        filter is used)
        """
        # we need a querystring without the created/assigned parts
        qs_filter_without_user = dict(filter_context['qs_filters'])
        qs_filter_without_user.pop('user_filter_type', None)
        qs_filter_without_user.pop('username', None)

        context_issues_filter = {
            'parts': filter_context['qs_filters'],
            'objects': filter_context['filter_objects'],
            'querystring': make_querystring(qs_filter_without_user),
            'querystring_with_user': make_querystring(filter_context['qs_filters']),
        }

        return context_issues_filter
def replace_in_querystring(context, key, value):
    """
    {% replace_in_querystring "state" "open" %}
        => set the state to open
    """
    qs_parts = _get_qs_parts_from_context(context)
    value = _coerce_value(value)
    if value is None:
        qs_parts.pop(key, None)
    else:
        qs_parts[key] = value
    return make_querystring(qs_parts)
def toggle_in_querystring(context, key, value):
    """
    {% toggle_in_querystring "state" "open" %}
        => is state is open, remove the state, else set state to open
    """
    qs_parts = _get_qs_parts_from_context(context)
    value = _coerce_value(value)
    if value is not None:
        if key in qs_parts and qs_parts[key] == value:
            del qs_parts[key]
        else:
            qs_parts[key] = value
    return make_querystring(qs_parts)
def replace_many_in_querystring(context, *args):
    """
    {% replace_many_in_querystring "sort" "created" "direction" "asc" %}
        => set the sort to created and the direction to asc
    """
    qs_parts = _get_qs_parts_from_context(context)
    pairs = zip(args[::2], args[1::2])
    for key, value in pairs:
        value = _coerce_value(value)
        if value is None:
            qs_parts.pop(key, None)
        else:
            qs_parts[key] = value
    return make_querystring(qs_parts)