ItemImage   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __str__() 0 2 1
A __init__() 0 3 1
1
from .model import *
2
3
class ItemImage(Base):
4
    __tablename__ = 'items_images'
5
6
    id         = Column(Integer, primary_key=True, nullable=False)
7
    item_id    = Column(Integer, ForeignKey("items.id"), nullable=False)
8
    img        = Column(LargeBinary, nullable=False)
9
10
    def __init__(self, item_id, img):
11
        self.item_id = item_id
12
        self.img = img
13
14
    def __str__(self):
15
        return "<ItemImage (%s)>" % self.item.name
16
17
    __repr__ = __str__
18
19
20
class Item(Versioned, Base):
21
    __tablename__ = 'items'
22
23
    id           = Column(Integer, primary_key=True, nullable=False)
24
    name         = Column(String(255), nullable=False, unique=True)
25
    barcode      = Column(String(255), nullable=True, unique=True)
26
    price        = Column(Numeric, nullable=False)
27
    sticky_price = Column(Boolean, default=False)
28
    wholesale    = Column(Numeric, nullable=False)
29
    bottle_dep   = Column(Boolean, nullable=False, default=False)
30
    sales_tax    = Column(Boolean, nullable=False, default=False)
31
    in_stock     = Column(Integer, nullable=False, default=0)
32
    img          = relationship(ItemImage, uselist=False, backref="item")
33
34
    enabled      = Column(Boolean, default=True, nullable=False)
35
36
    def __init__(self, name, barcode, price, wholesale, bottle_dep, sales_tax,
37
                 in_stock, enabled):
38
        self.name = name
39
        self.barcode = barcode
40
        self.price = price
41
        self.wholesale = wholesale
42
        self.bottle_dep = bottle_dep
43
        self.sales_tax = sales_tax
44
        self.in_stock = in_stock
45
        self.enabled = enabled
46
47
    @classmethod
48
    def from_id(cls, id):
49
        return DBSession.query(cls).filter(cls.id == id).one()
50
51
    @classmethod
52
    def from_barcode(cls, barcode):
53
        return DBSession.query(cls).filter(cls.barcode == barcode).one()
54
55
    @classmethod
56
    def from_fuzzy(cls, search_str):
57
        return DBSession.query(cls)\
58
                        .filter(or_(
59
                            cls.barcode.ilike('%{}%'.format(search_str)),
60
                            cls.name.ilike('%{}%'.format(search_str))
61
                        )).all()
62
63
    @classmethod
64
    def from_name(cls, name):
65
        return DBSession.query(cls).filter(cls.name == name).one()
66
67
    @classmethod
68
    def all(cls):
69
        return DBSession.query(cls)\
70
                        .filter(cls.enabled)\
71
                        .order_by(cls.name).all()
72
73
    @classmethod
74
    def disabled(cls):
75
        return DBSession.query(cls)\
76
                        .filter(cls.enabled==False)\
77
                        .order_by(cls.name).all()
78
79
    @classmethod
80
    def all_force(cls):
81
        return DBSession.query(cls)\
82
                        .order_by(cls.name).all()
83
84
    @classmethod
85
    def count(cls):
86
        return DBSession.query(func.count(cls.id).label('c'))\
87
                        .filter(cls.enabled).one().c
88
89
    @classmethod
90
    def exists_name(cls, name):
91
        return DBSession.query(func.count(cls.id).label('c'))\
92
                        .filter(cls.name == name).one().c > 0
93
94
    @classmethod
95
    def exists_barcode(cls, barcode):
96
        return DBSession.query(func.count(cls.id).label('c'))\
97
                        .filter(cls.barcode == barcode).one().c > 0
98
99
    @classmethod
100
    def total_inventory_wholesale(cls):
101
        return DBSession.query(func.sum(cls.wholesale * cls.in_stock).label('c'))\
102
                        .one().c or 0
103
104
    def __str__(self):
105
        return "<Item (%s)>" % self.name
106
107
    __repr__ = __str__
108
109