示例#1
0
def quiz(request):
  if 'uid' not in request.session:
    return redirect('login')
  user = User.objects.get(id=request.session['uid'])

  if user.stage == 'quiz_started':
    if request.method == "GET":
      quizForm = QuizForm()
      return render(request, 'core/quiz.html', {'quizForm': quizForm})

    elif request.method == "POST":
      quizForm = QuizForm(request.POST, request.FILES)
      if quizForm.is_valid():
        quiz = quizForm.save(commit=False)
        quiz.work_eligible = request.POST['work_eligible'] == 'Yes'
        quiz.save()
        user.quiz = quiz
        user.stage = 'quiz_completed'
        now = datetime.datetime.now()
        user.quiz_completed_date = now
        user.latest_change = now
        user.save()
      else:
        return render(request, 'core/quiz.html',
                      {'quizForm': quizForm,
                       'errors': quizForm.errors})

  return redirect('decider')
示例#2
0
def add_quiz(request):
    """Add the new quiz that superuser created to the database."""
    form = QuizForm(request.POST)
    if form.is_valid():
        new_quiz = form.save()
        new_quiz.save()
        return HttpResponseRedirect('/create_quiz/')
    else:
        return render(request, 'invalidAttempt.html',
                      {'message': 'Invalid input!'})
示例#3
0
def make_quiz(request):
    """Shows all current quizes and lets users make new ones"""
    quizzes = Quiz.objects.all()
    if request.method == "POST":
        # copies form data to a new dictionary and adds the user as author
        data = request.POST
        data_copy = dict()
        for key, value in data.iteritems():
            data_copy[key] = value
            data_copy['author'] = request.user.id
        form = QuizForm(data_copy)
        if form.is_valid():
            form.save(request.user)
            return HttpResponseRedirect('/quiz/new')
    else:
        form = QuizForm()
    return render(request, 'quiz/make_quiz.html', context={'form': form, 'quizzes': quizzes})
示例#4
0
def quizUpdate(request, department, class_number, year, semester, section, qid):
	user = request.user
	c = getClassObject(department, class_number, year, semester, section, user)
	q = get_object_or_404(Quiz, pk=qid)
	
	quizzes = Quiz.objects.filter(cid=c.cid)

	if request.method == 'POST':
		form = QuizForm(request.POST)
		if form.is_valid():
			Quiz.objects.filter(id=q.id).update(name=form.cleaned_data["name"], start_date=form.cleaned_data["start_date"], end_date=form.cleaned_data["end_date"], student_attempts=form.cleaned_data["student_attempts"])
			url = getClassUrl(c) + 'instructor/quiz/create/'
			return HttpResponseRedirect(url)
	else:
		form = QuizForm(initial={'name': q.name, 'start_date': q.start_date, 'end_date': q.end_date, 'student_attempts': q.student_attempts} )

	content = getContent(c, user)
	content['form'] = form
	content['quizzes'] = quizzes

	return render_to_response('instructor/quizCreate.html', content, 
		context_instance=RequestContext(request))
示例#5
0
def quizCreate(request, department, class_number, year, semester, section):
	user = request.user
	c = getClassObject(department, class_number, year, semester, section, user)

	quizzes = Quiz.objects.filter(cid=c.cid)

	if request.method == 'POST':
		quiz = Quiz(cid=c)
		form = QuizForm(request.POST, instance=quiz)
		if form.is_valid():
			form.save()
			url = getClassUrl(c) + 'instructor/quiz/create/'
			return HttpResponseRedirect("")
	else:
		form = QuizForm()

	content = getContent(c, user)
	content['form'] = form
	content['quizzes'] = quizzes

	return render_to_response('instructor/quizCreate.html', content, 
		context_instance=RequestContext(request))
示例#6
0
def index(request):
    if request.method == 'POST':

        form = QuizForm(request.POST)
        if form.is_valid():  # TODO: Errors must be handled
            _id = form.cleaned_data["categories"]

            _category = Category.objects.get(
                id=_id)  # TODO: cleaned data must be used
            number_of_questions = 6
            _questions = _category.get_random_questions(number_of_questions)
            if len(_questions) == 0:
                form = QuizForm(request.POST)
                context_data = {
                    'form': form,
                    'error_message': "سوالی در این موضوع وجود ندارد"
                }
                return render(request, "quiz_index.html", context_data)
            print "random questions:"
            print _questions

            quiz = Quiz(category=_category)
            user1 = User.objects.get(username=request.user)
            quiz.set_user1(user1)
            user2 = User.objects.order_by('?').first()
            while user2 == user1:
                user2 = User.objects.order_by('?').first()
            quiz.set_user2(user2)
            quiz.save()

            quiz.questions = _questions
            quiz.save()

            print quiz.questions.all()

            #for question in _questions:
            # print "question to be added: "
            # print question.question
            #    quiz.add_question(question)

            #quiz.save()
            # print "quiz questions:" + str(quiz.questions)
            # print "quiz id: " + str(quiz.id)

            print user2.email
            email = EmailMessage('new Challenge',
                                 'quizdown.ir/quiz/start?quiz_id=' +
                                 ` quiz.id `,
                                 to=[user2.email])
            email.send()

            request.session['_quiz_id'] = json.dumps(quiz.id)
            request.session['question_index'] = 0
            request.session.save()
            request.session.modified = True
            return redirect('/quiz/start/')
            # start(None, quiz.id)

    # if a GET (or any other method) we'll create a blank form
    else:
        form = QuizForm(request.POST)
        context_data = {'form': form}
        return render(request, "quiz_index.html", context_data)
示例#7
0
 def test_quiz_form_invalid(self):
     """Testing whether the quiz form is invalid."""
     form2_data = {'name': 'Database', 'subject': 'CS306', 'difficulty': 5}
     form2 = QuizForm(data=form2_data)
     self.assertFalse(form2.is_valid())
示例#8
0
 def test_quiz_form_valid(self):
     """Testing whether the quiz form is valid."""
     form_data = {'name': 'Database', 'subject': 'CS306', 'difficulty': 1}
     form = QuizForm(data=form_data)
     self.assertTrue(form.is_valid())