~homeworkprod/byceps

ref: 43bf3630475839afde6e691f0ac561dcb01aaba6 byceps/byceps/blueprints/newsletter/views.py -rw-r--r-- 1.1 KiB
43bf3630 — Jochen Kupperschmidt Upgrade pytest on Travis CI to get v3.3 rather than v3.0 provided by default 4 years 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
"""
byceps.blueprints.newsletter.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask import abort, g

from ...services.newsletter import service as newsletter_service
from ...util.framework.blueprint import create_blueprint
from ...util.framework.flash import flash_success
from ...util.views import respond_no_content


blueprint = create_blueprint('newsletter', __name__)


@blueprint.route('/subscription', methods=['POST'])
@respond_no_content
def subscribe():
    user = _get_current_user_or_404()

    newsletter_service.subscribe(user.id, g.brand_id)

    flash_success('Du hast dich zum Newsletter angemeldet.')


@blueprint.route('/subscription', methods=['DELETE'])
@respond_no_content
def unsubscribe():
    user = _get_current_user_or_404()

    newsletter_service.unsubscribe(user.id, g.brand_id)

    flash_success('Du hast dich vom Newsletter abgemeldet.')


def _get_current_user_or_404():
    user = g.current_user

    if not user.is_active:
        abort(404)

    return user