~homeworkprod/byceps

ref: 533662eefb314d54b168e5a46bc07c88b5f88be4 byceps/byceps/blueprints/admin/terms/views.py -rw-r--r-- 2.6 KiB
533662ee — Jochen Kupperschmidt Use Flask v2.0's `delete`/`get`/`patch`/`post` route shortcuts 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
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
"""
byceps.blueprints.admin.terms.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask import abort

from ....services.terms import consent_service as terms_consent_service
from ....services.terms import document_service as terms_document_service
from ....services.terms import version_service as terms_version_service
from ....services.user import service as user_service
from ....util.authorization import register_permission_enum
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from .authorization import TermsPermission


blueprint = create_blueprint('terms_admin', __name__)


register_permission_enum(TermsPermission)


@blueprint.get('/documents/<document_id>')
@permission_required(TermsPermission.view)
@templated
def view_document(document_id):
    """Show the document's attributes and versions."""
    document = terms_document_service.find_document(document_id)
    if document is None:
        abort(404)

    versions = terms_version_service.get_versions(document.id)

    _add_version_creators(versions)

    consent_counts_by_version_id = (
        terms_consent_service.count_consents_for_document_versions(document.id)
    )

    for version in versions:
        version.consent_count = consent_counts_by_version_id[version.id]

    document = terms_document_service.find_document(document.id)

    return {
        'document': document,
        'versions': versions,
    }


def _add_version_creators(versions):
    creator_ids = {v.snippet_version.creator_id for v in versions}
    creators = user_service.find_users(creator_ids, include_avatars=True)
    creators_by_id = user_service.index_users_by_id(creators)

    for version in versions:
        version.creator = creators_by_id[version.snippet_version.creator_id]


@blueprint.get('/versions/<uuid:version_id>')
@permission_required(TermsPermission.view)
@templated
def view_version(version_id):
    """Show the version."""
    version = _get_version_or_404(version_id)

    return {
        'version': version,
    }


@blueprint.get('/versions/<uuid:version_id>/body.html')
@permission_required(TermsPermission.view)
@templated
def view_version_body_html(version_id):
    """Show the version's HTML body."""
    version = _get_version_or_404(version_id)

    return version.body


def _get_version_or_404(version_id):
    version = terms_version_service.find_version(version_id)

    if version is None:
        abort(404)

    return version