Passed
Push — main ( 4c626d...b574ef )
by Jochen
09:17
created

CatalogArticle.__init__()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 3
dl 0
loc 5
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 10
c 0
b 0
f 0
1
"""
2
byceps.services.shop.catalog.models
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from sqlalchemy.ext.orderinglist import ordering_list
10
11 1
from ....database import db, generate_uuid
12 1
from ....util.instances import ReprBuilder
13
14 1
from ..article.transfer.models import ArticleNumber
15
16 1
from .transfer.models import CatalogID, CollectionID
17
18
19 1
class Catalog(db.Model):
20
    """A catalog to offer articles."""
21
22 1
    __tablename__ = 'shop_catalogs'
23
24 1
    id = db.Column(db.UnicodeText, primary_key=True)
25 1
    title = db.Column(db.UnicodeText, unique=True, nullable=False)
26
27 1
    def __init__(self, catalog_id: CatalogID, title: str) -> None:
28
        self.id = catalog_id
29
        self.title = title
30
31 1
    def __repr__(self) -> str:
32
        return ReprBuilder(self) \
33
            .add_with_lookup('id') \
34
            .build()
35
36
37 1 View Code Duplication
class Collection(db.Model):
38
    """A group of articles inside of catalog."""
39
40 1
    __tablename__ = 'shop_catalog_collections'
41 1
    __table_args__ = (
42
        db.UniqueConstraint('catalog_id', 'title'),
43
    )
44
45 1
    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
46 1
    catalog_id = db.Column(db.UnicodeText, db.ForeignKey('shop_catalogs.id'), index=True, nullable=False)
47 1
    title = db.Column(db.UnicodeText, nullable=False)
48 1
    position = db.Column(db.Integer, nullable=False)
49
50 1
    catalog = db.relationship(
51
        Catalog,
52
        backref=db.backref(
53
            'collections',
54
            order_by=position,
55
            collection_class=ordering_list('position', count_from=1),
56
        ),
57
    )
58
59 1
    def __init__(self, catalog_id: CatalogID, title: str) -> None:
60
        self.catalog_id = catalog_id
61
        self.title = title
62
63 1
    def __repr__(self) -> str:
64
        return ReprBuilder(self) \
65
            .add_with_lookup('catalog_id') \
66
            .add_with_lookup('title') \
67
            .build()
68
69
70 1
class CatalogArticle(db.Model):
71
    """The assignment of an article to a collection."""
72
73 1
    __tablename__ = 'shop_catalog_articles'
74 1
    __table_args__ = (
75
        db.UniqueConstraint('collection_id', 'article_number'),
76
    )
77
78 1
    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
79 1
    collection_id = db.Column(db.Uuid, db.ForeignKey('shop_catalog_collections.id'), index=True, nullable=False)
80 1
    article_number = db.Column(db.UnicodeText, db.ForeignKey('shop_articles.item_number'), index=True, nullable=False)
81 1
    position = db.Column(db.Integer, nullable=False)
82
83 1
    collection = db.relationship(
84
        Collection,
85
        backref=db.backref(
86
            'catalog_articles',
87
            order_by=position,
88
            collection_class=ordering_list('position', count_from=1),
89
        ),
90
    )
91
92 1
    def __init__(
93
        self, collection_id: CollectionID, article_number: ArticleNumber
94
    ) -> None:
95
        self.collection_id = collection_id
96
        self.article_number = article_number
97