|
1
|
|
|
from .model import * |
|
2
|
|
|
|
|
3
|
|
|
class Box(Base): |
|
4
|
|
|
__tablename__ = 'boxes' |
|
5
|
|
|
|
|
6
|
|
|
id = Column(Integer, primary_key=True, nullable=False) |
|
7
|
|
|
name = Column(String(255), nullable=False, unique=True) |
|
8
|
|
|
barcode = Column(String(255), nullable=True, unique=True) |
|
9
|
|
|
wholesale = Column(Numeric, nullable=False) |
|
10
|
|
|
bottle_dep = Column(Boolean, nullable=False, default=False) |
|
11
|
|
|
sales_tax = Column(Boolean, nullable=False, default=False) |
|
12
|
|
|
|
|
13
|
|
|
enabled = Column(Boolean, default=True, nullable=False) |
|
14
|
|
|
|
|
15
|
|
|
def __init__(self, name, barcode, bottle_dep, |
|
16
|
|
|
sales_tax, wholesale=0, enabled=True): |
|
17
|
|
|
self.name = name |
|
18
|
|
|
self.barcode = barcode |
|
19
|
|
|
self.wholesale = wholesale |
|
20
|
|
|
self.bottle_dep = bottle_dep |
|
21
|
|
|
self.sales_tax = sales_tax |
|
22
|
|
|
self.enabled = enabled |
|
23
|
|
|
|
|
24
|
|
|
@classmethod |
|
25
|
|
|
def from_id(cls, id): |
|
26
|
|
|
return DBSession.query(cls).filter(cls.id == id).one() |
|
27
|
|
|
|
|
28
|
|
|
@classmethod |
|
29
|
|
|
def from_barcode(cls, barcode): |
|
30
|
|
|
return DBSession.query(cls).filter(cls.barcode == barcode).one() |
|
31
|
|
|
|
|
32
|
|
|
@classmethod |
|
33
|
|
|
def from_fuzzy(cls, search_str): |
|
34
|
|
|
return DBSession.query(cls)\ |
|
35
|
|
|
.filter(or_( |
|
36
|
|
|
cls.barcode.ilike('%{}%'.format(search_str)), |
|
37
|
|
|
cls.name.ilike('%{}%'.format(search_str)) |
|
38
|
|
|
)).all() |
|
39
|
|
|
|
|
40
|
|
|
@classmethod |
|
41
|
|
|
def all(cls): |
|
42
|
|
|
return DBSession.query(cls)\ |
|
43
|
|
|
.filter(cls.enabled)\ |
|
44
|
|
|
.order_by(cls.name).all() |
|
45
|
|
|
|
|
46
|
|
|
@classmethod |
|
47
|
|
|
def count(cls): |
|
48
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
49
|
|
|
.filter(cls.enabled).one().c |
|
50
|
|
|
|
|
51
|
|
|
@classmethod |
|
52
|
|
|
def get_enabled(cls): |
|
53
|
|
|
return cls.all() |
|
54
|
|
|
|
|
55
|
|
|
@classmethod |
|
56
|
|
|
def get_disabled(cls): |
|
57
|
|
|
return DBSession.query(cls)\ |
|
58
|
|
|
.filter(cls.enabled==False)\ |
|
59
|
|
|
.order_by(cls.name).all() |
|
60
|
|
|
|
|
61
|
|
|
@classmethod |
|
62
|
|
|
def exists_name(cls, name): |
|
63
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
64
|
|
|
.filter(cls.name == name).one().c > 0 |
|
65
|
|
|
|
|
66
|
|
|
@classmethod |
|
67
|
|
|
def exists_barcode(cls, barcode): |
|
68
|
|
|
return DBSession.query(func.count(cls.id).label('c'))\ |
|
69
|
|
|
.filter(cls.barcode == barcode).one().c > 0 |
|
70
|
|
|
|
|
71
|
|
|
def __str__(self): |
|
72
|
|
|
return '<Box ({})>'.format(self.name) |
|
73
|
|
|
|
|
74
|
|
|
__repr__ = __str__ |
|
75
|
|
|
|