~homeworkprod/byceps

ref: bcef32cd2dc329bcf81057794883008f5c040ac8 byceps/byceps/services/shop/catalog/dbmodels.py -rw-r--r-- 2.9 KiB
bcef32cd — Jochen Kupperschmidt Work around Jinja 3.0.0 bug with `for` inside of `set` block 1 year, 7 days 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
"""
byceps.services.shop.catalog.dbmodels
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from sqlalchemy.ext.orderinglist import ordering_list

from ....database import db, generate_uuid
from ....util.instances import ReprBuilder

from ..article.transfer.models import ArticleNumber

from .transfer.models import CatalogID, CollectionID


class Catalog(db.Model):
    """A catalog to offer articles."""

    __tablename__ = 'shop_catalogs'

    id = db.Column(db.UnicodeText, primary_key=True)
    title = db.Column(db.UnicodeText, unique=True, nullable=False)

    def __init__(self, catalog_id: CatalogID, title: str) -> None:
        self.id = catalog_id
        self.title = title

    def __repr__(self) -> str:
        return ReprBuilder(self) \
            .add_with_lookup('id') \
            .build()


class Collection(db.Model):
    """A group of articles inside of catalog."""

    __tablename__ = 'shop_catalog_collections'
    __table_args__ = (
        db.UniqueConstraint('catalog_id', 'title'),
    )

    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
    catalog_id = db.Column(db.UnicodeText, db.ForeignKey('shop_catalogs.id'), index=True, nullable=False)
    title = db.Column(db.UnicodeText, nullable=False)
    position = db.Column(db.Integer, nullable=False)

    catalog = db.relationship(
        Catalog,
        backref=db.backref(
            'collections',
            order_by=position,
            collection_class=ordering_list('position', count_from=1),
        ),
    )

    def __init__(self, catalog_id: CatalogID, title: str) -> None:
        self.catalog_id = catalog_id
        self.title = title

    def __repr__(self) -> str:
        return ReprBuilder(self) \
            .add_with_lookup('catalog_id') \
            .add_with_lookup('title') \
            .build()


class CatalogArticle(db.Model):
    """The assignment of an article to a collection."""

    __tablename__ = 'shop_catalog_articles'
    __table_args__ = (
        db.UniqueConstraint('collection_id', 'article_number'),
    )

    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
    collection_id = db.Column(db.Uuid, db.ForeignKey('shop_catalog_collections.id'), index=True, nullable=False)
    article_number = db.Column(db.UnicodeText, db.ForeignKey('shop_articles.item_number'), index=True, nullable=False)
    position = db.Column(db.Integer, nullable=False)

    collection = db.relationship(
        Collection,
        backref=db.backref(
            'catalog_articles',
            order_by=position,
            collection_class=ordering_list('position', count_from=1),
        ),
    )

    def __init__(
        self, collection_id: CollectionID, article_number: ArticleNumber
    ) -> None:
        self.collection_id = collection_id
        self.article_number = article_number