Ephemeron.all()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
from .model import *
2
3
from sqlalchemy_utils import ArrowType
4
5
class Ephemeron(Base):
6
    __tablename__ = 'ephemera'
7
8
    id        = Column(Integer, primary_key=True, nullable=False)
9
    timestamp = Column(ArrowType, nullable=False, default=datetime.datetime.utcnow)
10
    name      = Column(String(255), nullable=False, unique=True)
11
    value     = Column(Text, nullable=False)
12
13
    def __init__(self, name, value):
14
        self.name = name
15
        self.value = value
16
17
    @classmethod
18
    def from_id(cls, id):
19
        return DBSession.query(cls).filter(cls.id == id).one()
20
21
    @classmethod
22
    def all(cls):
23
        return DBSession.query(cls)\
24
                        .filter(cls.deleted==False)\
25
                        .order_by(cls.name).all()
26
27
    @classmethod
28
    def from_name(cls, name):
29
        return DBSession.query(cls).filter(cls.name == name).one_or_none()
30
31
    @classmethod
32
    def add_list(cls, name, new_item):
33
        out = ''
34
        initial = DBSession.query(cls).filter(cls.name == name).one_or_none()
35
        if initial:
36
            initial.value = '{},{}'.format(initial.value, new_item)
37
            out = initial.value
38
        else:
39
            toadd = Ephemeron(name, new_item)
40
            DBSession.add(toadd)
41
            out = new_item
42
        DBSession.flush()
43
        return out
44
45
    @classmethod
46
    def add_decimal(cls, name, new_decimal):
47
        total_stored = Decimal(0)
48
49
        existing = Ephemeron.from_name(name)
50
        if existing:
51
            total_stored = Decimal(existing.value)
52
            total_stored += new_decimal
53
            existing.value = '{}'.format(total_stored)
54
        else:
55
            total_stored = new_decimal
56
            new_temp_deposit = Ephemeron(name, '{}'.format(new_decimal))
57
            DBSession.add(new_temp_deposit)
58
59
        DBSession.flush()
60
        return total_stored
61