Completed
Push — master ( f10ede...2cdad2 )
by Jochen
04:07
created

byceps.services.site.service.get_enabled_sites()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
"""
2
byceps.services.site.service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2019 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9
from typing import List, Optional
10
11
from ...database import db
12
from ...typing import PartyID
13
14
from .models.site import Site as DbSite
15
from .transfer.models import Site, SiteID
16
17
18
class UnknownSiteId(Exception):
19
    pass
20
21
22
def create_site(
23
    site_id: SiteID,
24
    title: str,
25
    server_name: str,
26
    email_config_id: str,
27
    enabled: bool,
28
    user_account_creation_enabled: bool,
29
    *,
30
    party_id: Optional[PartyID] = None,
31
) -> Site:
32
    """Create a site for that party."""
33
    site = DbSite(
34
        site_id,
35
        title,
36
        server_name,
37
        email_config_id,
38
        enabled,
39
        user_account_creation_enabled,
40
        party_id=party_id,
41
    )
42
43
    db.session.add(site)
44
    db.session.commit()
45
46
    return _db_entity_to_site(site)
47
48
49
def update_site(
50
    site_id: SiteID,
51
    title: str,
52
    server_name: str,
53
    email_config_id: str,
54
    party_id: Optional[PartyID],
55
    enabled: bool,
56
    user_account_creation_enabled: bool,
57
) -> Site:
58
    """Update the site."""
59
    site = DbSite.query.get(site_id)
60
61
    if site is None:
62
        raise UnknownSiteId(site_id)
63
64
    site.title = title
65
    site.server_name = server_name
66
    site.email_config_id = email_config_id
67
    site.party_id = party_id
68
    site.enabled = enabled
69
    site.user_account_creation_enabled = user_account_creation_enabled
70
71
    db.session.commit()
72
73
    return _db_entity_to_site(site)
74
75
76
def find_site(site_id: SiteID) -> Optional[Site]:
77
    """Return the site with that ID, or `None` if not found."""
78
    site = DbSite.query.get(site_id)
79
80
    if site is None:
81
        return None
82
83
    return _db_entity_to_site(site)
84
85
86
def get_site(site_id: SiteID) -> Site:
87
    """Return the site with that ID."""
88
    site = find_site(site_id)
89
90
    if site is None:
91
        raise UnknownSiteId(site_id)
92
93
    return site
94
95
96
def get_all_sites() -> List[Site]:
97
    """Return all sites."""
98
    sites = DbSite.query.all()
99
100
    return [_db_entity_to_site(site) for site in sites]
101
102
103
def get_enabled_sites() -> List[Site]:
104
    """Return all enabled sites."""
105
    sites = DbSite.query \
106
        .filter_by(enabled=True) \
107
        .all()
108
109
    return [_db_entity_to_site(site) for site in sites]
110
111
112
def _db_entity_to_site(site: DbSite) -> Site:
113
    return Site(
114
        site.id,
115
        site.title,
116
        site.server_name,
117
        site.email_config_id,
118
        site.party_id,
119
        site.enabled,
120
        site.user_account_creation_enabled,
121
    )
122