def remove_tags(old, new, post_id): # todo: maybe delete unused tags new_tags = [nt.decode('utf-8') for nt in new.split(';')] for old_tag in old: if unicode(old_tag.text) not in new_tags: Tag_to_Post.delete().\ where(Tag_to_Post.post_id == post_id and Tag_to_Post.tag_id == old_tag.tag_id).execute()
def add_new_tags(tags_string, post_id): """ Add new tags or create connection to post with existed """ tags = tags_string.split(';') for tag in tags: tg = tag.replace(' ', '') if not tg: continue try: old_tag = Tag.get(Tag.text == tag) try: tmp = Tag_to_Post.get(Tag_to_Post.post_id == post_id, Tag_to_Post.tag_id == old_tag.tag_id) except Tag_to_Post.DoesNotExist: Tag_to_Post.create(post_id=post_id, tag_id=old_tag.tag_id) except Tag.DoesNotExist: new_tag = Tag.create(text=tag) Tag_to_Post.create(post_id=post_id, tag_id=new_tag.tag_id) return