def add(question, answers, category=None, tags=[]): ''' question is a dict with the following keys: text, endorsed_by, answer_type [, proposed_by, active, type, code] answers is a list of dicts with the following keys: text, correct [, explanation] ''' # imports valid only after init() from wouso.core.qpool.models import Question, Answer #print question #for a in answers: # print a # create and save question q = Question(**question) q.save() if len(tags) > 0: for tag in tags: q.tags.add(tag) q.save() if category is not None: q.category = category q.save() # create and save answers for question for answer in answers: a = Answer(question=q, **answer) a.save() return q
def add_question(question): """ question is a dictionary with the following keys: 'text': the question text (string) 'answer_type': whether the question is single choice or multiple choice (string) 'answers': a dictionary of answers with the following keys: 'text': the answer text (string) 'correct': whether the answer is correct (boolean) 'proposed_by': the user that proposed the question (User object) 'endorsed_by': the user endorsing the question (User object) 'active': whether the question is active (boolean) 'category': the question category (i.e. quiz) (Category object) 'tag': the question primary tag (from file name) (Tag object) 'file_tags': secondary tags (from file contents) (array of Tag objects) """ q = Question() q.save() q.text = question['text'] q.answer_type = question['answer_type'] q.save() if question['proposed_by']: q.proposed_by = question['proposed_by'] q.save() if question['endorsed_by']: q.endorsed_by = question['endorsed_by'] q.save() q.active = question['active'] if question['category']: q.category = question['category'] q.save() if question['tag']: q.tags.add(question['tag']) q.save() if question['file_tags']: for tag in question['file_tags']: q.tags.add(tag) q.save() for answer in question['answers']: a = Answer(question=q, **answer) a.save() return q
def add(question, answers, category=None, tags=None, file_tags=None): ''' question is a dict with the following keys: text, endorsed_by, answer_type [, proposed_by, active, type, code] answers is a list of dicts with the following keys: text, correct [, explanation] ''' # imports valid only after init() from wouso.core.qpool.models import Question, Answer, Tag, Category #print question #for a in answers: # print a # create and save question q = Question(**question) q.save() if tags: for tag in tags: q.tags.add(tag) q.save() if file_tags: for tag_name in file_tags: tag, created = Tag.objects.get_or_create(name=tag_name) if created: tag.save() if category is not None: category.tag_set.add(tag) category.save() q.tags.add(tag) q.save() if category is not None: q.category = category q.save() # create and save answers for question for answer in answers: a = Answer(question=q, **answer) a.save() return q