1
|
|
|
""" |
2
|
|
|
byceps.services.brand.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 |
10
|
|
|
|
11
|
1 |
|
from ...database import db |
12
|
1 |
|
from ...typing import BrandID |
13
|
|
|
|
14
|
1 |
|
from .models.brand import Brand as DbBrand |
15
|
1 |
|
from .models.setting import Setting as DbSetting |
16
|
1 |
|
from .transfer.models import Brand |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
def create_brand(brand_id: BrandID, title: str) -> Brand: |
20
|
|
|
"""Create a brand.""" |
21
|
1 |
|
brand = DbBrand(brand_id, title) |
22
|
|
|
|
23
|
1 |
|
db.session.add(brand) |
24
|
1 |
|
db.session.commit() |
25
|
|
|
|
26
|
1 |
|
return _db_entity_to_brand(brand) |
27
|
|
|
|
28
|
|
|
|
29
|
1 |
|
def update_brand( |
30
|
|
|
brand_id: BrandID, title: str, *, image_filename: Optional[str] = None |
31
|
|
|
) -> Brand: |
32
|
|
|
"""Update a brand.""" |
33
|
|
|
brand = _get_db_brand(brand_id) |
34
|
|
|
|
35
|
|
|
brand.title = title |
36
|
|
|
brand.image_filename = image_filename |
37
|
|
|
|
38
|
|
|
db.session.commit() |
39
|
|
|
|
40
|
|
|
return _db_entity_to_brand(brand) |
41
|
|
|
|
42
|
|
|
|
43
|
1 |
|
def delete_brand(brand_id: BrandID) -> None: |
44
|
|
|
"""Delete a brand.""" |
45
|
1 |
|
db.session.query(DbSetting) \ |
46
|
|
|
.filter_by(brand_id=brand_id) \ |
47
|
|
|
.delete() |
48
|
|
|
|
49
|
1 |
|
db.session.query(DbBrand) \ |
50
|
|
|
.filter_by(id=brand_id) \ |
51
|
|
|
.delete() |
52
|
|
|
|
53
|
1 |
|
db.session.commit() |
54
|
|
|
|
55
|
|
|
|
56
|
1 |
|
def find_brand(brand_id: BrandID) -> Optional[Brand]: |
57
|
|
|
"""Return the brand with that id, or `None` if not found.""" |
58
|
1 |
|
brand = _get_db_brand(brand_id) |
59
|
|
|
|
60
|
1 |
|
if brand is None: |
61
|
|
|
return None |
62
|
|
|
|
63
|
1 |
|
return _db_entity_to_brand(brand) |
64
|
|
|
|
65
|
|
|
|
66
|
1 |
|
def _get_db_brand(brand_id: BrandID) -> DbBrand: |
67
|
|
|
"""Return the brand with that ID.""" |
68
|
1 |
|
return DbBrand.query.get(brand_id) |
69
|
|
|
|
70
|
|
|
|
71
|
1 |
|
def get_all_brands() -> List[Brand]: |
72
|
|
|
"""Return all brands, ordered by title.""" |
73
|
1 |
|
brands = DbBrand.query \ |
74
|
|
|
.order_by(DbBrand.title) \ |
75
|
|
|
.all() |
76
|
|
|
|
77
|
1 |
|
return [_db_entity_to_brand(brand) for brand in brands] |
78
|
|
|
|
79
|
|
|
|
80
|
1 |
|
def count_brands() -> int: |
81
|
|
|
"""Return the number of brands.""" |
82
|
|
|
return DbBrand.query.count() |
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
def _db_entity_to_brand(brand: DbBrand) -> Brand: |
86
|
1 |
|
if brand.image_filename: |
87
|
|
|
image_url_path = f'/data/global/brand_images/{brand.image_filename}' |
88
|
|
|
else: |
89
|
1 |
|
image_url_path = None |
90
|
|
|
|
91
|
1 |
|
return Brand( |
92
|
|
|
brand.id, |
93
|
|
|
brand.title, |
94
|
|
|
brand.image_filename, |
95
|
|
|
image_url_path, |
96
|
|
|
) |
97
|
|
|
|