Passed
Push — main ( 87d4f4...869e7e )
by Jochen
04:23
created

byceps.blueprints.admin.consent.views   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 81
ccs 24
cts 36
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 5

3 Functions

Rating   Name   Duplication   Size   Complexity  
A subject_create_form() 0 9 2
A subject_create() 0 24 2
A index() 0 13 1
1
"""
2
byceps.blueprints.admin.consent.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask import request
10 1
from flask_babel import gettext
11
12 1
from ....services.consent import subject_service
13 1
from ....util.authorization import register_permission_enum
14 1
from ....util.framework.blueprint import create_blueprint
15 1
from ....util.framework.flash import flash_success
16 1
from ....util.framework.templating import templated
17 1
from ....util.views import permission_required, redirect_to
18
19 1
from .authorization import ConsentPermission
20 1
from .forms import SubjectCreateForm
21
22
23 1
blueprint = create_blueprint('consent_admin', __name__)
24
25
26 1
register_permission_enum(ConsentPermission)
27
28
29 1
@blueprint.get('/')
30 1
@permission_required(ConsentPermission.administrate)
31 1
@templated
32
def index():
33
    """List consent subjects."""
34 1
    subjects_with_consent_counts = (
35
        subject_service.get_subjects_with_consent_counts()
36
    )
37
38 1
    subjects_with_consent_counts = list(subjects_with_consent_counts.items())
39
40 1
    return {
41
        'subjects_with_consent_counts': subjects_with_consent_counts,
42
    }
43
44
45 1
@blueprint.get('/consents/create')
46 1
@permission_required(ConsentPermission.administrate)
47 1
@templated
48 1
def subject_create_form(erroneous_form=None):
49
    """Show form to create a consent subject."""
50
    form = erroneous_form if erroneous_form else SubjectCreateForm()
51
52
    return {
53
        'form': form,
54
    }
55
56
57 1
@blueprint.post('/consents')
58 1
@permission_required(ConsentPermission.administrate)
59
def subject_create():
60
    """Create a consent subject."""
61
    form = SubjectCreateForm(request.form)
62
    if not form.validate():
63
        return subject_create_form(form)
64
65
    subject_name = form.subject_name.data.strip()
66
    subject_title = form.subject_title.data.strip()
67
    checkbox_label = form.checkbox_label.data.strip()
68
    checkbox_link_target = form.checkbox_link_target.data.strip()
69
70
    subject = subject_service.create_subject(
71
        subject_name, subject_title, checkbox_label, checkbox_link_target
72
    )
73
74
    flash_success(
75
        gettext(
76
            'Consent subject "%(title)s" has been created.', title=subject.title
77
        )
78
    )
79
80
    return redirect_to('.index')
81