Example #1
0
def isTagList(field_data, all_data):
    """
    Validates that ``field_data`` is a valid list of tags.
    """
    for tag_name in parse_tag_input(field_data):
        if len(tag_name) > settings.MAX_TAG_LENGTH:
            raise ValidationError(
                _('Each tag may be no more than %s characters long.') % settings.MAX_TAG_LENGTH)
Example #2
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 #3
0
def isTag(field_data, all_data):
    """
    Validates that ``field_data`` is a valid tag.
    """
    tag_names = parse_tag_input(field_data)
    if len(tag_names) > 1:
        raise ValidationError(_('Multiple tags were given.'))
    elif len(tag_names[0]) > settings.MAX_TAG_LENGTH:
        raise ValidationError(
            _('A tag may be no more than %s characters long.') % settings.MAX_TAG_LENGTH)
Example #4
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)