def parse_quiz_yaml(path, string): d = yaml.load(string) sugar = make_sugar(path, d) assert d.get('title') and len(d['title']) > 0, "Quiz is missing title" assert d.get('subtitle') and len( d['subtitle']) > 0, "Quiz is missing subtitle" assert d.get('text') and len(d['text']) > 0, "Quiz is missing text body" assert d.get('questions') and len( d['questions']) > 0, "Quiz has no questions" assert d.get('email') and len(d['email']) > 0, "Quiz has no email message" email = d['email'] for k in ['$name', '$url']: assert k in email, "Quiz email is missing parameter %s" % k questions = d['questions'] for i, q in enumerate(questions): assert q.get('text'), "Question #%d has no text" % (i + 1) assert q.get('choices') and len( q['choices']) > 1, "Question #%d needs more than one choice" % (i + 1) assert q.get( 'correct'), "Question #%d is missing a correct answer" % (i + 1) assert q.get('correct') in q[ 'choices'], "Question #%d has a correct answer that is not one of the choices" % ( i + 1) if q.get('type'): assert q['type'] in [t[0] for t in QUESTION_TYPES] quiz = Quiz( title=d['title'], subtitle=d['subtitle'], text=sugar(d['text']), ) quiz.save() for i, q in enumerate(d['questions']): for choice in q['choices']: q['choices'][choice] = sugar(q['choices'][choice]) question = Question(text=sugar(q['text']), choices=json.dumps(q['choices']), correct=q['correct'], explanation=sugar(q.get('explanation') or ''), index=i + 1) if q.get('type'): question.type = q['type'] question.save() quiz.questions.add(question) return md.markdown(email), quiz
def parse_quiz_yaml(path, string): d = yaml.load(string) sugar = make_sugar(path, d) assert d.get('title') and len(d['title']) > 0, "Quiz is missing title" assert d.get('subtitle') and len(d['subtitle']) > 0, "Quiz is missing subtitle" assert d.get('text') and len(d['text']) > 0, "Quiz is missing text body" assert d.get('questions') and len(d['questions']) > 0, "Quiz has no questions" assert d.get('email') and len(d['email']) > 0, "Quiz has no email message" email = d['email'] for k in ['$name','$url']: assert k in email, "Quiz email is missing parameter %s" % k questions = d['questions'] for i, q in enumerate(questions): assert q.get('text'), "Question #%d has no text" % (i + 1) assert q.get('choices') and len(q['choices']) > 1, "Question #%d needs more than one choice" % (i + 1) assert q.get('correct'), "Question #%d is missing a correct answer" % (i + 1) assert q.get('correct') in q['choices'], "Question #%d has a correct answer that is not one of the choices" % (i + 1) if q.get('type'): assert q['type'] in [t[0] for t in QUESTION_TYPES] quiz = Quiz( title=d['title'], subtitle=d['subtitle'], text=sugar(d['text']), ) quiz.save() for i, q in enumerate(d['questions']): for choice in q['choices']: q['choices'][choice] = sugar(q['choices'][choice]) question = Question( text=sugar(q['text']), choices=json.dumps(q['choices']), correct=q['correct'], explanation=sugar(q.get('explanation') or ''), index=i+1 ) if q.get('type'): question.type = q['type'] question.save() quiz.questions.add(question) return md.markdown(email), quiz
def create_quiz(request, course_slug, page_slug): ''' create_Quiz View This view will create a quiz and take the user to the quiz editor screen. @author John Hartquist ''' #enforcing permissions try: e = Enrollment.objects.get(course__slug = course_slug, user = request.user) except Enrollment.DoesNotExist: return custom_403(request, "User cannot create a quiz in the course because the user is not enrolled in this course") if not e.edit: return custom_403(request, "User cannot create a quiz in the course because the user does not have edit permissions on the course") page_slug = safeSlug(page_slug) if request.method == "POST" and "Create Quiz" in request.POST: course = Course.objects.get(slug=course_slug) name = request.POST['name'] if name == "": return master_rtr(request, 'page/quiz/create-quiz.html', {'message': "You may not enter a blank quiz name"}) #exists = Quiz.objects.get(slug=slugify(name)) try: exists = Quiz.objects.get(slug=slugify(name), course=course) return master_rtr(request, 'page/quiz/create-quiz.html', {'message': "Quiz with this name already exists"}) except: newQuiz = Quiz(course=course, name=name, slug=slugify(name), text=name, upToDate=True) insertLastChildPage(newQuiz, Page.objects.get(slug=page_slug, course=course)) newQuiz = Quiz.objects.get(slug=newQuiz.slug, course=course) workingCopy = Quiz(course=newQuiz.course, name=newQuiz.name, slug=(newQuiz.slug + "_workingCopy"), text=newQuiz.name, left=0, right=0) workingCopy.save() return HttpResponseRedirect(reverse('pages.views.edit_page', args=[course_slug, newQuiz.slug])) else: return master_rtr(request, 'page/quiz/create-quiz.html')
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)