def process_login_form(request, login_form): user = login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, "portal/email_verification_needed.html", {"user": user}) login(request, login_form.user) if using_two_factor(request.user): return render( request, "portal/2FA_redirect.html", { "form": AuthenticationForm(), "username": request.user.username, "password": login_form.cleaned_data["teacher_password"], }, ) next_url = request.GET.get("next", None) if next_url and is_safe_url(next_url): return HttpResponseRedirect(next_url) teacher = request.user.userprofile.teacher return redirect_teacher_to_correct_page(request, teacher)
def process_login_form(request, login_form): user = login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, "portal/email_verification_needed.html", {"user": user}) login(request, login_form.user) if using_two_factor(request.user): return render( request, "portal/2FA_redirect.html", { "form": AuthenticationForm(), "username": request.user.username, "password": login_form.cleaned_data["teacher_password"], }, ) next_url = request.GET.get("next", None) if next_url and is_safe_url(next_url): return HttpResponseRedirect(next_url) teacher = request.user.userprofile.teacher return redirect_user_to_correct_page(request, teacher)
def process_indep_student_login_form(request, independent_student_login_form): user = independent_student_login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, "portal/email_verification_needed.html", {"user": user}) login(request, independent_student_login_form.user) next_url = request.GET.get("next", None) if next_url and is_safe_url(next_url): return HttpResponseRedirect(next_url) return HttpResponseRedirect(reverse_lazy("student_details"))
def process_indep_student_login_form(request, independent_student_login_form): user = independent_student_login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, "portal/email_verification_needed.html", {"user": user}) login(request, independent_student_login_form.user) next_url = request.GET.get("next", None) if next_url and is_safe_url(next_url): return HttpResponseRedirect(next_url) return HttpResponseRedirect(reverse_lazy("student_details"))
def process_indep_student_login_form(request, independent_student_login_form): user = independent_student_login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, 'portal/email_verification_needed.html', {'user': user}) login(request, independent_student_login_form.user) next_url = request.GET.get('next', None) if next_url: return HttpResponseRedirect(next_url) return HttpResponseRedirect(reverse_lazy('student_details'))
def process_login_form(request, login_form): user = login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, 'portal/email_verification_needed.html', {'user': user}) login(request, login_form.user) if using_two_factor(request.user): return render( request, 'portal/2FA_redirect.html', { 'form': AuthenticationForm(), 'username': request.user.username, 'password': login_form.cleaned_data['teacher_password'], }) next_url = request.GET.get('next', None) if next_url: return HttpResponseRedirect(next_url) teacher = request.user.userprofile.teacher return redirect_user_to_correct_page(request, teacher)
def teach(request): invalid_form = False limits = getattr(request, 'limits', {'ip': [0], 'email': [0]}) captcha_limit = 5 using_captcha = (limits['ip'][0] > captcha_limit or limits['email'][0] > captcha_limit) should_use_captcha = (limits['ip'][0] >= captcha_limit or limits['email'][0] >= captcha_limit) LoginFormWithCaptcha = partial( create_form_subclass_with_recaptcha(TeacherLoginForm, recaptcha_client), request) InputLoginForm = LoginFormWithCaptcha if using_captcha else TeacherLoginForm OutputLoginForm = LoginFormWithCaptcha if should_use_captcha else TeacherLoginForm login_form = OutputLoginForm(prefix='login') signup_form = TeacherSignupForm(prefix='signup') if request.method == 'POST': if 'login' in request.POST: login_form = InputLoginForm(request.POST, prefix='login') if login_form.is_valid(): user = login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, 'portal/email_verification_needed.html', {'user': user}) login(request, login_form.user) if using_two_factor(request.user): return render(request, 'portal/2FA_redirect.html', { 'form': AuthenticationForm(), 'username': request.user.username, 'password': login_form.cleaned_data['password'], }) else: link = reverse('two_factor:profile') messages.info( request, ("You are not currently set up with two-factor authentication. " + "Use your phone or tablet to enhance your account's security. " + "Click <a href='" + link + "'>here</a> to find out more and " + "set it up or go to your account page at any time."), extra_tags='safe') next_url = request.GET.get('next', None) if next_url: return HttpResponseRedirect(next_url) return HttpResponseRedirect(reverse_lazy('teacher_home')) else: login_form = OutputLoginForm(request.POST, prefix='login') invalid_form = True if 'signup' in request.POST: signup_form = TeacherSignupForm(request.POST, prefix='signup') if signup_form.is_valid(): data = signup_form.cleaned_data teacher = Teacher.objects.factory( title=data['title'], first_name=data['first_name'], last_name=data['last_name'], email=data['email'], password=data['password']) send_verification_email(request, teacher.new_user) return render(request, 'portal/email_verification_needed.html', {'user': teacher.new_user}) logged_in_as_teacher = hasattr(request.user, 'userprofile') and \ hasattr(request.user, 'teacher') and \ (request.user.is_verified() or not using_two_factor(request.user)) res = render(request, 'portal/teach.html', { 'login_form': login_form, 'signup_form': signup_form, 'logged_in_as_teacher': logged_in_as_teacher, }) res.count = invalid_form return res
def play(request): invalid_form = False limits = getattr(request, 'limits', {'ip': [0], 'name': [0]}) ip_captcha_limit = 30 name_captcha_limit = 5 using_captcha = (limits['ip'][0] > ip_captcha_limit or limits['name'][0] >= name_captcha_limit) should_use_captcha = (limits['ip'][0] >= ip_captcha_limit or limits['name'][0] >= name_captcha_limit) StudentLoginFormWithCaptcha = partial( create_form_subclass_with_recaptcha(StudentLoginForm, recaptcha_client), request) InputStudentLoginForm = StudentLoginFormWithCaptcha if using_captcha else StudentLoginForm OutputStudentLoginForm = StudentLoginFormWithCaptcha if should_use_captcha else StudentLoginForm IndependentStudentLoginFormWithCaptcha = partial( create_form_subclass_with_recaptcha(IndependentStudentLoginForm, recaptcha_client), request) InputIndependentStudentLoginForm = IndependentStudentLoginFormWithCaptcha if using_captcha else IndependentStudentLoginForm OutputIndependentStudentLoginForm = IndependentStudentLoginFormWithCaptcha if should_use_captcha else IndependentStudentLoginForm school_login_form = OutputStudentLoginForm(prefix='login') independent_student_login_form = IndependentStudentLoginForm(prefix='independent_student') signup_form = StudentSignupForm(prefix='signup') independent_student_view = False signup_view = False if request.method == 'POST': if 'school_login' in request.POST: school_login_form = InputStudentLoginForm(request.POST, prefix='login') if school_login_form.is_valid(): login(request, school_login_form.user) next_url = request.GET.get('next', None) if next_url: return HttpResponseRedirect(next_url) return HttpResponseRedirect(reverse_lazy('student_details')) else: school_login_form = OutputStudentLoginForm(request.POST, prefix='login') invalid_form = True elif 'independent_student_login' in request.POST: independent_student_login_form = InputIndependentStudentLoginForm(request.POST, prefix='independent_student') if independent_student_login_form.is_valid(): user = independent_student_login_form.user if not is_verified(user): send_verification_email(request, user) return render(request, 'portal/email_verification_needed.html', {'user': user}) login(request, independent_student_login_form.user) next_url = request.GET.get('next', None) if next_url: return HttpResponseRedirect(next_url) return HttpResponseRedirect(reverse_lazy('student_details')) else: independent_student_view = True independent_student_login_form = OutputIndependentStudentLoginForm(request.POST, prefix='independent_student') school_login_form = StudentLoginForm(prefix='login') invalid_form = True elif 'signup' in request.POST: signup_form = StudentSignupForm(request.POST, prefix='signup') if signup_form.is_valid(): data = signup_form.cleaned_data student = Student.objects.independentStudentFactory( username=data['username'], name=data['name'], email=data['email'], password=data['password']) email_supplied = (data['email'] != '') if (email_supplied): send_verification_email(request, student.new_user) return render(request, 'portal/email_verification_needed.html', {'user': student.new_user}) else: # dead code - frontend ensures email supplied. auth_user = authenticate(username=data['username'], password=data['password']) login(request, auth_user) return render(request, 'portal/play/student_details.html') else: signup_view = True res = render(request, 'portal/play.html', { 'school_login_form': school_login_form, 'independent_student_login_form': independent_student_login_form, 'signup_form': signup_form, 'independent_student_view': independent_student_view, 'signup_view': signup_view, }) res.count = invalid_form return res