~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/terms/document_service.py -rw-r--r-- 2.0 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 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
"""
byceps.services.terms.document_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from typing import Optional

from ...database import db
from ...typing import BrandID

from ..brand import settings_service as brand_settings_service

from .models.document import Document as DbDocument
from .models.version import Version as DbVersion
from .transfer.models import Document, DocumentID, VersionID


def create_document(document_id: DocumentID, title: str) -> Document:
    """Create a terms of service document."""
    document = DbDocument(document_id, title)

    db.session.add(document)
    db.session.commit()

    return _db_entity_to_document(document)


def find_document(document_id: DocumentID) -> Optional[Document]:
    """Return the document with that ID, or `None` if not found."""
    document = DbDocument.query.get(document_id)

    if document is None:
        return None

    return _db_entity_to_document(document)


def set_current_version(document_id: DocumentID, version_id: VersionID) -> None:
    """Specify the current version of the document."""
    document = DbDocument.query.get(document_id)
    if document is None:
        raise ValueError(
            f'Unknown terms of service document ID "{document_id}"'
        )

    version = DbVersion.query.get(version_id)
    if version is None:
        raise ValueError(f'Unknown terms of service version ID "{version_id}"')

    document.current_version_id = version.id
    db.session.commit()


def find_document_id_for_brand(brand_id: BrandID) -> Optional[DocumentID]:
    """Return the document ID configured for the brand, or `None` if
    none is configured.
    """
    setting_name = 'terms_document_id'
    return brand_settings_service.find_setting_value(brand_id, setting_name)


def _db_entity_to_document(document: DbDocument) -> Document:
    return Document(
        document.id,
        document.title,
        document.current_version_id,
    )