Completed
Push — main ( d487f1...b7ef7f )
by Jochen
03:15
created

set_extra_setting()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.3145

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 3
dl 0
loc 10
ccs 1
cts 6
cp 0.1666
crap 4.3145
rs 10
c 0
b 0
f 0
1
"""
2
byceps.services.shop.shop.service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from typing import List, Optional, Set
10
11 1
from ....database import db
12
13 1
from .models import Shop as DbShop
14 1
from .transfer.models import Shop, ShopID
15
16
17 1
class UnknownShopId(ValueError):
18 1
    pass
19
20
21 1
def create_shop(shop_id: ShopID, title: str, email_config_id: str) -> Shop:
22
    """Create a shop."""
23 1
    shop = DbShop(shop_id, title, email_config_id)
24
25 1
    db.session.add(shop)
26 1
    db.session.commit()
27
28 1
    return _db_entity_to_shop(shop)
29
30
31 1
def update_shop(shop_id: ShopID, title: str, email_config_id: str) -> Shop:
32
    """Update a shop."""
33
    shop = _get_db_shop(shop_id)
34
35
    shop.title = title
36
    shop.email_config_id = email_config_id
37
38
    db.session.commit()
39
40
    return _db_entity_to_shop(shop)
41
42
43 1
def delete_shop(shop_id: ShopID) -> None:
44
    """Delete a shop."""
45 1
    db.session.query(DbShop) \
46
        .filter_by(id=shop_id) \
47
        .delete()
48
49 1
    db.session.commit()
50
51
52 1
def find_shop(shop_id: ShopID) -> Optional[Shop]:
53
    """Return the shop with that id, or `None` if not found."""
54 1
    shop = _find_db_shop(shop_id)
55
56 1
    if shop is None:
57
        return None
58
59 1
    return _db_entity_to_shop(shop)
60
61
62 1
def _find_db_shop(shop_id: ShopID) -> Optional[DbShop]:
63
    """Return the database entity for the shop with that id, or `None`
64
    if not found.
65
    """
66 1
    return DbShop.query.get(shop_id)
67
68
69 1
def get_shop(shop_id: ShopID) -> Shop:
70
    """Return the shop with that id, or raise an exception."""
71 1
    shop = find_shop(shop_id)
72
73 1
    if shop is None:
74
        raise UnknownShopId(shop_id)
75
76 1
    return shop
77
78
79 1
def _get_db_shop(shop_id: ShopID) -> DbShop:
80
    """Return the database entity for the shop with that id.
81
82
    Raise an exception if not found.
83
    """
84
    shop = _find_db_shop(shop_id)
85
86
    if shop is None:
87
        raise UnknownShopId(shop_id)
88
89
    return shop
90
91
92 1
def find_shops(shop_ids: Set[ShopID]) -> List[Shop]:
93
    """Return the shops with those IDs."""
94
    if not shop_ids:
95
        return []
96
97
    shops = DbShop.query \
98
        .filter(DbShop.id.in_(shop_ids)) \
99
        .all()
100
101
    return [_db_entity_to_shop(shop) for shop in shops]
102
103
104 1
def get_all_shops() -> List[Shop]:
105
    """Return all shops."""
106
    shops = DbShop.query.all()
107
108
    return [_db_entity_to_shop(shop) for shop in shops]
109
110
111 1
def get_active_shops() -> List[Shop]:
112
    """Return all shops that are not archived."""
113 1
    shops = DbShop.query \
114
        .filter_by(archived=False) \
115
        .all()
116
117 1
    return [_db_entity_to_shop(shop) for shop in shops]
118
119
120 1
def set_extra_setting(shop_id: ShopID, key: str, value: str) -> None:
121
    """Set a value for a key in the shop's extra settings."""
122
    shop = _get_db_shop(shop_id)
123
124
    if shop.extra_settings is None:
125
        shop.extra_settings = {}
126
127
    shop.extra_settings[key] = value
128
129
    db.session.commit()
130
131
132 1
def remove_extra_setting(shop_id: ShopID, key: str) -> None:
133
    """Remove the entry with that key from the shop's extra settings."""
134
    shop = _get_db_shop(shop_id)
135
136
    if (shop.extra_settings is None) or (key not in shop.extra_settings):
137
        return
138
139
    del shop.extra_settings[key]
140
141
    db.session.commit()
142
143
144 1
def _db_entity_to_shop(shop: DbShop) -> Shop:
145 1
    settings = shop.extra_settings if (shop.extra_settings is not None) else {}
146
147 1
    return Shop(
148
        shop.id,
149
        shop.title,
150
        shop.email_config_id,
151
        shop.archived,
152
        settings,
153
    )
154