Projet

Général

Profil

0001-registration-provide-a-more-user-friendly-input-code.patch

Paul Marillonnet, 20 décembre 2022 09:55

Télécharger (4,48 ko)

Voir les différences:

Subject: [PATCH] registration: provide a more user-friendly input code form
 (#72604)

 src/authentic2/forms/registration.py                       | 7 ++++++-
 src/authentic2/models.py                                   | 5 ++++-
 .../registration/sms_input_registration_code.html          | 7 +++++++
 src/authentic2/views.py                                    | 5 +++++
 tests/test_registration.py                                 | 2 ++
 5 files changed, 24 insertions(+), 2 deletions(-)
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>
12
      {% blocktrans count counter=duration %}
13
        Your code is valid for the next minute.
14
      {% plural %}
15
        Your code is valid for the next {{ duration }} minutes.
16
      {% endblocktrans %}
17
    </p>
11 18
    {% csrf_token %}
12 19
    {{ form }}
13 20
    <div class="buttons">
src/authentic2/views.py
1252 1252
            return HttpResponseBadRequest(_('Invalid account activation code'))
1253 1253
        return super().dispatch(request, *args, **kwargs)
1254 1254

  
1255
    def get_context_data(self, **kwargs):
1256
        ctx = super().get_context_data(**kwargs)
1257
        ctx['duration'] = models.SMSCode.CODE_DURATION // 60
1258
        return ctx
1259

  
1255 1260
    def post(self, request, *args, **kwargs):
1256 1261
        if 'cancel' in request.POST:
1257 1262
            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
-