Completed
Push — master ( 45d843...d0a2b0 )
by Pat
58s
created

chezbetty.models.Event.get_deleted()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 5
rs 9.4286
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", "emptybitcoin", "reconcile",
24
                       "donation", "withdrawal",
25
                       name="event_type"), nullable=False)
26
    __mapper_args__ = {'polymorphic_on': type}
27
28
29
    def __init__(self, user, notes, timestamp=None):
30
        self.user_id = user.id
31
        self.notes = notes
32
        self.timestamp = timestamp or datetime.datetime.utcnow()
33
34
    def delete(self, user):
35
        self.deleted = True
36
        self.deleted_timestamp = datetime.datetime.utcnow()
37
        self.deleted_user_id = user.id
38
39
    @classmethod
40
    def from_id(cls, id):
41
        return DBSession.query(cls).filter(cls.id == id).one()
42
43
    @classmethod
44
    @limitable_all
45
    def all(cls, trans_type=None):
46
        if not trans_type:
47
            return DBSession.query(cls)\
48
                            .filter(cls.deleted == False)\
49
                            .order_by(desc(cls.id))
50
        else:
51
            return DBSession.query(cls)\
52
                            .filter(cls.deleted == False)\
53
                            .filter(cls.type==trans_type)\
54
                            .order_by(desc(cls.id))
55
56
    @classmethod
57
    def some(cls, count):
58
        return DBSession.query(cls)\
59
                        .filter(cls.deleted == False)\
60
                        .order_by(desc(cls.id))\
61
                        .limit(count).all()
62
63
    @classmethod
64
    @limitable_all
65
    def get_deleted_events(cls):
66
        return DBSession.query(cls)\
67
                        .filter(cls.deleted == True)\
68
                        .order_by(desc(cls.timestamp))
69
70
class Purchase(Event):
71
    __mapper_args__ = {'polymorphic_identity': 'purchase'}
72
    def __init__(self, user):
73
        Event.__init__(self, user, None)
74
75
76
class Deposit(Event):
77
    __mapper_args__ = {'polymorphic_identity': 'deposit'}
78
    def __init__(self, user):
79
        Event.__init__(self, user, None)
80
81
82
class Adjustment(Event):
83
    __mapper_args__ = {'polymorphic_identity': 'adjustment'}
84
    def __init__(self, admin, notes):
85
        if len(notes) < 3:
86
            raise NotesMissingException()
87
        Event.__init__(self, admin, notes)
88
89
90
class Restock(Event):
91
    __mapper_args__ = {'polymorphic_identity': 'restock'}
92
    def __init__(self, admin, timestamp=None):
93
        Event.__init__(self, admin, None, timestamp)
94
95
96
class Inventory(Event):
97
    __mapper_args__ = {'polymorphic_identity': 'inventory'}
98
    def __init__(self, admin):
99
        Event.__init__(self, admin, None)
100
101
102
class EmptyCashBox(Event):
103
    __mapper_args__ = {'polymorphic_identity': 'emptycashbox'}
104
    def __init__(self, admin):
105
        Event.__init__(self, admin, None)
106
107
108
class EmptyBitcoin(Event):
109
    __mapper_args__ = {'polymorphic_identity': 'emptybitcoin'}
110
    def __init__(self, admin):
111
        Event.__init__(self, admin, None)
112
113
114
class Reconcile(Event):
115
    __mapper_args__ = {'polymorphic_identity': 'reconcile'}
116
    def __init__(self, admin, notes):
117
        if len(notes) < 3:
118
            raise NotesMissingException()
119
        Event.__init__(self, admin, notes)
120
121
122
class Donation(Event):
123
    __mapper_args__ = {'polymorphic_identity': 'donation'}
124
    def __init__(self, admin, notes, timestamp):
125
        if len(notes) < 3:
126
            raise NotesMissingException()
127
        Event.__init__(self, admin, notes, timestamp)
128
129
130
class Withdrawal(Event):
131
    __mapper_args__ = {'polymorphic_identity': 'withdrawal'}
132
    def __init__(self, admin, notes, timestamp):
133
        if len(notes) < 3:
134
            raise NotesMissingException()
135
        Event.__init__(self, admin, notes, timestamp)
136
137
138