Withdrawal   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 2
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, start=None, end=None, trans_type=None):
47
        if not trans_type:
48
            q = DBSession.query(cls)\
49
                    .filter(cls.deleted == False)
50
        else:
51
            q = DBSession.query(cls)\
52
                    .filter(cls.deleted == False)\
53
                    .filter(cls.type==trans_type)
54
55
        if start:
56
            q = q.filter(cls.timestamp>=start)
57
        if end:
58
            q = q.filter(cls.timestamp<end)
59
60
        q = q.order_by(desc(cls.id))
61
62
        return q
63
64
    @classmethod
65
    def some(cls, count):
66
        return DBSession.query(cls)\
67
                        .filter(cls.deleted == False)\
68
                        .order_by(desc(cls.id))\
69
                        .limit(count).all()
70
71
    @classmethod
72
    @limitable_all
73
    def get_deleted_events(cls):
74
        return DBSession.query(cls)\
75
                        .filter(cls.deleted == True)\
76
                        .order_by(desc(cls.timestamp))
77
78
class Purchase(Event):
79
    __mapper_args__ = {'polymorphic_identity': 'purchase'}
80
    def __init__(self, user):
81
        Event.__init__(self, user, None)
82
83
84
class Deposit(Event):
85
    __mapper_args__ = {'polymorphic_identity': 'deposit'}
86
    def __init__(self, user):
87
        Event.__init__(self, user, None)
88
89
    @classmethod
90
    def get_user_recent(cls, user):
91
        return DBSession.query(cls)\
92
                        .filter(cls.user_id == user.id)\
93
                        .filter(cls.timestamp>=(datetime.datetime.utcnow()-datetime.timedelta(minutes=2)))\
94
                        .filter(cls.deleted == False)\
95
                        .limit(1)\
96
                        .all()
97
98
99
class Adjustment(Event):
100
    __mapper_args__ = {'polymorphic_identity': 'adjustment'}
101
    def __init__(self, admin, notes):
102
        if len(notes) < 3:
103
            raise NotesMissingException()
104
        Event.__init__(self, admin, notes)
105
106
107
class Restock(Event):
108
    __mapper_args__ = {'polymorphic_identity': 'restock'}
109
    def __init__(self, admin, timestamp=None):
110
        Event.__init__(self, admin, None, timestamp)
111
112
113
class Inventory(Event):
114
    __mapper_args__ = {'polymorphic_identity': 'inventory'}
115
    def __init__(self, admin):
116
        Event.__init__(self, admin, None)
117
118
119
class EmptyCashBox(Event):
120
    __mapper_args__ = {'polymorphic_identity': 'emptycashbox'}
121
    def __init__(self, admin):
122
        Event.__init__(self, admin, None)
123
124
125
class EmptySafe(Event):
126
    __mapper_args__ = {'polymorphic_identity': 'emptysafe'}
127
    def __init__(self, admin):
128
        Event.__init__(self, admin, None)
129
130
131
class EmptyBitcoin(Event):
132
    __mapper_args__ = {'polymorphic_identity': 'emptybitcoin'}
133
    def __init__(self, admin):
134
        Event.__init__(self, admin, None)
135
136
137
class Reconcile(Event):
138
    __mapper_args__ = {'polymorphic_identity': 'reconcile'}
139
    def __init__(self, admin, notes):
140
        if len(notes) < 3:
141
            raise NotesMissingException()
142
        Event.__init__(self, admin, notes)
143
144
145
class Donation(Event):
146
    __mapper_args__ = {'polymorphic_identity': 'donation'}
147
    def __init__(self, admin, notes, timestamp):
148
        if len(notes) < 3:
149
            raise NotesMissingException()
150
        Event.__init__(self, admin, notes, timestamp)
151
152
153
class Withdrawal(Event):
154
    __mapper_args__ = {'polymorphic_identity': 'withdrawal'}
155
    def __init__(self, admin, notes, timestamp):
156
        if len(notes) < 3:
157
            raise NotesMissingException()
158
        Event.__init__(self, admin, notes, timestamp)
159
160
161
class Reimbursement(Event):
162
    __mapper_args__ = {'polymorphic_identity': 'reimbursement'}
163
    def __init__(self, admin, notes, timestamp):
164
        Event.__init__(self, admin, notes, timestamp)
165
166
167