0001-registration-provide-a-more-user-friendly-input-code.patch
src/authentic2/forms/registration.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
import re |
18 | 18 | |
19 |
from django.conf import settings |
|
19 | 20 |
from django.contrib.auth import get_user_model |
20 | 21 |
from django.contrib.auth.models import BaseUserManager, Group |
21 | 22 |
from django.core.exceptions import ValidationError |
... | ... | |
195 | 196 | |
196 | 197 | |
197 | 198 |
class InputRegistrationCodeForm(Form): |
198 |
registration_code = CharField() |
|
199 |
registration_code = CharField( |
|
200 |
label=_('Registration code'), |
|
201 |
help_text=_('The registration code you received by SMS.'), |
|
202 |
max_length=settings.SMS_CODE_LENGTH, |
|
203 |
) |
src/authentic2/models.py | ||
---|---|---|
815 | 815 | |
816 | 816 | |
817 | 817 |
class SMSCode(models.Model): |
818 |
CODE_DURATION = 120 |
|
818 | 819 |
KIND_REGISTRATION = 'registration' |
819 | 820 |
KIND_PASSWORD_LOST = 'password-lost' |
820 | 821 |
value = models.CharField( |
... | ... | |
838 | 839 |
cls.objects.filter(expires__lte=now).delete() |
839 | 840 | |
840 | 841 |
@classmethod |
841 |
def create(cls, phone, kind=None, expires=None, duration=120):
|
|
842 |
def create(cls, phone, kind=None, expires=None, duration=None):
|
|
842 | 843 |
if not kind: |
843 | 844 |
kind = cls.KIND_REGISTRATION |
845 |
if not duration: |
|
846 |
duration = cls.CODE_DURATION |
|
844 | 847 |
expires = expires or (timezone.now() + datetime.timedelta(seconds=duration)) |
845 | 848 |
return cls.objects.create(kind=kind, phone=phone, expires=expires) |
src/authentic2/templates/registration/sms_input_registration_code.html | ||
---|---|---|
8 | 8 |
{% block content %} |
9 | 9 |
<form method="post" action="."> |
10 | 10 |
<p>{% blocktrans trimmed %}Input your account activation code.{% endblocktrans %}</p> |
11 |
<p>{% blocktrans trimmed with plural=duration|pluralize %}Your code is valid for the next {{ duration }} minute{{ plural }}.{% endblocktrans %}</p> |
|
11 | 12 |
{% csrf_token %} |
12 | 13 |
{{ form }} |
13 | 14 |
<div class="buttons"> |
src/authentic2/views.py | ||
---|---|---|
1251 | 1251 |
return HttpResponseBadRequest(_('Invalid account activation code')) |
1252 | 1252 |
return super().dispatch(request, *args, **kwargs) |
1253 | 1253 | |
1254 |
def get_context_data(self, **kwargs): |
|
1255 |
ctx = super().get_context_data(**kwargs) |
|
1256 |
ctx['duration'] = models.SMSCode.CODE_DURATION // 60 |
|
1257 |
return ctx |
|
1258 | ||
1254 | 1259 |
def post(self, request, *args, **kwargs): |
1255 | 1260 |
if 'cancel' in request.POST: |
1256 | 1261 |
self.code.delete() |
tests/test_registration.py | ||
---|---|---|
1072 | 1072 |
assert body['message'].startswith('Your code is') |
1073 | 1073 |
code = SMSCode.objects.get() |
1074 | 1074 |
assert body['message'][-code_length:] == code.value |
1075 |
assert ("Your code is valid for the next %s minute" % (SMSCode.CODE_DURATION // 60)) in resp.text |
|
1076 |
assert "The registration code you received by SMS." in resp.text |
|
1075 | 1077 |
resp.form.set('registration_code', code.value) |
1076 | 1078 |
resp = resp.form.submit().follow() |
1077 | 1079 |
assert Token.objects.count() == 1 |
1078 |
- |