def test_saving_and_retrieving_users(self): quiz_ = Quiz() quiz_.save() first_user = User() first_user.user_text = 'The first (ever) quiz user' first_user.quiz2 = quiz_ first_user.save() second_user = User() second_user.user_text = 'user the second' second_user.quiz2 = quiz_ second_user.save() saved_quiz = Quiz.objects.first() self.assertEqual(saved_quiz, quiz_) saved_users = User.objects.all() self.assertEqual(saved_users.count(), 2) first_saved_user = saved_users[0] second_saved_user = saved_users[1] self.assertEqual(first_saved_user.user_text, 'The first (ever) quiz user') self.assertEqual(first_saved_user.quiz2, quiz_) self.assertEqual(second_saved_user.user_text, 'user the second') self.assertEqual(second_saved_user.quiz2, quiz_)
def submit(request): option_pk = [] type_all = Type.objects.all() correct_answer_count = 0 total_question = 0 if request.method == 'POST': if 'email' in request.POST: email = request.POST['email'] if 'type_id' in request.POST: id = request.POST['type_id'] quiz = get_object_or_404(Quiz,pk=id) question_attempted = quiz.question_set.all() level = 0 template = 'quiz/submit.html' elif 'level_id' in request.POST: id = request.POST['level_id'] level = get_object_or_404(Level,pk=id) question_attempted = level.question_set.all() template = 'quiz/submit-full.html' quiz = '' user = User(email=email,all_info='') user.save() #Get Question as per Quiz Type Attempted #Get Correct Answer Count for question in question_attempted: total_question = total_question + 1 if('option'+str(question.id)) in request.POST: pk = request.POST['option'+str(question.id)] option = get_object_or_404(Question_option, pk=pk) test_attempted = Attempted(question_id=question.id,option_id=option.id,user_id=int(user.id)) test_attempted.save() if(option.is_correct == 1): if(option.id == test_attempted.option_id): correct_answer_count = correct_answer_count + 1 user_result = get_list_or_404(Attempted, user_id=user.id) # user_result = answer= [] per = (float(correct_answer_count)/total_question)*100 percentage = round(per,2) for result in user_result: answer.append(int(result.option_id)) #return render(request, {'type_id':type_id}) """try: subject, from_email, to = 'mybologsteps: Quiz Result', '*****@*****.**', email text_content = 'This is an important message.' html_content = render_to_string('quiz/the_template.html', {'answer':answer ,'correct_answer_count':correct_answer_count,'total_question':total_question}) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() except BadHeaderError: return HttpResponse('Invalid header found.')""" return render(request, template,{'count':option_pk, 'type_all':type_all, 'question_attempted':question_attempted,'user_id':user.id,'answer':answer ,'correct_answer_count':correct_answer_count,'total_question':total_question,'level':level, 'percentage':percentage, 'quiz':quiz}) else: return index(request)
def login(request): if request.method == 'POST': inputEmail = request.POST['inputEmail'] inputUsername = request.POST['inputUsername'] inputPassword = request.POST['inputPassword'] user = User(email=inputEmail, all_info='') user.save() return render(request, 'quiz/logged-in.html', {'inputEmail': inputEmail})
def login(request): if request.method == 'POST': inputEmail = request.POST['inputEmail'] inputUsername = request.POST['inputUsername'] inputPassword = request.POST['inputPassword'] user = User(email=inputEmail,all_info='') user.save() return render(request, 'quiz/logged-in.html',{'inputEmail':inputEmail})
def save(self): password = self.validated_data['password'] password2 = self.validated_data['password2'] if password != password2: raise serializers.ValidationError({'password':'******'}) user = User(username = self.validated_data['username']) user.set_password(password) user.save() return user
def test_render_after_submit(self): quiz_ = Quiz() quiz_.save() user_ = User() user_.user_text = 'test' user_.user_point += 1 user_.quiz2 = quiz_ user_.save() response = self.client.post('/%d/submit' % (quiz_.id, ), data={'user_input': 'A new user'})
def post(self, request): try: data = { 'licence_type': request.data['licence_type'], 'email': request.data['email'], 'licence': '', 'created_by': request.user, } if request.data['licence_type'] == 'student': data['class_entity'] = Class.objects.get( id=request.data['class_entity_id']) user = User(**data) user.set_password('AFbp12Mpl56!') user.save() return Response(status=status.HTTP_201_CREATED) except Exception as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
def create(self, validated_data): profile_data = self.initial_data['profile'] validated_data.pop('profile') password = validated_data.pop('password') validated_data['username'] = '******'.format( validated_data['first_name'], validated_data['last_name'] ) user = User(**validated_data) validate_password(password, user) user.set_password(password) user.save() UserProfile.objects.create(user=user, **profile_data) return user
def start(request, exp, uuid=None): if not uuid or uuid == "None": uuid = uuid4() returning_user = User(uuid=uuid) returning_user.user_agent = request.META.get("HTTP_USER_AGENT") returning_user.language = request.META.get("HTTP_ACCEPT_LANGUAGE") returning_user.ip = request.META.get('REMOTE_ADDR') returning_user.save() return redirect(start, exp=exp, uuid=uuid) try: returning_user = User.objects.get(uuid=uuid) except: start(request, exp, None) experiment = Experiment.objects.get(id=exp) if not experiment.active: return redirect(home) title = experiment.name return render(request, "start.html", {"returning_user": returning_user, "exp": exp, "uuid": uuid, "title": title})
def submit(request): option_pk = [] type_all = Type.objects.all() correct_answer_count = 0 total_question = 0 if request.method == 'POST': if 'email' in request.POST: email = request.POST['email'] if 'type_id' in request.POST: id = request.POST['type_id'] quiz = get_object_or_404(Quiz, pk=id) question_attempted = quiz.question_set.all() level = 0 template = 'quiz/submit.html' elif 'level_id' in request.POST: id = request.POST['level_id'] level = get_object_or_404(Level, pk=id) question_attempted = level.question_set.all() template = 'quiz/submit-full.html' quiz = '' user = User(email=email, all_info='') user.save() #Get Question as per Quiz Type Attempted #Get Correct Answer Count for question in question_attempted: total_question = total_question + 1 if ('option' + str(question.id)) in request.POST: pk = request.POST['option' + str(question.id)] option = get_object_or_404(Question_option, pk=pk) test_attempted = Attempted(question_id=question.id, option_id=option.id, user_id=int(user.id)) test_attempted.save() if (option.is_correct == 1): if (option.id == test_attempted.option_id): correct_answer_count = correct_answer_count + 1 user_result = get_list_or_404(Attempted, user_id=user.id) # user_result = answer = [] per = (float(correct_answer_count) / total_question) * 100 percentage = round(per, 2) for result in user_result: answer.append(int(result.option_id)) #return render(request, {'type_id':type_id}) """try: subject, from_email, to = 'mybologsteps: Quiz Result', '*****@*****.**', email text_content = 'This is an important message.' html_content = render_to_string('quiz/the_template.html', {'answer':answer ,'correct_answer_count':correct_answer_count,'total_question':total_question}) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() except BadHeaderError: return HttpResponse('Invalid header found.')""" return render( request, template, { 'count': option_pk, 'type_all': type_all, 'question_attempted': question_attempted, 'user_id': user.id, 'answer': answer, 'correct_answer_count': correct_answer_count, 'total_question': total_question, 'level': level, 'percentage': percentage, 'quiz': quiz }) else: return index(request)