Beispiel #1
0
psycogreen.gevent.patch_psycopg()

ENV = os.getenv('ENV', 'local')

REGISTERED_MODELS = []

database = Proxy()

OP['IRGX'] = 'irgx'


def pg_regex_i(lhs, rhs):
    return Expression(lhs, OP.IRGX, rhs)


PostgresqlExtDatabase.register_ops({OP.IRGX: '~*'})


class BaseModel(Model):
    class Meta:
        database = database

    @staticmethod
    def register(cls):
        REGISTERED_MODELS.append(cls)
        return cls


def init_db(env):
    if env == 'docker':
        database.initialize(
Beispiel #2
0
    def python_value(self, value):
        return ipaddress.ip_address(value)


class IPRangeField(IPField):
    db_field = 'iprange'

    def python_value(self, value):
        return ipaddress.ip_network(value)


ip_ops = {
    'IP4R_CONTAINS': '>>=',
    'IP4R_STRICT_CONTAINS': '>>',
    'IP4R_CONTAINED_BY': '<<=',
    'IP4R_STRICT_CONTAINED_BY': '<<',
    'IP4R_OVERLAPS': '&&',
}

for op_name, op in ip_ops.items():
    OP[op_name] = 'IP' + op

PostgresqlExtDatabase.register_fields({
    'ipaddress': 'ipaddress',
    'iprange': 'iprange'
})
PostgresqlExtDatabase.register_ops(
    {OP[op_name]: op
     for op_name, op in ip_ops.items()})