Exemple #1
0
def update_quiz(request, id):
    data = json.loads(request.raw_post_data)
    quiz = Quiz.objects.get(id=id)
    quiz.title = data.get("title", quiz.title)
    quiz.subtitle = data.get("subtitle", quiz.subtitle)
    quiz.text = data.get("text", quiz.text)
    quiz.email = data.get("email", quiz.email)
    qids = []
    for qd in data.get("questions", []):
        qid = qd.get("id", None)
        if not qid:
            q = Question(index=qd['index'],
                         text=qd.get('text', ''),
                         choices=json.dumps(qd.get('choices', {})),
                         explanation=qd.get('explanation'),
                         correct=qd.get('correct', ''))
            q.save()
            qids.append(q.id)
            quiz.questions.add(q)
        else:
            q = Question.objects.get(id=qid)
            q.text = qd.get('text', '')
            q.choices = json.dumps(qd['choices'])
            q.correct = qd.get('correct', '')
            q.explanation = qd.get('explanation')
            q.index = qd['index']
            qids.append(q.id)
            q.save()
    # Look for questions that don't exist and delete them
    for q in Question.objects.filter(quiz=quiz):
        if not q.id in qids:
            q.delete()
    quiz.save()
    return retrieve_quiz(request, id)
Exemple #2
0
    def create(self, validated_data):
        q_uid = self.initial_data.get('q_uid')
        if q_uid is None:
            m_uid = Question.objects.all().aggregate(Max('uid')).get('uid__max')
            m_uid = 1 if m_uid is None else m_uid+1
        else:
            m_uid = int(q_uid)
        m_version = Question.objects.all().filter(uid=m_uid).aggregate(Max('version')).get('version__max')
        m_version = 1 if m_version is None else m_version+1
        answers = self.initial_data['answers']
        answers = self.validate_answers(answers)
        question = Question(uid=m_uid, version=m_version,
                            user=self.context['request'].user,
                            active=1, **validated_data)
        question.save()
        for one_answer in answers:
            q_answer = QuestionAnswer(question=question, **one_answer)
            try:
                q_answer.full_clean()
            except ValidationError as exc:
                question.delete()
                raise serializers.ValidationError(exc.message_dict)
            q_answer.save()

        return question
Exemple #3
0
def update_quiz(request, id):
	data = json.loads(request.raw_post_data)
	quiz = Quiz.objects.get(id=id)
	quiz.title = data.get("title", quiz.title)
	quiz.subtitle = data.get("subtitle", quiz.subtitle)
	quiz.text = data.get("text", quiz.text)
	quiz.email = data.get("email", quiz.email)
	qids = []
	for qd in data.get("questions",[]):
		qid = qd.get("id", None)
		if not qid:
			q = Question(
				index=qd['index'],
				text=qd.get('text',''),
				choices=json.dumps(qd.get('choices',{})),
				explanation=qd.get('explanation'),
				correct=qd.get('correct','')
			)
			q.save()
			qids.append(q.id)
			quiz.questions.add(q)
		else:
			q = Question.objects.get(id=qid)
			q.text = qd.get('text','')
			q.choices = json.dumps(qd['choices'])
			q.correct = qd.get('correct','')
			q.explanation = qd.get('explanation')
			q.index = qd['index']
			qids.append(q.id)
			q.save()
	# Look for questions that don't exist and delete them
	for q in Question.objects.filter(quiz=quiz):
		if not q.id in qids:
			q.delete()
	quiz.save()
	return retrieve_quiz(request, id)