def SetTags(self, tag_list): author_tag = Tag.normalise(u"author:%s" % (self.user.username,)) tags = (set([x for x in tag_list if Map.TAG_RE.search(x)][:5]) - Map.RESERVED_TAGS) old_tags = set(self.tags) old_tags.discard(author_tag) new_tags = set(tags) # Update tag join counts # These are used for suggestions, so exclude reserved tags old_tagjoins = set(TagJoin.GetPowerset(list(old_tags - Map.RESERVED_TAGS)[:5])) new_tagjoins = set(TagJoin.GetPowerset(list(tags))) new_tags.update(old_tags.intersection(Map.RESERVED_TAGS)) new_tags.add(author_tag) # Process added tags modified_tags = [] for tag_name in new_tags - old_tags: if Tag.normalise(tag_name): tag = Tag.get_or_insert_tag(tag_name) tag.count += 1 modified_tags.append(tag) # Process removed tags for tag_name in old_tags - new_tags: if Tag.normalise(tag_name): tag = Tag.get_or_insert_tag(tag_name) tag.count -= 1 modified_tags.append(tag) if modified_tags: db.put(modified_tags) # Process modified tagjoins TagJoin.update_joins(new_tagjoins, old_tagjoins, 20) self.tags = new_tags if tag_list: self.category = tag_list[0] else: self.category = None
def GetUserTags(self): user_tags = set(self.tags - Map.RESERVED_TAGS) author_tag = Tag.normalise(u"author:%s" % (self.user.username,)) user_tags.discard(author_tag) return sorted(user_tags)