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

change()   B

Complexity

Conditions 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.8142

Importance

Changes 0
Metric Value
cc 7
eloc 21
nop 1
dl 0
loc 32
ccs 12
cts 18
cp 0.6667
crap 8.8142
rs 7.9759
c 0
b 0
f 0
1
"""
2
byceps.blueprints.site.user.email_address.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask import abort, g, request
10 1
from flask_babel import gettext
11
12 1
from .....services.user import (
13
    command_service as user_command_service,
14
    email_address_verification_service,
15
    service as user_service,
16
)
17 1
from .....services.verification_token import (
18
    service as verification_token_service,
19
)
20 1
from .....signals import user as user_signals
21 1
from .....util.framework.blueprint import create_blueprint
22 1
from .....util.framework.flash import flash_error, flash_notice, flash_success
23 1
from .....util.framework.templating import templated
24 1
from .....util.views import redirect_to
25
26 1
from .forms import RequestConfirmationEmailForm
27
28
29 1
blueprint = create_blueprint('user_email_address', __name__)
30
31
32 1
@blueprint.get('/confirmation_email/request')
33 1
@templated
34 1
def request_confirmation_email_form(erroneous_form=None):
35
    """Show a form to request the email address confirmation email for the user
36
    account again.
37
    """
38
    form = erroneous_form if erroneous_form else RequestConfirmationEmailForm()
39
    return {'form': form}
40
41
42 1
@blueprint.post('/confirmation_email/request')
43
def request_confirmation_email():
44
    """Request the email address confirmation email for the user account
45
    again.
46
    """
47
    form = RequestConfirmationEmailForm(request.form)
48
    if not form.validate():
49
        return request_confirmation_email_form(form)
50
51
    screen_name = form.screen_name.data.strip()
52
    user = user_service.find_user_by_screen_name(
53
        screen_name, case_insensitive=True
54
    )
55
56
    if (user is None) or user.deleted:
57
        flash_error(
58
            gettext(
59
                'Username "%(screen_name)s" is unknown.',
60
                screen_name=screen_name,
61
            )
62
        )
63
        return request_confirmation_email_form(form)
64
65
    if user.email_address is None:
66
        flash_error(
67
            gettext(
68
                'No email address is set for user "%(screen_name)s".',
69
                screen_name=screen_name,
70
            )
71
        )
72
        return request_confirmation_email_form(form)
73
74
    if user.email_address_verified:
75
        flash_notice(
76
            gettext(
77
                'The email address for user "%(screen_name)s" has already been verified.',
78
                screen_name=user.screen_name,
79
            )
80
        )
81
        return request_confirmation_email_form()
82
83
    if user.suspended:
84
        flash_error(
85
            gettext(
86
                'User "%(screen_name)s" has been suspended.',
87
                screen_name=screen_name,
88
            )
89
        )
90
        return request_confirmation_email_form()
91
92
    email_address_verification_service.send_email_address_confirmation_email(
93
        user.email_address, user.screen_name, user.id, g.site_id
94
    )
95
96
    flash_success(
97
        gettext(
98
            'The link to verify the email address for user "%(screen_name)s" '
99
            'has been sent again.',
100
            screen_name=user.screen_name,
101
        )
102
    )
103
104
    return redirect_to('.request_confirmation_email_form')
105
106
107 1
@blueprint.get('/confirmation/<token>')
108
def confirm(token):
109
    """Confirm e-mail address of the user account assigned with the
110
    verification token.
111
    """
112 1
    verification_token = (
113
        verification_token_service.find_for_email_address_confirmation_by_token(
114
            token
115
        )
116
    )
117 1
    if verification_token is None:
118 1
        abort(404)
119
120 1
    user = user_service.get_db_user(verification_token.user_id)
121 1
    if (user is None) or user.suspended or user.deleted:
122
        flash_error(gettext('No valid token specified.'))
123
        abort(404)
124
125 1
    try:
126 1
        event = email_address_verification_service.confirm_email_address(
127
            verification_token
128
        )
129 1
    except email_address_verification_service.EmailAddressConfirmationFailed as e:
130 1
        flash_error(gettext('Email address verification failed.'))
131 1
        return redirect_to('authentication_login.login_form')
132
133 1
    flash_success(gettext('Email address has been verified.'))
134
135 1
    if not user.initialized:
136 1
        user_command_service.initialize_account(user.id)
137 1
        flash_success(
138
            gettext(
139
                'User "%(screen_name)s" has been activated.',
140
                screen_name=user.screen_name,
141
            )
142
        )
143
144 1
    user_signals.email_address_confirmed.send(None, event=event)
145
146 1
    return redirect_to('authentication_login.login_form')
147
148
149 1
@blueprint.get('/change/<token>')
150
def change(token):
151
    """Confirm and change e-mail address of the user account assigned
152
    with the verification token.
153
    """
154 1
    verification_token = (
155
        verification_token_service.find_for_email_address_change_by_token(token)
156
    )
157 1
    if verification_token is None:
158 1
        abort(404)
159
160 1
    user = user_service.get_db_user(verification_token.user_id)
161 1
    if (user is None) or user.suspended or user.deleted:
162
        flash_error(gettext('No valid token specified.'))
163
        abort(404)
164
165 1
    try:
166 1
        event = email_address_verification_service.change_email_address(
167
            verification_token
168
        )
169
    except email_address_verification_service.EmailAddressChangeFailed as e:
170
        flash_error(gettext('Email address change failed.'))
171
        return redirect_to('authentication_login.login_form')
172
173 1
    flash_success(gettext('Email address has been changed.'))
174
175 1
    user_signals.email_address_changed.send(None, event=event)
176
177 1
    if g.user.authenticated:
178
        return redirect_to('user_settings.view')
179
    else:
180
        return redirect_to('authentication_login.login_form')
181