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)
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)
def add_questions(request, quizid): user = request.user if user.is_admin(): quiz = Quiz.objects.get(quiz_id=quizid) if request.method == 'POST': q_type = strip_tags(request.POST.get('type')) ques = Question() ques.quiz = quiz ques.type = strip_tags(request.POST.get('type')) ques.marks = strip_tags(request.POST.get('marks')) ques.level = strip_tags(request.POST.get('level')) ques.time_limit = strip_tags(request.POST.get('time_limit')) ques.question = strip_tags(request.POST.get('question')) ques.negative = strip_tags(request.POST.get('negative')) # if Image is uploaded img = request.FILES.get('image') if img: ques.image = img else: print("\n\n\n\nNo File was uploaded\n\n\n\n") # if Code is added code = request.POST.get('code') if code: ques.code = code # if Question is subjective if q_type == 's': ques.subjective_answer = strip_tags( request.POST.get('subjective_answer')) # Question is objective else: ques.option_A = strip_tags(request.POST.get('option_a')) ques.option_B = strip_tags(request.POST.get('option_b')) ques.option_C = strip_tags(request.POST.get('option_c')) ques.option_D = strip_tags(request.POST.get('option_d')) ques.correct = strip_tags(request.POST.get('correct')) ques.save() return JsonResponse({'kudos': "kudos"}) # GET the page else: return render(request, 'add_questions.html', { 'title': 'Add Questions', 'quiz_data': quiz }) # User Does not has sufficient permissions else: messages.info( request, 'You do not have the permissions required to edit this quiz') return redirect('home')