Passed
Push — main ( 4ee965...bf18b3 )
by Jochen
06:06
created

validate_email_address()   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 7.608

Importance

Changes 0
Metric Value
cc 3
eloc 6
nop 2
dl 0
loc 7
ccs 1
cts 5
cp 0.2
crap 7.608
rs 10
c 0
b 0
f 0
1
"""
2
byceps.blueprints.site.user.settings.forms
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
import re
10
11 1
from flask import g
12 1
from flask_babel import lazy_gettext
13 1
from wtforms import PasswordField, StringField
14 1
from wtforms.fields.html5 import DateField, TelField
15 1
from wtforms.validators import InputRequired, Length, Optional, ValidationError
16
17 1
from .....services.authentication.password import service as password_service
18 1
from .....services.user import screen_name_validator, service as user_service
19 1
from .....util.l10n import LocalizedForm
20
21 1
from ....common.core.forms import ScreenNameValidator
22
23
24 1
EMAIL_ADDRESS_PATTERN = re.compile(r'^[email protected]+?\..+?$')
25
26
27 1
def validate_email_address(form, field):
28
    if EMAIL_ADDRESS_PATTERN.search(field.data) is None:
29
        raise ValidationError(lazy_gettext('Invalid email address'))
30
31
    if user_service.is_email_address_already_assigned(field.data):
32
        raise ValidationError(
33
            lazy_gettext('This email address is not available.')
34
        )
35
36
37 1
class ChangeEmailAddressForm(LocalizedForm):
38 1
    new_email_address = StringField(
39
        lazy_gettext('New email address'),
40
        [InputRequired(), Length(min=6, max=120), validate_email_address],
41
    )
42 1
    password = PasswordField(lazy_gettext('Current password'), [InputRequired()])
43
44 1
    @staticmethod
45
    def validate_password(form, field):
46
        password = field.data
47
48
        if not password_service.is_password_valid_for_user(g.user.id, password):
49
            raise ValidationError(
50
                lazy_gettext('The password does not match the current one.')
51
            )
52
53
54 1
class ChangeScreenNameForm(LocalizedForm):
55 1
    screen_name = StringField(
56
        lazy_gettext('New username'),
57
        [
58
            InputRequired(),
59
            Length(
60
                min=screen_name_validator.MIN_LENGTH,
61
                max=screen_name_validator.MAX_LENGTH,
62
            ),
63
            ScreenNameValidator(),
64
        ],
65
    )
66 1
    password = PasswordField(
67
        lazy_gettext('Current password'), [InputRequired()]
68
    )
69
70 1
    @staticmethod
71
    def validate_screen_name(form, field):
72
        if g.user.screen_name == field.data:
73
            raise ValidationError(
74
                lazy_gettext('This already is the current username.')
75
            )
76
77
        if user_service.is_screen_name_already_assigned(field.data):
78
            raise ValidationError(
79
                lazy_gettext('This username is not available.')
80
            )
81
82 1
    @staticmethod
83
    def validate_password(form, field):
84
        user_id = g.user.id
85
        password = field.data
86
87
        if not password_service.is_password_valid_for_user(user_id, password):
88
            raise ValidationError(lazy_gettext('Wrong password.'))
89
90
91 1
class DetailsForm(LocalizedForm):
92 1
    first_names = StringField(
93
        lazy_gettext('First name(s)'), [InputRequired(), Length(min=2)]
94
    )
95 1
    last_name = StringField(
96
        lazy_gettext('Last name(s)'), [InputRequired(), Length(min=2, max=80)]
97
    )
98 1
    date_of_birth = DateField(lazy_gettext('Date of birth'), [Optional()])
99 1
    country = StringField(lazy_gettext('Country'), [Optional(), Length(max=60)])
100 1
    zip_code = StringField(lazy_gettext('Zip code'), [Optional()])
101 1
    city = StringField(lazy_gettext('City'), [Optional()])
102 1
    street = StringField(lazy_gettext('Street'), [Optional()])
103 1
    phone_number = TelField(
104
        lazy_gettext('Phone number'), [Optional(), Length(max=20)]
105
    )
106