def update_tags(self, obj, tag_names, default_namespace=None, q=None): """ Update tags associated with an object. Accepts a ``default_namespace`` parameter that is assigned to tags with no namespace specified. """ ctype = ContentType.objects.get_for_model(obj) current_tags = self.filter(items__content_type__pk=ctype.pk, items__object_id=obj.pk) if q is not None: current_tags = current_tags.filter(q) current_tags = list(current_tags) updated_tag_names = parse_tag_input(tag_names, default_namespace=default_namespace) if conf.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 unicode(tag) 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 = [unicode(tag) 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(**get_tag_parts(tag_name)) TaggedItem._default_manager.create(tag=tag, object=obj)
def update_tags(self, obj, tag_names, default_namespace=None, q=None): """ Update tags associated with an object. Accepts a ``default_namespace`` parameter that is assigned to tags with no namespace specified. """ ctype = ContentType.objects.get_for_model(obj) current_tags = self.filter(items__content_type__pk=ctype.pk, items__object_id=obj.pk) if q is not None: current_tags = current_tags.filter(q) current_tags = list(current_tags) updated_tag_names = parse_tag_input( tag_names, default_namespace=default_namespace) if conf.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 str(tag) 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 = [str(tag) 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(**get_tag_parts(tag_name)) TaggedItem._default_manager.create(tag=tag, object=obj)
def clean(self, value): value = super(TagField, self).clean(value) if value == u'': return value for tag_name in parse_tag_input( value, default_namespace=self.default_namespace): try: check_tag_length(get_tag_parts(tag_name)) except (ValueError, e): if len(e.args) < 3: raise part, max_len = e.args[1:3] if part == 'tag': raise forms.ValidationError( _('Each tag may be no more than %s characters long.') % max_len) elif part == 'namespace': raise forms.ValidationError( _('Each tag\'s namespace may be no more than %s characters long.' ) % max_len) elif part == 'name': raise forms.ValidationError( _('Each tag\'s name may be no more than %s characters long.' ) % max_len) elif part == 'value': raise forms.ValidationError( _('Each tag\'s value may be no more than %s characters long.' ) % max_len) else: raise return value
def add_tag(self, obj, tag_name, default_namespace=None): """ Associates the given object with a tag. Accepts a ``default_namespace`` parameter that is assigned to a tag with no namespace specified. """ tag_names = parse_tag_input(tag_name, default_namespace=default_namespace) 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 conf.FORCE_LOWERCASE_TAGS: tag_name = tag_name.lower() tag, created = self.get_or_create(**get_tag_parts(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 clean(self, value): value = super(TagField, self).clean(value) if value == u'': return value for tag_name in parse_tag_input(value, default_namespace=self.default_namespace): try: check_tag_length(get_tag_parts(tag_name)) except ValueError, e: if len(e.args) < 3: raise part, max_len = e.args[1:3] if part == 'tag': raise forms.ValidationError(_('Each tag may be no more than %s characters long.') % max_len) elif part == 'namespace': raise forms.ValidationError(_('Each tag\'s namespace may be no more than %s characters long.') % max_len) elif part == 'name': raise forms.ValidationError(_('Each tag\'s name may be no more than %s characters long.') % max_len) elif part == 'value': raise forms.ValidationError(_('Each tag\'s value may be no more than %s characters long.') % max_len) else: raise
def add_tag(self, obj, tag_name, default_namespace=None): """ Associates the given object with a tag. Accepts a ``default_namespace`` parameter that is assigned to a tag with no namespace specified. """ tag_names = parse_tag_input(tag_name, default_namespace=default_namespace) 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 conf.FORCE_LOWERCASE_TAGS: tag_name = tag_name.lower() tag, created = self.get_or_create(**get_tag_parts(tag_name)) ctype = ContentType.objects.get_for_model(obj) TaggedItem._default_manager.get_or_create(tag=tag, content_type=ctype, object_id=obj.pk)