~homeworkprod/byceps

ref: a3214d27456b4060727966ca1ad520a92edaa6b2 byceps/scripts/create_terms_version.py -rwxr-xr-x 2.2 KiB
a3214d27 — Jochen Kupperschmidt Make tourney avatar path depend on party path, but non-configurable besides that 2 years 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
#!/usr/bin/env python

"""Create a new terms of service version.

However, do not set the new version as the current version for the brand.

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

import click

from byceps.services.consent import subject_service as consent_subject_service
from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import SnippetVersionID
from byceps.services.terms import document_service as terms_document_service
from byceps.services.terms.transfer.models import DocumentID
from byceps.services.terms import version_service as terms_version_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_brand


def validate_document_id(ctx, param, value) -> DocumentID:
    document = terms_document_service.find_document(value)

    if not document:
        raise click.BadParameter(f'Unknown document ID "{value}".')

    return document.id


def validate_snippet_version_id(ctx, param, value) -> SnippetVersionID:
    snippet_version = snippet_service.find_snippet_version(value)

    if not snippet_version:
        raise click.BadParameter(f'Unknown snippet_version ID "{value}".')

    return snippet_version.id


@click.command()
@click.argument('brand', callback=validate_brand)
@click.argument('document_id', callback=validate_document_id)
@click.argument('title')
@click.argument('snippet_version_id', callback=validate_snippet_version_id)
@click.argument('consent_subject_name_suffix')
def execute(
    brand, document_id, title, snippet_version_id, consent_subject_name_suffix
):
    consent_subject_name = (
        f'{brand.id}_terms-of-service_{consent_subject_name_suffix}'
    )

    consent_subject_title = f'AGB {brand.title} / {title}'

    consent_subject = consent_subject_service.create_subject(
        consent_subject_name, consent_subject_title, 'terms_of_service'
    )

    terms_version_service.create_version(
        document_id, title, snippet_version_id, consent_subject.id
    )

    click.secho('Done.', fg='green')


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()