VirtualAccount   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
wmc 0
1
from .model import *
2
3
from sqlalchemy_utils import ArrowType
4
5
class Account(Versioned, Base):
6
    __tablename__ = "accounts"
7
8
    id               = Column(Integer, primary_key=True, nullable=False)
9
    type             = Column(Enum("user", "virtual", "cash", "pool", "reimbursee", name="account_type"), nullable=False)
10
    name             = Column(String(255), nullable=False)
11
    balance          = Column(Numeric, nullable=False)
12
    archived_balance = Column(Numeric, nullable=True)
13
    created_at       = Column(ArrowType, default=datetime.datetime.utcnow)
14
15
    __mapper_args__ = {'polymorphic_on': type}
16
17
    def __init__(self, name):
18
        self.name = name
19
        self.balance = Decimal(0.0)
20
21
22
class VirtualAccount(Account):
23
    __mapper_args__ = {'polymorphic_identity': 'virtual'}
24
25
26
class CashAccount(Account):
27
    __mapper_args__ = {'polymorphic_identity': 'cash'}
28
29
30
# Get an account object of the virtual account corresponding to the argument
31
# "name". If one does not exist, make it transparently.
32
def get_virt_account(name):
33
    t = DBSession.query(VirtualAccount).filter(VirtualAccount.name == name).first()
34
    if t:
35
        return t
36
    t = VirtualAccount(name)
37
    DBSession.add(t)
38
    DBSession.flush()
39
    return t
40
41
def get_cash_account(name):
42
    t = DBSession.query(CashAccount).filter(CashAccount.name == name).first()
43
    if t:
44
        return t
45
    t = CashAccount(name)
46
    t.archived_balance = None
47
    DBSession.add(t)
48
    DBSession.flush()
49
    return t
50