Esempio n. 1
0
 def test_recreation_of_tag_list_string_representations(self):
     plain = Tag.objects.create(name='plain')
     spaces = Tag.objects.create(name='spa ces')
     comma = Tag.objects.create(name='com,ma')
     self.assertEqual(edit_string_for_tags([plain]), u'plain')
     self.assertEqual(edit_string_for_tags([plain, spaces]), u'"spa ces", plain')
     self.assertEqual(edit_string_for_tags([plain, spaces, comma]), u'"com,ma", "spa ces", plain')
     self.assertEqual(edit_string_for_tags([plain, comma]), u'"com,ma", plain')
     self.assertEqual(edit_string_for_tags([comma, spaces]), u'"com,ma", "spa ces"')
Esempio n. 2
0
 def test_recreation_of_tag_list_string_representations(self):
     plain = Tag.objects.create(name='plain')
     spaces = Tag.objects.create(name='spa ces')
     comma = Tag.objects.create(name='com,ma')
     self.assertEqual(edit_string_for_tags([plain]), u'plain')
     self.assertEqual(edit_string_for_tags([plain, spaces]),
                      u'"spa ces", plain')
     self.assertEqual(edit_string_for_tags([plain, spaces, comma]),
                      u'"com,ma", "spa ces", plain')
     self.assertEqual(edit_string_for_tags([plain, comma]),
                      u'"com,ma", plain')
     self.assertEqual(edit_string_for_tags([comma, spaces]),
                      u'"com,ma", "spa ces"')
Esempio n. 3
0
    def add(self, *tags):
        str_tags = set([
            t
            for t in tags
            if not isinstance(t, self.through.tag_model())
        ])
        tag_objs = set(tags) - str_tags
        # If str_tags has 0 elements Django actually optimizes that to not do a
        # query.  Malcolm is very smart.
        existing = self.through.tag_model().objects.filter(
            name__in=str_tags
        )
        tag_objs.update(existing)

        for new_tag in str_tags - set(t.name for t in existing):
            tag_objs.add(self.through.tag_model().objects.create(name=new_tag))

        new_added_tags = []
        for tag in tag_objs:
            obj, created = self.through.objects.get_or_create(tag=tag, **self._lookup_kwargs())
            if created:
                self._inc_tag_count(tag, 1)
                new_added_tags.append(tag)
        # update tags_txt
        throughs = self.through.objects.filter(**self._lookup_kwargs())
        all_tags = [t.tag for t in throughs]
        self.instance.tags_txt = edit_string_for_tags(all_tags)
Esempio n. 4
0
    def add(self, *tags):
        str_tags = set(
            [t for t in tags if not isinstance(t, self.through.tag_model())])
        tag_objs = set(tags) - str_tags
        # If str_tags has 0 elements Django actually optimizes that to not do a
        # query.  Malcolm is very smart.
        existing = self.through.tag_model().objects.filter(name__in=str_tags)
        tag_objs.update(existing)

        for new_tag in str_tags - set(t.name for t in existing):
            tag_objs.add(self.through.tag_model().objects.create(name=new_tag))

        new_added_tags = []
        for tag in tag_objs:
            obj, created = self.through.objects.get_or_create(
                tag=tag, **self._lookup_kwargs())
            if created:
                self._inc_tag_count(tag, 1)
                new_added_tags.append(tag)
        # update tags_txt
        throughs = self.through.objects.filter(**self._lookup_kwargs())
        all_tags = [t.tag for t in throughs]
        self.instance.tags_txt = edit_string_for_tags(all_tags)
Esempio n. 5
0
 def render(self, name, value, attrs=None):
     if value is not None and not isinstance(value, basestring):
         value = edit_string_for_tags(
             [o.tag for o in value.select_related("tag")])
     return super(TagWidget, self).render(name, value, attrs)
Esempio n. 6
0
 def render(self, name, value, attrs=None):
     if value is not None and not isinstance(value, basestring):
         value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
     return super(TagWidget, self).render(name, value, attrs)