1
|
|
|
""" |
2
|
|
|
byceps.blueprints.newsletter.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from datetime import datetime |
10
|
|
|
|
11
|
|
|
from flask import abort, g |
12
|
|
|
|
13
|
|
|
from ...services.newsletter import command_service, service |
14
|
|
|
from ...util.framework.blueprint import create_blueprint |
15
|
|
|
from ...util.framework.flash import flash_success |
16
|
|
|
from ...util.views import respond_no_content |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
blueprint = create_blueprint('newsletter', __name__) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
@blueprint.route('/lists/<list_id>/subscription', methods=['POST']) |
23
|
|
|
@respond_no_content |
24
|
|
|
def subscribe(list_id): |
25
|
|
|
list_ = _get_list_or_404(list_id) |
26
|
|
|
user = _get_current_user_or_404() |
27
|
|
|
expressed_at = datetime.utcnow() |
28
|
|
|
|
29
|
|
|
command_service.subscribe(user.id, list_.id, expressed_at) |
30
|
|
|
|
31
|
|
|
flash_success(f'Du hast dich zum Newsletter "{list_.title}" angemeldet.') |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
@blueprint.route('/lists/<list_id>/subscription', methods=['DELETE']) |
35
|
|
|
@respond_no_content |
36
|
|
|
def unsubscribe(list_id): |
37
|
|
|
list_ = _get_list_or_404(list_id) |
38
|
|
|
user = _get_current_user_or_404() |
39
|
|
|
expressed_at = datetime.utcnow() |
40
|
|
|
|
41
|
|
|
command_service.unsubscribe(user.id, list_.id, expressed_at) |
42
|
|
|
|
43
|
|
|
flash_success(f'Du hast dich vom Newsletter "{list_.title}" abgemeldet.') |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def _get_current_user_or_404(): |
47
|
|
|
user = g.current_user |
48
|
|
|
|
49
|
|
|
if not user.is_active: |
50
|
|
|
abort(404) |
51
|
|
|
|
52
|
|
|
return user |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def _get_list_or_404(list_id): |
56
|
|
|
list_ = service.find_list(list_id) |
57
|
|
|
|
58
|
|
|
if list_ is None: |
59
|
|
|
abort(404) |
60
|
|
|
|
61
|
|
|
return list_ |
62
|
|
|
|