Completed
Push — master ( d2aebd...b0ae7e )
by
unknown
01:01
created

Vendor.from_id()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
1
from .model import *
2
3
class Vendor(Base):
4
    __tablename__ = 'vendors'
5
6
    id        = Column(Integer, primary_key=True, nullable=False)
7
    name      = Column(String(255), nullable=False)
8
9
    enabled   = Column(Boolean, default=True, nullable=False)
10
11
    def __init__(self, name, enabled=True):
12
        self.name = name
13
        self.enabled = enabled
14
15
    @classmethod
16
    def from_id(cls, id):
17
        return DBSession.query(cls).filter(cls.id == id).one()
18
19
    @classmethod
20
    def all(cls):
21
        return DBSession.query(cls)\
22
                        .filter(cls.enabled)\
23
                        .order_by(cls.name).all()
24
25
    @classmethod
26
    def count(cls):
27
        return DBSession.query(func.count(cls.id).label('c'))\
28
                        .filter(cls.enabled).one().c
29
30
    def __str__(self):
31
        return "<Vendor (%s)>" % self.name
32
33
    __repr__ = __str__
34