def create_or_modify(topic_name, question, answer, user): """ Creates or modifies a topic based if we have topics with the name entered and the user is the creator. """ query_ids = topics_by_id(topic_name) topic_com = None for topic_id in query_ids: topic = Topic.objects.get(id=topic_id) if topic.creator == user: topic_com = topic break if not topic_com: topic_to_add = Topic() topic_to_add.name = parsing.scrub_name(topic_name) topic_to_add.color = random_color() topic_to_add.creator = user topic_to_add.created_flag = False topic_to_add.save() topic_com = topic_to_add # In case the user wants to add questions to an existing topic. question_to_add = Question() question_to_add.topic = topic_com question_to_add.question = markdown.markdown(question) question_to_add.answer = markdown.markdown(answer) question_to_add.added_flag = False question_to_add.save() return topic_com
def include_questions(q_a, topic_name, user): """ Inserting the questions and answers in the db. """ topics = list(Topic.objects.all()) if topic_name not in [topic.name for topic in topics]: topic = Topic() topic.name = topic_name topic.color = random_color() topic.creator = user topic.created_flag = False topic.save() else: topic = Topic.objects.get(name=topic_name) for question_el, answer_el in q_a.items(): if not same_questions(question_el, topic_name) and not same_answers( answer_el, topic_name ): question = Question() question.topic = topic question.question = question_el question.answer = answer_el question.added_flag = False question.save() return topic.id