~homeworkprod/byceps

ref: bcef32cd2dc329bcf81057794883008f5c040ac8 byceps/byceps/services/site/service.py -rw-r--r-- 4.8 KiB
bcef32cd — Jochen Kupperschmidt Work around Jinja 3.0.0 bug with `for` inside of `set` block 8 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
byceps.services.site.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from __future__ import annotations
import dataclasses
from typing import Optional, Union

from ...database import db
from ...typing import BrandID, PartyID

from ..board.transfer.models import BoardID
from ..brand import service as brand_service
from ..news.transfer.models import ChannelID as NewsChannelID
from ..shop.storefront.transfer.models import StorefrontID

from .dbmodels.site import Site as DbSite
from .dbmodels.setting import Setting as DbSetting
from .transfer.models import Site, SiteID, SiteWithBrand


class UnknownSiteId(Exception):
    pass


def create_site(
    site_id: SiteID,
    title: str,
    server_name: str,
    brand_id: BrandID,
    *,
    enabled: bool = False,
    user_account_creation_enabled: bool = False,
    login_enabled: bool = False,
    party_id: Optional[PartyID] = None,
    news_channel_id: Optional[NewsChannelID] = None,
    board_id: Optional[BoardID] = None,
    storefront_id: Optional[StorefrontID] = None,
) -> Site:
    """Create a site for that party."""
    site = DbSite(
        site_id,
        title,
        server_name,
        brand_id,
        enabled,
        user_account_creation_enabled,
        login_enabled,
        party_id=party_id,
        news_channel_id=news_channel_id,
        board_id=board_id,
        storefront_id=storefront_id,
    )

    db.session.add(site)
    db.session.commit()

    return _db_entity_to_site(site)


def update_site(
    site_id: SiteID,
    title: str,
    server_name: str,
    brand_id: BrandID,
    party_id: Optional[PartyID],
    enabled: bool,
    user_account_creation_enabled: bool,
    login_enabled: bool,
    news_channel_id: Optional[NewsChannelID],
    board_id: Optional[BoardID],
    storefront_id: Optional[StorefrontID],
    archived: bool,
) -> Site:
    """Update the site."""
    site = DbSite.query.get(site_id)

    if site is None:
        raise UnknownSiteId(site_id)

    site.title = title
    site.server_name = server_name
    site.brand_id = brand_id
    site.party_id = party_id
    site.enabled = enabled
    site.user_account_creation_enabled = user_account_creation_enabled
    site.login_enabled = login_enabled
    site.news_channel_id = news_channel_id
    site.board_id = board_id
    site.storefront_id = storefront_id
    site.archived = archived

    db.session.commit()

    return _db_entity_to_site(site)


def delete_site(site_id: SiteID) -> None:
    """Delete a site."""
    db.session.query(DbSetting) \
        .filter_by(site_id=site_id) \
        .delete()

    db.session.query(DbSite) \
        .filter_by(id=site_id) \
        .delete()

    db.session.commit()


def find_site(site_id: SiteID) -> Optional[Site]:
    """Return the site with that ID, or `None` if not found."""
    site = DbSite.query.get(site_id)

    if site is None:
        return None

    return _db_entity_to_site(site)


def get_site(site_id: SiteID) -> Site:
    """Return the site with that ID."""
    site = find_site(site_id)

    if site is None:
        raise UnknownSiteId(site_id)

    return site


def get_all_sites() -> set[Site]:
    """Return all sites."""
    sites = DbSite.query.all()

    return {_db_entity_to_site(site) for site in sites}


def get_sites_for_brand(brand_id: BrandID) -> set[Site]:
    """Return the sites for that brand."""
    sites = DbSite.query \
        .filter_by(brand_id=brand_id) \
        .all()

    return {_db_entity_to_site(site) for site in sites}


def get_current_sites(
    brand_id: Optional[BrandID] = None, *, include_brands: bool = False
) -> set[Union[Site, SiteWithBrand]]:
    """Return all "current" (i.e. enabled and not archived) sites."""
    query = DbSite.query

    if brand_id is not None:
        query = query.filter_by(brand_id=brand_id)

    if include_brands:
        query = query.options(db.joinedload('brand'))

    sites = query \
        .filter_by(enabled=True) \
        .filter_by(archived=False) \
        .all()

    if include_brands:
        transform = _db_entity_to_site_with_brand
    else:
        transform = _db_entity_to_site

    return {transform(site) for site in sites}


def _db_entity_to_site(site: DbSite) -> Site:
    return Site(
        site.id,
        site.title,
        site.server_name,
        site.brand_id,
        site.party_id,
        site.enabled,
        site.user_account_creation_enabled,
        site.login_enabled,
        site.news_channel_id,
        site.board_id,
        site.storefront_id,
        site.archived,
    )


def _db_entity_to_site_with_brand(site_entity: DbSite) -> SiteWithBrand:
    site = _db_entity_to_site(site_entity)
    brand = brand_service._db_entity_to_brand(site_entity.brand)

    site_tuple = dataclasses.astuple(site)
    brand_tuple = (brand,)

    return SiteWithBrand(*(site_tuple + brand_tuple))