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
|
|
|
login_enabled: bool, |
30
|
|
|
*, |
31
|
|
|
party_id: Optional[PartyID] = None, |
32
|
|
|
) -> Site: |
33
|
|
|
"""Create a site for that party.""" |
34
|
|
|
site = DbSite( |
35
|
|
|
site_id, |
36
|
|
|
title, |
37
|
|
|
server_name, |
38
|
|
|
email_config_id, |
39
|
|
|
enabled, |
40
|
|
|
user_account_creation_enabled, |
41
|
|
|
login_enabled, |
42
|
|
|
party_id=party_id, |
43
|
|
|
) |
44
|
|
|
|
45
|
|
|
db.session.add(site) |
46
|
|
|
db.session.commit() |
47
|
|
|
|
48
|
|
|
return _db_entity_to_site(site) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def update_site( |
52
|
|
|
site_id: SiteID, |
53
|
|
|
title: str, |
54
|
|
|
server_name: str, |
55
|
|
|
email_config_id: str, |
56
|
|
|
party_id: Optional[PartyID], |
57
|
|
|
enabled: bool, |
58
|
|
|
user_account_creation_enabled: bool, |
59
|
|
|
login_enabled: bool, |
60
|
|
|
archived: bool, |
61
|
|
|
) -> Site: |
62
|
|
|
"""Update the site.""" |
63
|
|
|
site = DbSite.query.get(site_id) |
64
|
|
|
|
65
|
|
|
if site is None: |
66
|
|
|
raise UnknownSiteId(site_id) |
67
|
|
|
|
68
|
|
|
site.title = title |
69
|
|
|
site.server_name = server_name |
70
|
|
|
site.email_config_id = email_config_id |
71
|
|
|
site.party_id = party_id |
72
|
|
|
site.enabled = enabled |
73
|
|
|
site.user_account_creation_enabled = user_account_creation_enabled |
74
|
|
|
site.login_enabled = login_enabled |
75
|
|
|
site.archived = archived |
76
|
|
|
|
77
|
|
|
db.session.commit() |
78
|
|
|
|
79
|
|
|
return _db_entity_to_site(site) |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def find_site(site_id: SiteID) -> Optional[Site]: |
83
|
|
|
"""Return the site with that ID, or `None` if not found.""" |
84
|
|
|
site = DbSite.query.get(site_id) |
85
|
|
|
|
86
|
|
|
if site is None: |
87
|
|
|
return None |
88
|
|
|
|
89
|
|
|
return _db_entity_to_site(site) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def get_site(site_id: SiteID) -> Site: |
93
|
|
|
"""Return the site with that ID.""" |
94
|
|
|
site = find_site(site_id) |
95
|
|
|
|
96
|
|
|
if site is None: |
97
|
|
|
raise UnknownSiteId(site_id) |
98
|
|
|
|
99
|
|
|
return site |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
def get_all_sites() -> List[Site]: |
103
|
|
|
"""Return all sites.""" |
104
|
|
|
sites = DbSite.query.all() |
105
|
|
|
|
106
|
|
|
return [_db_entity_to_site(site) for site in sites] |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def get_current_sites() -> List[Site]: |
110
|
|
|
"""Return all "current" (i.e. enabled and not archived) sites.""" |
111
|
|
|
sites = DbSite.query \ |
112
|
|
|
.filter_by(enabled=True) \ |
113
|
|
|
.filter_by(archived=False) \ |
114
|
|
|
.all() |
115
|
|
|
|
116
|
|
|
return [_db_entity_to_site(site) for site in sites] |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
def _db_entity_to_site(site: DbSite) -> Site: |
120
|
|
|
return Site( |
121
|
|
|
site.id, |
122
|
|
|
site.title, |
123
|
|
|
site.server_name, |
124
|
|
|
site.email_config_id, |
125
|
|
|
site.party_id, |
126
|
|
|
site.enabled, |
127
|
|
|
site.user_account_creation_enabled, |
128
|
|
|
site.login_enabled, |
129
|
|
|
site.archived, |
130
|
|
|
) |
131
|
|
|
|