def save_to_db(self): # Only create a new post instance in the database if it doesn't exist already. try: PostModel.get(PostModel.link == self.link) return 1 except PostModel.DoesNotExist: post_model = PostModel.create(title=self.title, link=self.link, timestamp=self.timestamp) post_model.save() # Save the tags for tag in self.tags: # Check that there is already a tag try: tag_model = TagModel.get(TagModel.tag_name == tag.string) except TagModel.DoesNotExist: tag_model = TagModel.create(tag_name=tag.string, tag_link=tag['href']) tag_model.save() # Check that there isn't already a relationship try: TagRelation.get(TagRelation.post_id == post_model.get_id(), TagRelation.tag_id == tag_model.get_id()) except TagRelation.DoesNotExist: tag_relation = TagRelation.create(post_id=post_model, tag_id=tag_model) tag_relation.save() return 0