Example #1
0
    def test_with_simple_space_delimited_tags(self):
        """ Test with simple space-delimited tags. """

        self.assertEquals(parse_tag_input("one"), [u"one"])
        self.assertEquals(parse_tag_input("one two"), [u"one", u"two"])
        self.assertEquals(parse_tag_input("one two three"), [u"one", u"three", u"two"])
        self.assertEquals(parse_tag_input("one one two two"), [u"one", u"two"])
Example #2
0
    def test_with_comma_delimited_multiple_words(self):
        """ Test with comma-delimited multiple words.
            An unquoted comma in the input will trigger this. """

        self.assertEquals(parse_tag_input(",one"), [u"one"])
        self.assertEquals(parse_tag_input(",one two"), [u"one two"])
        self.assertEquals(parse_tag_input(",one two three"), [u"one two three"])
        self.assertEquals(parse_tag_input("a-one, a-two and a-three"), [u"a-one", u"a-two and a-three"])
Example #3
0
    def test_with_double_quoted_multiple_words(self):
        """ Test with double-quoted multiple words.
            A completed quote will trigger this.  Unclosed quotes are ignored. """

        self.assertEquals(parse_tag_input('"one'), [u"one"])
        self.assertEquals(parse_tag_input('"one two'), [u"one", u"two"])
        self.assertEquals(parse_tag_input('"one two three'), [u"one", u"three", u"two"])
        self.assertEquals(parse_tag_input('"one two"'), [u"one two"])
        self.assertEquals(parse_tag_input('a-one "a-two and a-three"'), [u"a-one", u"a-two and a-three"])
Example #4
0
 def clean(self, value):
     value = super(TagField, self).clean(value)
     if value == u'':
         return value
     for tag_name in parse_tag_input(value):
         if len(tag_name) > settings.MAX_TAG_LENGTH:
             raise forms.ValidationError(
                 _('Each tag may be no more than %s characters long.') %
                     settings.MAX_TAG_LENGTH)
     return value
Example #5
0
 def clean_name(self):
     value = self.cleaned_data['name']
     tag_names = parse_tag_input(value)
     if len(tag_names) > 1:
         raise forms.ValidationError(_('Multiple tags were given.'))
     elif len(tag_names[0]) > settings.MAX_TAG_LENGTH:
         raise forms.ValidationError(
             _('A tag may be no more than %s characters long.') %
                 settings.MAX_TAG_LENGTH)
     return value
Example #6
0
    def test_with_naughty_input(self):
        """ Test with naughty input. """

        # Bad users! Naughty users!
        self.assertEquals(parse_tag_input(None), [])
        self.assertEquals(parse_tag_input(""), [])
        self.assertEquals(parse_tag_input('"'), [])
        self.assertEquals(parse_tag_input('""'), [])
        self.assertEquals(parse_tag_input('"' * 7), [])
        self.assertEquals(parse_tag_input(",,,,,,"), [])
        self.assertEquals(parse_tag_input('",",",",",",","'), [u","])
        self.assertEquals(parse_tag_input('a-one "a-two" and "a-three'), [u"a-one", u"a-three", u"a-two", u"and"])
Example #7
0
 def add_tag(self, obj, tag_name):
     """
     Associates the given object with a tag.
     """
     tag_names = parse_tag_input(tag_name)
     if not len(tag_names):
         raise AttributeError(_('No tags were given: "%s".') % tag_name)
     if len(tag_names) > 1:
         raise AttributeError(_('Multiple tags were given: "%s".') % tag_name)
     tag_name = tag_names[0]
     if settings.FORCE_LOWERCASE_TAGS:
         tag_name = tag_name.lower()
     tag, created = self.get_or_create(name=tag_name)
     ctype = ContentType.objects.get_for_model(obj)
     TaggedItem._default_manager.get_or_create(
         tag=tag, content_type=ctype, object_id=obj.pk)
Example #8
0
    def update_tags(self, obj, tag_names):
        """
        Update tags associated with an object.
        """
        ctype = ContentType.objects.get_for_model(obj)
        current_tags = list(self.filter(items__content_type__pk=ctype.pk,
                                        items__object_id=obj.pk))
        updated_tag_names = parse_tag_input(tag_names)
        if settings.FORCE_LOWERCASE_TAGS:
            updated_tag_names = [t.lower() for t in updated_tag_names]

        # Remove tags which no longer apply
        tags_for_removal = [tag for tag in current_tags \
                            if tag.name not in updated_tag_names]
        if len(tags_for_removal):
            TaggedItem._default_manager.filter(content_type__pk=ctype.pk,
                                               object_id=obj.pk,
                                               tag__in=tags_for_removal).delete()
        # Add new tags
        current_tag_names = [tag.name for tag in current_tags]
        for tag_name in updated_tag_names:
            if tag_name not in current_tag_names:
                tag, created = self.get_or_create(name=tag_name)
                TaggedItem._default_manager.create(tag=tag, object=obj)
Example #9
0
 def test_tags_with_double_quotes_can_contain_commas(self):
     """ Double quotes can contain commas """
     self.assertEquals(parse_tag_input('a-one "a-two, and a-three"'), [u"a-one", u"a-two, and a-three"])
     self.assertEquals(parse_tag_input('"two", one, one, two, "one"'), [u"one", u"two"])
Example #10
0
 def test_with_loose_commas(self):
     """ Loose commas - split on commas """
     self.assertEquals(parse_tag_input('"one", two three'), [u"one", u"two three"])
Example #11
0
 def test_with_no_loose_commas(self):
     """ Test with no loose commas -- split on spaces. """
     self.assertEquals(parse_tag_input('one two "thr,ee"'), [u"one", u"thr,ee", u"two"])