1
|
|
|
""" |
2
|
|
|
byceps.services.shop.shop.dbmodels |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2014-2022 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from typing import TYPE_CHECKING |
10
|
|
|
|
11
|
1 |
|
from moneyed import Currency, get_currency |
12
|
1 |
|
|
13
|
1 |
|
if TYPE_CHECKING: |
14
|
|
|
hybrid_property = property |
15
|
1 |
|
else: |
16
|
|
|
from sqlalchemy.ext.hybrid import hybrid_property |
17
|
|
|
from sqlalchemy.ext.mutable import MutableDict |
18
|
1 |
|
|
19
|
|
|
from ....database import db |
20
|
|
|
from ....typing import BrandID |
21
|
1 |
|
from ....util.instances import ReprBuilder |
22
|
|
|
|
23
|
1 |
|
from .transfer.models import ShopID |
24
|
1 |
|
|
25
|
|
|
|
26
|
|
|
class DbShop(db.Model): |
27
|
|
|
"""A shop.""" |
28
|
|
|
|
29
|
|
|
__tablename__ = 'shops' |
30
|
|
|
|
31
|
1 |
|
id = db.Column(db.UnicodeText, primary_key=True) |
32
|
1 |
|
brand_id = db.Column( |
33
|
1 |
|
db.UnicodeText, |
34
|
|
|
db.ForeignKey('brands.id'), |
35
|
1 |
|
unique=True, |
36
|
1 |
|
index=True, |
37
|
1 |
|
nullable=False, |
38
|
1 |
|
) |
39
|
|
|
title = db.Column(db.UnicodeText, unique=True, nullable=False) |
40
|
1 |
|
_currency = db.Column('currency', db.UnicodeText, nullable=False) |
41
|
|
|
archived = db.Column(db.Boolean, default=False, nullable=False) |
42
|
|
|
extra_settings = db.Column(MutableDict.as_mutable(db.JSONB)) |
43
|
|
|
|
44
|
|
|
def __init__( |
45
|
|
|
self, shop_id: ShopID, brand_id: BrandID, title: str, currency: Currency |
46
|
|
|
) -> None: |
47
|
|
|
self.id = shop_id |
48
|
|
|
self.brand_id = brand_id |
49
|
|
|
self.title = title |
50
|
|
|
self.currency = currency |
51
|
|
|
|
52
|
|
|
@hybrid_property |
53
|
|
|
def currency(self) -> Currency: |
54
|
|
|
return get_currency(self._currency) |
55
|
|
|
|
56
|
|
|
@currency.setter |
57
|
|
|
def currency(self, currency: Currency) -> None: |
58
|
|
|
self._currency = currency.code |
59
|
|
|
|
60
|
|
|
def __repr__(self) -> str: |
61
|
|
|
return ReprBuilder(self).add_with_lookup('id').build() |
62
|
|
|
|