validate_user()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 35.2661

Importance

Changes 0
Metric Value
cc 6
eloc 22
nop 2
dl 0
loc 37
ccs 1
cts 15
cp 0.0667
crap 35.2661
rs 8.4186
c 0
b 0
f 0
1
"""
2
byceps.blueprints.site.ticketing.forms
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2014-2024 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask import g
10 1
from flask_babel import gettext, lazy_gettext
11 1
from wtforms import StringField
12 1
from wtforms.validators import InputRequired, ValidationError
13
14 1
from byceps.services.consent import consent_service, consent_subject_service
15 1
from byceps.services.user import user_service
16 1
from byceps.util.l10n import LocalizedForm
17
18
19 1
def validate_user(form, field):
20
    screen_name = field.data.strip()
21
22
    db_user = user_service.find_db_user_by_screen_name(
23
        screen_name, case_insensitive=True
24
    )
25
26
    if db_user is None:
27
        raise ValidationError(gettext('Unknown username'))
28
29
    user = user_service.get_user(db_user.id)
30
31
    if not user.initialized:
32
        raise ValidationError(gettext('The user account is not active.'))
33
34
    if user.suspended or user.deleted:
35
        raise ValidationError(gettext('The user account is not active.'))
36
37
    required_consent_subjects = (
38
        consent_subject_service.get_subjects_required_for_brand(g.brand_id)
39
    )
40
    required_consent_subject_ids = {
41
        subject.id for subject in required_consent_subjects
42
    }
43
44
    if not consent_service.has_user_consented_to_all_subjects(
45
        user.id, required_consent_subject_ids
46
    ):
47
        raise ValidationError(
48
            gettext(
49
                'User "%(screen_name)s" has not yet given all necessary '
50
                'consents. Logging in again is required.',
51
                screen_name=user.screen_name,
52
            )
53
        )
54
55
    field.data = user
56
57
58 1
class SpecifyUserForm(LocalizedForm):
59 1
    user = StringField(
60
        lazy_gettext('Username'), [InputRequired(), validate_user]
61
    )
62