Passed
Push — main ( 7e9c56...8b0189 )
by Jochen
03:34
created

create_terms_version._create_consent_subject()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
"""Create a new terms of service version.
4
5
However, do not set the new version as the current version for the brand.
6
7
:Copyright: 2006-2020 Jochen Kupperschmidt
8
:License: Modified BSD, see LICENSE for details.
9
"""
10
11
import click
12
13
from byceps.services.consent import subject_service as consent_subject_service
14
from byceps.services.snippet import service as snippet_service
15
from byceps.services.snippet.transfer.models import SnippetVersionID
16
from byceps.services.terms import document_service as terms_document_service
17
from byceps.services.terms.transfer.models import DocumentID
18
from byceps.services.terms import version_service as terms_version_service
19
from byceps.util.system import get_config_filename_from_env_or_exit
20
21
from _util import app_context
22
from _validators import validate_brand
23
24
25
def validate_document_id(ctx, param, value) -> DocumentID:
26
    document = terms_document_service.find_document(value)
27
28
    if not document:
29
        raise click.BadParameter(f'Unknown document ID "{value}".')
30
31
    return document.id
32
33
34
def validate_snippet_version_id(ctx, param, value) -> SnippetVersionID:
35
    snippet_version = snippet_service.find_snippet_version(value)
36
37
    if not snippet_version:
38
        raise click.BadParameter(f'Unknown snippet_version ID "{value}".')
39
40
    return snippet_version.id
41
42
43
@click.command()
44
@click.argument('brand', callback=validate_brand)
45
@click.argument('document_id', callback=validate_document_id)
46
@click.argument('title')
47
@click.argument('snippet_version_id', callback=validate_snippet_version_id)
48
@click.argument('consent_subject_name_suffix')
49
def execute(
50
    brand, document_id, title, snippet_version_id, consent_subject_name_suffix
51
):
52
    consent_subject = _create_consent_subject(
53
        brand, title, consent_subject_name_suffix
54
    )
55
56
    terms_version_service.create_version(
57
        document_id, title, snippet_version_id, consent_subject.id
58
    )
59
60
    click.secho('Done.', fg='green')
61
62
63
def _create_consent_subject(brand, title, consent_subject_name_suffix):
64
    subject_name = f'{brand.id}_terms-of-service_{consent_subject_name_suffix}'
65
    subject_title = f'AGB {brand.title} / {title}'
66
    type_ = 'terms_of_service'
67
    checkbox_label = 'Ich akzeptiere die <a href="{url}" target="_blank">Allgemeinen Geschäftsbedingungen</a>'
68
    checkbox_link_target = '/terms/'
69
70
    return consent_subject_service.create_subject(
71
        subject_name, subject_title, type_, checkbox_label, checkbox_link_target
72
    )
73
74
75
if __name__ == '__main__':
76
    config_filename = get_config_filename_from_env_or_exit()
77
    with app_context(config_filename):
78
        execute()
79