Completed
Push — master ( ba470c...1324e2 )
by
unknown
01:42
created

Reimbursement.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
from .model import *
2
from . import account
3
from . import item
4
5
from sqlalchemy_utils import ArrowType
6
7
class NotesMissingException(Exception):
8
    pass
9
10
class Event(Base):
11
    __tablename__ = 'events'
12
13
    id        = Column(Integer, primary_key=True, nullable=False)
14
    timestamp = Column(ArrowType, nullable=False, default=datetime.datetime.utcnow)
15
    user_id   = Column(Integer, ForeignKey("users.id"), nullable=False) # user that performed the event
16
    notes     = Column(Text)
17
18
    deleted           = Column(Boolean, default=False, nullable=False)
19
    deleted_timestamp = Column(ArrowType, nullable=True)
20
    deleted_user_id   = Column(Integer, ForeignKey("users.id"), nullable=True) # user that deleted the event
21
22
    type = Column(Enum("purchase", "deposit", "adjustment", "restock",
23
                       "inventory", "emptycashbox", "emptysafe", "emptybitcoin", "reconcile",
24
                       "donation", "withdrawal",
25
                       "reimbursement",
26
                       name="event_type"), nullable=False)
27
    __mapper_args__ = {'polymorphic_on': type}
28
29
30
    def __init__(self, user, notes, timestamp=None):
31
        self.user_id = user.id
32
        self.notes = notes
33
        self.timestamp = timestamp or datetime.datetime.utcnow()
34
35
    def delete(self, user):
36
        self.deleted = True
37
        self.deleted_timestamp = datetime.datetime.utcnow()
38
        self.deleted_user_id = user.id
39
40
    @classmethod
41
    def from_id(cls, id):
42
        return DBSession.query(cls).filter(cls.id == id).one()
43
44
    @classmethod
45
    @limitable_all
46
    def all(cls, trans_type=None):
47
        if not trans_type:
48
            return DBSession.query(cls)\
49
                            .filter(cls.deleted == False)\
50
                            .order_by(desc(cls.id))
51
        else:
52
            return DBSession.query(cls)\
53
                            .filter(cls.deleted == False)\
54
                            .filter(cls.type==trans_type)\
55
                            .order_by(desc(cls.id))
56
57
    @classmethod
58
    def some(cls, count):
59
        return DBSession.query(cls)\
60
                        .filter(cls.deleted == False)\
61
                        .order_by(desc(cls.id))\
62
                        .limit(count).all()
63
64
    @classmethod
65
    @limitable_all
66
    def get_deleted_events(cls):
67
        return DBSession.query(cls)\
68
                        .filter(cls.deleted == True)\
69
                        .order_by(desc(cls.timestamp))
70
71
class Purchase(Event):
72
    __mapper_args__ = {'polymorphic_identity': 'purchase'}
73
    def __init__(self, user):
74
        Event.__init__(self, user, None)
75
76
77
class Deposit(Event):
78
    __mapper_args__ = {'polymorphic_identity': 'deposit'}
79
    def __init__(self, user):
80
        Event.__init__(self, user, None)
81
82
    @classmethod
83
    def get_user_recent(cls, user):
84
        return DBSession.query(cls)\
85
                        .filter(cls.user_id == user.id)\
86
                        .filter(cls.timestamp>=(datetime.datetime.utcnow()-datetime.timedelta(minutes=2)))\
87
                        .filter(cls.deleted == False)\
88
                        .limit(1)\
89
                        .all()
90
91
92
class Adjustment(Event):
93
    __mapper_args__ = {'polymorphic_identity': 'adjustment'}
94
    def __init__(self, admin, notes):
95
        if len(notes) < 3:
96
            raise NotesMissingException()
97
        Event.__init__(self, admin, notes)
98
99
100
class Restock(Event):
101
    __mapper_args__ = {'polymorphic_identity': 'restock'}
102
    def __init__(self, admin, timestamp=None):
103
        Event.__init__(self, admin, None, timestamp)
104
105
106
class Inventory(Event):
107
    __mapper_args__ = {'polymorphic_identity': 'inventory'}
108
    def __init__(self, admin):
109
        Event.__init__(self, admin, None)
110
111
112
class EmptyCashBox(Event):
113
    __mapper_args__ = {'polymorphic_identity': 'emptycashbox'}
114
    def __init__(self, admin):
115
        Event.__init__(self, admin, None)
116
117
118
class EmptySafe(Event):
119
    __mapper_args__ = {'polymorphic_identity': 'emptysafe'}
120
    def __init__(self, admin):
121
        Event.__init__(self, admin, None)
122
123
124
class EmptyBitcoin(Event):
125
    __mapper_args__ = {'polymorphic_identity': 'emptybitcoin'}
126
    def __init__(self, admin):
127
        Event.__init__(self, admin, None)
128
129
130
class Reconcile(Event):
131
    __mapper_args__ = {'polymorphic_identity': 'reconcile'}
132
    def __init__(self, admin, notes):
133
        if len(notes) < 3:
134
            raise NotesMissingException()
135
        Event.__init__(self, admin, notes)
136
137
138
class Donation(Event):
139
    __mapper_args__ = {'polymorphic_identity': 'donation'}
140
    def __init__(self, admin, notes, timestamp):
141
        if len(notes) < 3:
142
            raise NotesMissingException()
143
        Event.__init__(self, admin, notes, timestamp)
144
145
146
class Withdrawal(Event):
147
    __mapper_args__ = {'polymorphic_identity': 'withdrawal'}
148
    def __init__(self, admin, notes, timestamp):
149
        if len(notes) < 3:
150
            raise NotesMissingException()
151
        Event.__init__(self, admin, notes, timestamp)
152
153
154
class Reimbursement(Event):
155
    __mapper_args__ = {'polymorphic_identity': 'reimbursement'}
156
    def __init__(self, admin, timestamp):
157
        Event.__init__(self, admin, None, timestamp)
158
159
160