示例#1
0
 def clean(self):
     self.name = TagNameNormalizer.normalize(self.name)
     try:
         syn = Synonym.objects.get(name=self.name)
         raise ValidationError(
             'Synonym with same name already exists for tag "%s"' %
                 syn.tag.name)
     except:
         pass
示例#2
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))

        # Parse the input and normalize the tag names
        normalized_tag_names = []
        for t_name in parse_tag_input(tag_names):
            t_name = TagNameNormalizer.normalize(t_name)
            #
            # After normalization, make sure the same tag was not entered
            # twice with different capitalization
            #
            if t_name not in normalized_tag_names:
                normalized_tag_names.append(t_name)

        # Apply synonyms (my new word: synonymization :-)
        synonymized_tag_names = []
        for t_name in normalized_tag_names:
            try:
                s = Synonym.objects.get(name=t_name)
                t_name = s.tag.name
            except Synonym.DoesNotExist:
                pass
            #
            # After "synonymization", make sure the same tag was not entered
            # more than once
            #
            if t_name not in synonymized_tag_names:
                synonymized_tag_names.append(t_name)

        # Remove tags which no longer apply
        tags_for_removal = [tag for tag in current_tags \
                            if tag.name not in synonymized_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 synonymized_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)
示例#3
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 = TagNameNormalizer.normalize(tag_names[0])
     try:
         s = Synonym.objects.get(name=tag_name)
         tag_name = s.tag.name
     except Synonym.DoesNotExist:
         pass
     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)
 def handle(self, *args, **options):
     for tag in Tag.objects.all():
         normalized_name = TagNameNormalizer.normalize(tag.name)
         if normalized_name == tag.name:
             self.stdout.write('Skipping: %s\n' % tag.name)
             continue
         try:
             existing_tag = Tag.objects.get(name=normalized_name)
             self.stdout.write('Merging: %s => %s\n' %
                 (tag.name, existing_tag.name)
             )
             tag.merge_into(existing_tag)
             continue
         except Tag.DoesNotExist:
             self.stdout.write('Renaming: %s => %s\n' %
                 (tag.name, normalized_name)
             )
             tag.name = normalized_name
             tag.save()
     self.stdout.write('Done normalizing tags names.\n')
示例#5
0
 def clean(self):
     self.name = TagNameNormalizer.normalize(self.name)