def create_quiz(): ## Create quiz quiz_name = (str)(request.form['quiz_name']) quiz = Quiz(name=quiz_name) ## Pick questions for that quiz question_list = list() question_count = db.session.query(Question).count() for _ in range(0, 10): searching = True while searching: number = random.randint(1, question_count - 1) if number not in question_list: question_list.append(number) searching = False quiz.questions = json.dumps(question_list) seconds_later = 60 if 'seconds' in request.form.keys(): seconds_later = (int)(request.form['seconds']) quiz.start_time = datetime.now() + timedelta(seconds=seconds_later) quiz.end_time = quiz.start_time + timedelta(minutes=10) db.session.add(quiz) db.session.commit() status_dict = dict() status_dict['status'] = 'CREATED' status_dict['quiz_id'] = quiz.id return jsonify(status_dict)
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)