def add_tags_and_return_added_tag_names(self, tag_names, item, user): """Add tags to the database and return the names of tags that were added. Arguments: tag_names -- the unparsed string representation of the tags to be added to the database item -- the item the tags are related to user -- the user adding the tags """ parsed_tag_names = parse_tag_input(tag_names) added_tags = [] for tag_name in parsed_tag_names: tag, created = Tagging.objects.get_or_create(tag_name=tag_name, item=item, user=user) if created: added_tags.append(tag.tag) return added_tags
def update_tags(self, tag_names, item=None, user=None): if item is None and user is None: raise Exception("User and item must not be none together. That deletes all tags.") filters = {} if item is not None: filters['item'] = item if user is not None: filters['user'] = user clean_tag_names = parse_tag_input(tag_names) self.filter(**filters).exclude(tag__in=clean_tag_names).delete() current_tags = [value['tag'] for value in self.filter(**filters).values('tag')] for tag in clean_tag_names: if tag not in current_tags: try: self.get_or_create(tag=tag, **filters) except IntegrityError: pass
def update_tags(self, tag_names, item, user): """Add tags from the tag_names that are not already in the database and remove those that are in the database but are not in tag_names. Arguments: tag_names -- the unparsed string representation of the tags that should be the only ones attached to an item item -- the item the tagging is related to user -- the user adding the taggings """ #clean the tag names clean_tag_names = parse_tag_input(tag_names) # Remove tags from the database that aren't in tag names. if user == item.creator: self.filter(item=item).exclude(tag__in=clean_tag_names).delete() else: self.filter(item=item, user=user).exclude(tag__in=clean_tag_names).delete() # Add new tags to the database. for tag in clean_tag_names: self.get_or_create(tag_name=tag, item=item, user=user)