~sirn/fanboi2

ref: 834edf0edc5dd633c0ecea16231b6ed2d728476d fanboi2/fanboi2/models/rule.py -rw-r--r-- 1.3 KiB
834edf0eKridsada Thanabulpong Massive cleanup in preparation for 0.30 (#25) 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from sqlalchemy.dialects.postgresql import INET
from sqlalchemy.sql import func, and_, or_
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import Boolean, DateTime, Integer, String, Unicode

from ._base import Base


class Rule(Base):
    """Model class that provides an IP rule."""

    __tablename__ = 'rule'

    id = Column(Integer, primary_key=True)
    type = Column(String, nullable=False)
    scope = Column(String)
    created_at = Column(DateTime(timezone=True), default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    ip_address = Column(INET, nullable=False)
    active = Column(Boolean, nullable=False, default=True)
    active_until = Column(DateTime(timezone=True))
    description = Column(Unicode)

    __mapper_args__ = {
        'polymorphic_on': type,
        'polymorphic_identity': 'base',
    }

    @classmethod
    def listed(cls, ip_address, scopes=None):
        scope_q = cls.scope == None  # noqa: E712
        if scopes is not None:
            scope_q = or_(scope_q, cls.scope.in_(scopes))
        return and_(
            scope_q,
            cls.active == True,  # noqa: E712
            cls.ip_address.op('>>=')(ip_address),
            or_(cls.active_until == None,  # noqa: E712
                cls.active_until >= func.now()))