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

change_email_address()   A

Complexity

Conditions 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.048

Importance

Changes 0
Metric Value
cc 2
eloc 17
nop 0
dl 0
loc 28
ccs 2
cts 10
cp 0.2
crap 4.048
rs 9.55
c 0
b 0
f 0
1
"""
2
byceps.blueprints.site.user.settings.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from typing import Optional
10
11 1
from flask import abort, g, request
12 1
from flask_babel import gettext
13
14 1
from .....services.brand import settings_service as brand_settings_service
15 1
from .....services.country import service as country_service
16 1
from .....services.newsletter import service as newsletter_service
17 1
from .....services.newsletter.transfer.models import ListID as NewsletterListID
18 1
from .....services.user import (
19
    command_service as user_command_service,
20
    email_address_verification_service,
21
    service as user_service,
22
)
23 1
from .....signals import user as user_signals
24 1
from .....util.framework.blueprint import create_blueprint
25 1
from .....util.framework.flash import flash_success
26 1
from .....util.framework.templating import templated
27 1
from .....util.views import login_required, redirect_to
28
29 1
from .forms import DetailsForm, ChangeEmailAddressForm, ChangeScreenNameForm
30
31
32 1
blueprint = create_blueprint('user_settings', __name__)
33
34
35 1
@blueprint.get('')
36 1
@login_required
37 1
@templated
38
def view():
39
    """Show the current user's internal profile."""
40 1
    user = user_service.find_active_db_user(g.user.id)
41 1
    if user is None:
42
        abort(404)
43
44 1
    newsletter_list_id = _find_newsletter_list_for_brand()
45 1
    newsletter_offered = newsletter_list_id is not None
46
47 1
    subscribed_to_newsletter = newsletter_service.is_subscribed(
48
        user.id, newsletter_list_id
49
    )
50
51 1
    return {
52
        'user': user,
53
        'newsletter_offered': newsletter_offered,
54
        'newsletter_list_id': newsletter_list_id,
55
        'subscribed_to_newsletter': subscribed_to_newsletter,
56
    }
57
58
59 1
@blueprint.get('/email_address')
60 1
@login_required
61 1
@templated
62 1
def change_email_address_form(erroneous_form=None):
63
    """Show a form to change the current user's email address."""
64
    form = erroneous_form if erroneous_form else ChangeEmailAddressForm()
65
66
    return {
67
        'form': form,
68
    }
69
70
71 1
@login_required
72 1
@blueprint.post('/email_address')
73
def change_email_address():
74
    """Request a change of the current user's email address."""
75
    current_user = g.user
76
77
    form = ChangeEmailAddressForm(request.form)
78
    if not form.validate():
79
        return change_email_address_form(form)
80
81
    new_email_address = form.new_email_address.data.strip()
82
83
    email_address_verification_service.send_email_address_change_email(
84
        new_email_address,
85
        current_user.screen_name,
86
        current_user.id,
87
        g.site_id,
88
    )
89
90
    flash_success(
91
        gettext(
92
            'An email with a verification link has been sent to your new '
93
            'address. The email address of your account will be changed '
94
            'to the new address once you visit the link to verify it.'
95
        )
96
    )
97
98
    return redirect_to('.view')
99
100
101 1
@blueprint.get('/screen_name')
102 1
@login_required
103 1
@templated
104 1
def change_screen_name_form(erroneous_form=None):
105
    """Show a form to change the current user's screen name."""
106
    form = erroneous_form if erroneous_form else ChangeScreenNameForm()
107
108
    return {
109
        'form': form,
110
    }
111
112
113 1
@login_required
114 1
@blueprint.post('/screen_name')
115
def change_screen_name():
116
    """Change the current user's screen name."""
117
    current_user = g.user
118
119
    form = ChangeScreenNameForm(request.form)
120
    if not form.validate():
121
        return change_screen_name_form(form)
122
123
    old_screen_name = current_user.screen_name
124
    new_screen_name = form.screen_name.data.strip()
125
    initiator_id = current_user.id
126
127
    event = user_command_service.change_screen_name(
128
        current_user.id, new_screen_name, initiator_id
129
    )
130
131
    user_signals.screen_name_changed.send(None, event=event)
132
133
    flash_success(
134
        gettext(
135
            'Your username has been changed to "%(new_screen_name)s".',
136
            new_screen_name=new_screen_name,
137
        )
138
    )
139
140
    return redirect_to('.view')
141
142
143 1
@blueprint.get('/details')
144 1
@login_required
145 1
@templated
146 1
def details_update_form(erroneous_form=None):
147
    """Show a form to update the current user's details."""
148
    user = user_service.find_user_with_details(g.user.id)
149
150
    form = erroneous_form if erroneous_form else DetailsForm(obj=user.detail)
151
    country_names = country_service.get_country_names()
152
153
    return {
154
        'form': form,
155
        'country_names': country_names,
156
    }
157
158
159 1
@blueprint.post('/details')
160 1
@login_required
161
def details_update():
162
    """Update the current user's details."""
163
    current_user = g.user
164
165
    form = DetailsForm(request.form)
166
167
    if not form.validate():
168
        return details_update_form(form)
169
170
    first_names = form.first_names.data.strip()
171
    last_name = form.last_name.data.strip()
172
    date_of_birth = form.date_of_birth.data
173
    country = form.country.data.strip()
174
    zip_code = form.zip_code.data.strip()
175
    city = form.city.data.strip()
176
    street = form.street.data.strip()
177
    phone_number = form.phone_number.data.strip()
178
179
    event = user_command_service.update_user_details(
180
        current_user.id,
181
        first_names,
182
        last_name,
183
        date_of_birth,
184
        country,
185
        zip_code,
186
        city,
187
        street,
188
        phone_number,
189
        current_user.id,  # initiator_id
190
    )
191
192
    flash_success(gettext('Your data has been saved.'))
193
194
    user_signals.details_updated.send(None, event=event)
195
196
    return redirect_to('.view')
197
198
199 1
def _find_newsletter_list_for_brand() -> Optional[NewsletterListID]:
200
    """Return the newsletter list configured for this brand, or `None`
201
    if none is configured.
202
    """
203 1
    value = brand_settings_service.find_setting_value(
204
        g.brand_id, 'newsletter_list_id'
205
    )
206
207 1
    if not value:
208 1
        return None
209
210
    return NewsletterListID(value)
211