def handle(self, *args, **options): """Logic of command 'python manage.py fill_db'""" for model in (Planet, Sith, QuestionSet, Question): model.objects.all().delete() all_planets = load_from_json('planets') for planet in all_planets: Planet(**planet).save() all_questions = load_from_json('questions') set_code = 0 for number, question in enumerate(all_questions): if number % QUESTIONS_NUMBER_IN_SET == 0: set_code += 1 QuestionSet(code=set_code).save() question["question_set"] = QuestionSet.get_item(set_code, parameter='code') Question(**question).save() superuser_data = settings.SUPERUSER_DATA all_sith = load_from_json('sith') for i, sith in enumerate([superuser_data] + all_sith): sith["planet"] = Planet.get_item(sith["planet"], parameter='name') sith["role"] = 'S' if i == 0: Sith.objects.create_superuser(**sith) else: sith["master"] = Sith.get_item(sith["master"], parameter='username') Sith(**sith).save()
def upload_questions(questions_dataframe): question_list = [] requires_diagram = [] ipdb.set_trace() for x in range(len(questions_dataframe)): answers = [None] * 4 specification_point, diagram_name, question, answers[0], answers[ 1], answers[2], answers[3] = questions_dataframe.iloc[x].values correct_answer = answers[0] random.shuffle(answers) # find index of correct answer, and convert {0, 1, 2, 3} into {a, b, c, d} correct_answer_letter = chr(answers.index(correct_answer) + 97) question_list.append( Question(specification_point=specification_point, question=question, diagram_name=diagram_name, a=answers[0], b=answers[1], c=answers[2], d=answers[3], correct_answer=correct_answer_letter))
def startElement(self, name, attrs): if name == "test": self.test = Test() self.test.tid = attrs.get("id") self.test.save() elif name == "question": self.cur_quest = Question() self.cur_quest.text = attrs.get("text") self.cur_quest.code = attrs.get("code") self.cur_quest.test_id = self.test self.cur_quest.save() elif name == "answer": self.cur_ans = Answer() self.cur_ans.correct = bool(attrs.get("correct")) self.cur_ans.text = attrs.get("text") self.cur_ans.question_id = self.cur_quest self.cur_ans.save()
class Sax(handler.ContentHandler): def __init__(self): handler.ContentHandler.__init__(self) self.test = None self.cur_quest = None self.cur_ans = None def startDocument(self): pass def startElement(self, name, attrs): if name == "test": self.test = Test() self.test.tid = attrs.get("id") self.test.save() elif name == "question": self.cur_quest = Question() self.cur_quest.text = attrs.get("text") self.cur_quest.code = attrs.get("code") self.cur_quest.test_id = self.test self.cur_quest.save() elif name == "answer": self.cur_ans = Answer() self.cur_ans.correct = bool(attrs.get("correct")) self.cur_ans.text = attrs.get("text") self.cur_ans.question_id = self.cur_quest self.cur_ans.save() def endElement(self, name): pass def characters(self, content): pass def ignorableWhitespace(self, content): pass def processingInstruction(self, target, data): pass
def result(request): """View for rendering of result page""" context = {} if request.method == 'POST': data = request.POST answers = [data[key] for key in data.keys() if '-right_answer' in key] recruit = Recruit.get_item(request.user.pk) questions = Question.get_items(recruit.question_set) if answers == [question.right_answer for question in questions]: context['test_passed'] = True recruit.change_role(answers) else: context['test_passed'] = False return render(request, 'recruits_app/result.html', context)
def get_context_data(self, **kwargs): """Method edits context data""" context = super(QuestionList, self).get_context_data(**kwargs) question_set = Recruit.get_item(self.request.user.pk).question_set questions = Question.get_items(question_set) if questions: question_set = inlineformset_factory(QuestionSet, Question, form=QuestionForm, extra=len(questions)) formset = question_set() for num, form in enumerate(formset.forms): form.initial['question'] = questions[num].question form.initial['right_answer'] = questions[num].right_answer context['question_set'] = formset return context
def categorise_and_format_questions(questions_dataframe): # the answers array is overwritten at every iteration # and only must exist temporarily answers = [None] * 4 # stores all different categorisations of questions questions_dict = { 'valid_questions': [], 'incomplete_questions': [], 'already_existing': [] } # iterating over the unvalidated questions dataframe for x in range(len(questions_dataframe)): # unpack the values from the dataframe into variables and an answers array specification_point, diagram_name, question, answers[0], answers[1], answers[2], answers[3] = questions_dataframe.iloc[x].values # creating an array to be iterated over when checking for unpopulated required fields required_fields = [specification_point, question, answers[0], answers[1], answers[2], answers[3]] correct_answer = answers[0] # retrieving the correct answer string random.shuffle(answers) # randomising the order of the answers # finding index of correct answer, and convert {0, 1, 2, 3} into {a, b, c, d} correct_answer_letter = chr(answers.index(correct_answer) + 97) if any(str(field) == 'nan' for field in required_fields): # checking whether required fields are unpopulated # if they are, populating and appending a question object to a # list of incomplete questions in the questions dict questions_dict['incomplete_questions'].append(Question( specification_point=specification_point, question=question, diagram_name=diagram_name, a=answers[0], b=answers[1], c=answers[2], d=answers[3], correct_answer=correct_answer_letter)) elif Question.objects.filter(question=question).count() != 0: # checking whether a question already exists in the database # if it does, populating and appending the question object to a # list of existing questions in the questions dict questions_dict['already_existing'].append(Question( specification_point=specification_point, question=question, diagram_name=diagram_name, a=answers[0], b=answers[1], c=answers[2], d=answers[3], correct_answer=correct_answer_letter)) else: # otherwise these questions will be valid # thus they are appended to a list of valid questions questions_dict['valid_questions'].append(Question( specification_point=specification_point, question=question, diagram_name=diagram_name, a=answers[0], b=answers[1], c=answers[2], d=answers[3], correct_answer=correct_answer_letter)) return questions_dict