def test_expiry(self): code = get_confirmation_code("*****@*****.**") self.assertTrue(code.startswith("[email protected]::")) self.assertEqual(decode(code, max_age=5), ["*****@*****.**", ""]) with self.assertRaises(ValidationError) as cm: time.sleep(2) decode(code, max_age=1) self.assertEqual( cm.exception.messages, ["The link is expired. Please request another registration link."], )
def email_registration(request, *, code=None, registration_form=EmailRegistrationForm, post_login_response=post_login_response, max_age=3600 * 3, email_login=email_login): if code is None: form = registration_form( request.POST if request.method == "POST" else None, request=request) if form.is_valid(): form.send_mail() messages.success(request, _("Please check your mailbox.")) return redirect(".") return render(request, "registration/email_registration.html", {"form": form}) else: try: email, payload = decode(code, max_age=max_age) except ValidationError as exc: [messages.error(request, msg) for msg in exc.messages] return redirect("../") user, created = email_login(request, email=email) if user: return post_login_response(request, new_user=created) messages.error( request, _("No active user with email address %s found.") % email) return redirect("login")
def email_registration( request, code=None, registration_form=EmailRegistrationForm, post_login_response=post_login_response, max_age=3600 * 3, ): User = auth.get_user_model() if code is None: if request.method == "POST": form = registration_form(request.POST, request=request) if form.is_valid(): send_registration_mail( form.cleaned_data["email"], request=request, user=request.user if request.user.is_authenticated else None, # noqa ) messages.success(request, _("Please check your mailbox.")) return redirect(".") else: form = registration_form(request=request) return render(request, "registration/email_registration.html", {"form": form}) else: try: email, _user = decode(code, max_age=max_age) except ValidationError as exc: [messages.error(request, msg) for msg in exc.messages] return redirect("../") _u, new_user = User.objects.get_or_create(email=email) if new_user: messages.success(request, _("Welcome! Please fill in your details.")) user = auth.authenticate(email=email) if user and user.is_active: auth.login(request, user) return post_login_response(request, new_user=new_user)
def custom_verification_code(request, code): email, payload = decode(code, max_age=100) return HttpResponse("email:{} payload:{}".format(email, payload))
def custom_verification_code(request, code): email, payload = decode(code, max_age=100) return HttpResponse(f"email:{email} payload:{payload}")