utils.py 3.89 KB
Newer Older
1 2 3
from __future__ import unicode_literals

from django.utils.encoding import force_text
4
from django.utils.functional import wraps
5
from django.utils import six
Alex Gaynor committed
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

def parse_tags(tagstring):
    """
    Parses tag input, with multiple word input being activated and
    delineated by commas and double quotes. Quotes take precedence, so
    they may contain commas.

    Returns a sorted list of unique tag names.

    Ported from Jonathan Buchanan's `django-tagging
    <http://django-tagging.googlecode.com/>`_
    """
    if not tagstring:
        return []

22
    tagstring = force_text(tagstring)
23 24 25 26

    # Special case - if there are no commas or double quotes in the
    # input, we don't *do* a recall... I mean, we know we only need to
    # split on spaces.
27 28
    if ',' not in tagstring and '"' not in tagstring:
        words = list(set(split_strip(tagstring, ' ')))
29 30 31 32 33 34 35 36 37 38 39 40
        words.sort()
        return words

    words = []
    buffer = []
    # Defer splitting of non-quoted sections until we know if there are
    # any unquoted commas.
    to_be_split = []
    saw_loose_comma = False
    open_quote = False
    i = iter(tagstring)
    try:
41
        while True:
42 43
            c = six.next(i)
            if c == '"':
44
                if buffer:
45
                    to_be_split.append(''.join(buffer))
46 47 48
                    buffer = []
                # Find the matching quote
                open_quote = True
49 50
                c = six.next(i)
                while c != '"':
51
                    buffer.append(c)
52
                    c = six.next(i)
53
                if buffer:
54
                    word = ''.join(buffer).strip()
55 56 57 58 59
                    if word:
                        words.append(word)
                    buffer = []
                open_quote = False
            else:
60
                if not saw_loose_comma and c == ',':
61 62 63 64 65 66
                    saw_loose_comma = True
                buffer.append(c)
    except StopIteration:
        # If we were parsing an open quote which was never closed treat
        # the buffer as unquoted.
        if buffer:
67
            if open_quote and ',' in buffer:
68
                saw_loose_comma = True
69
            to_be_split.append(''.join(buffer))
70 71
    if to_be_split:
        if saw_loose_comma:
72
            delimiter = ','
73
        else:
74
            delimiter = ' '
75 76 77 78 79 80 81
        for chunk in to_be_split:
            words.extend(split_strip(chunk, delimiter))
    words = list(set(words))
    words.sort()
    return words


82
def split_strip(string, delimiter=','):
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    """
    Splits ``string`` on ``delimiter``, stripping each resulting string
    and returning a list of non-empty strings.

    Ported from Jonathan Buchanan's `django-tagging
    <http://django-tagging.googlecode.com/>`_
    """
    if not string:
        return []

    words = [w.strip() for w in string.split(delimiter)]
    return [w for w in words if w]


def edit_string_for_tags(tags):
    """
    Given list of ``Tag`` instances, creates a string representation of
    the list suitable for editing by the user, such that submitting the
    given string representation back without changing it will give the
    same list of tags.

    Tag names which contain commas will be double quoted.

    If any tag name which isn't being quoted contains whitespace, the
    resulting string of tag names will be comma-delimited, otherwise
    it will be space-delimited.

    Ported from Jonathan Buchanan's `django-tagging
    <http://django-tagging.googlecode.com/>`_
    """
    names = []
    for tag in tags:
        name = tag.name
116
        if ',' in name or ' ' in name:
117
            names.append('"%s"' % name)
118 119
        else:
            names.append(name)
120
    return ', '.join(sorted(names))
121

Alex Gaynor committed
122 123

def require_instance_manager(func):
Alex Gaynor committed
124
    @wraps(func)
Alex Gaynor committed
125
    def inner(self, *args, **kwargs):
126
        if self.instance is None:
Alex Gaynor committed
127 128 129
            raise TypeError("Can't call %s with a non-instance manager" % func.__name__)
        return func(self, *args, **kwargs)
    return inner