~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/site/newsletter/views.py -rw-r--r-- 1.5 KiB
342e5296 — Jochen Kupperschmidt Require body blocks in base templates 2 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
"""
byceps.blueprints.site.newsletter.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from datetime import datetime

from flask import abort, g
from flask_babel import gettext

from ....services.newsletter import command_service, service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.views import login_required, respond_no_content


blueprint = create_blueprint('newsletter', __name__)


@blueprint.route('/lists/<list_id>/subscription', methods=['POST'])
@login_required
@respond_no_content
def subscribe(list_id):
    list_ = _get_list_or_404(list_id)
    expressed_at = datetime.utcnow()

    command_service.subscribe(g.user.id, list_.id, expressed_at)

    flash_success(
        gettext(
            'You have subscribed to newsletter "%(title)s".',
            title=list_.title,
        )
    )


@blueprint.route('/lists/<list_id>/subscription', methods=['DELETE'])
@login_required
@respond_no_content
def unsubscribe(list_id):
    list_ = _get_list_or_404(list_id)
    expressed_at = datetime.utcnow()

    command_service.unsubscribe(g.user.id, list_.id, expressed_at)

    flash_success(
        gettext(
            'You have unsubscribed from newsletter "%(title)s".',
            title=list_.title,
        )
    )


def _get_list_or_404(list_id):
    list_ = service.find_list(list_id)

    if list_ is None:
        abort(404)

    return list_