~sirn/fanboi2

ref: 98796226802a26b82c888365ad5b9cd331006792 fanboi2/fanboi2/models/rule.py -rw-r--r-- 1.3 KiB
98796226Kridsada Thanabulpong Bump copyright year. 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
from sqlalchemy.dialects.postgresql import INET
from sqlalchemy.sql import func, desc, and_, or_
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import Integer, DateTime, Boolean, 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
        if scopes is not None:
            scope_q = or_(scope_q, cls.scope.in_(scopes))
        return and_(
            scope_q,
            cls.active == True,
            cls.ip_address.op('>>=')(ip_address),
            or_(cls.active_until == None,
                cls.active_until >= func.now()))