~homeworkprod/byceps

ref: 3cf502460f3f3efa7c76e1a063ef143254ea377e byceps/byceps/blueprints/user/current/views.py -rw-r--r-- 4.8 KiB
3cf50246 — Jochen Kupperschmidt Rename "site mode" to "app mode" 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
byceps.blueprints.user.current.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, g, jsonify, request, Response

from ....config import get_app_mode
from ....services.country import service as country_service
from ....services.newsletter import service as newsletter_service
from ....services.user import command_service as user_command_service
from ....services.user import service as user_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.views import redirect_to

from ...authentication.decorators import login_required

from ..creation.views import _find_newsletter_list_for_brand
from .. import signals

from .forms import DetailsForm, ChangeScreenNameForm


blueprint = create_blueprint('user_current', __name__)


@blueprint.route('/me')
@login_required
@templated
def view():
    """Show the current user's internal profile."""
    current_user = g.current_user

    user = user_service.find_active_db_user(current_user.id)
    if user is None:
        abort(404)

    if get_app_mode().is_public():
        newsletter_list_id = _find_newsletter_list_for_brand()
        newsletter_offered = (newsletter_list_id is not None)

        subscribed_to_newsletter = newsletter_service.is_subscribed(
            user.id, newsletter_list_id
        )
    else:
        newsletter_list_id = None
        newsletter_offered = False
        subscribed_to_newsletter = None

    return {
        'user': user,
        'newsletter_offered': newsletter_offered,
        'newsletter_list_id': newsletter_list_id,
        'subscribed_to_newsletter': subscribed_to_newsletter,
    }


@blueprint.route('/me.json')
def view_as_json():
    """Show selected attributes of the current user's profile as JSON."""
    if get_app_mode().is_admin():
        abort(404)

    user = g.current_user

    if not user.is_active:
        # Return empty response.
        return Response(status=403)

    return jsonify({
        'id': user.id,
        'screen_name': user.screen_name,
        'avatar_url': user.avatar_url,
    })


@blueprint.route('/me/screen_name')
@templated
def change_screen_name_form(erroneous_form=None):
    """Show a form to change the current user's screen name."""
    _get_current_user_or_404()

    form = erroneous_form if erroneous_form else ChangeScreenNameForm()

    return {
        'form': form,
    }


@blueprint.route('/me/screen_name', methods=['POST'])
def change_screen_name():
    """Change the current user's screen name."""
    current_user = _get_current_user_or_404()

    form = ChangeScreenNameForm(request.form)
    if not form.validate():
        return change_screen_name_form(form)

    old_screen_name = current_user.screen_name
    new_screen_name = form.screen_name.data.strip()
    initiator_id = current_user.id

    event = user_command_service.change_screen_name(
        current_user.id, new_screen_name, initiator_id
    )

    signals.screen_name_changed.send(None, event=event)

    flash_success(f'Dein Benutzername wurde zu "{new_screen_name}" geändert.')

    return redirect_to('.view')


@blueprint.route('/me/details')
@templated
def details_update_form(erroneous_form=None):
    """Show a form to update the current user's details."""
    current_user = _get_current_user_or_404()
    user = user_service.find_user_with_details(current_user.id)

    form = erroneous_form if erroneous_form else DetailsForm(obj=user.detail)
    country_names = country_service.get_country_names()

    return {
        'form': form,
        'country_names': country_names,
    }


@blueprint.route('/me/details', methods=['POST'])
def details_update():
    """Update the current user's details."""
    current_user = _get_current_user_or_404()

    form = DetailsForm(request.form)

    if not form.validate():
        return details_update_form(form)

    first_names = form.first_names.data.strip()
    last_name = form.last_name.data.strip()
    date_of_birth = form.date_of_birth.data
    country = form.country.data.strip()
    zip_code = form.zip_code.data.strip()
    city = form.city.data.strip()
    street = form.street.data.strip()
    phone_number = form.phone_number.data.strip()

    event = user_command_service.update_user_details(
        current_user.id,
        first_names,
        last_name,
        date_of_birth,
        country,
        zip_code,
        city,
        street,
        phone_number,
        current_user.id,  # initiator_id
    )

    flash_success('Deine Daten wurden gespeichert.')

    signals.details_updated.send(None, event=event)

    return redirect_to('.view')


def _get_current_user_or_404():
    user = g.current_user
    if not user.is_active:
        abort(404)

    return user