2013-09-30 17:34:16 +03:00
|
|
|
import rethinkdb as rdb
|
|
|
|
from reimagine import conn
|
2013-09-29 00:09:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_one(cur):
|
|
|
|
res = cur.chunks[0]
|
|
|
|
return res[0] if len(res) else None
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModel():
|
|
|
|
__table__ = None
|
|
|
|
_conn = conn
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def filter(cls, f, limit=None):
|
2013-09-30 17:34:16 +03:00
|
|
|
q = rdb.table(cls.__table__).filter(f)
|
2013-09-29 00:09:02 +03:00
|
|
|
if limit:
|
|
|
|
q.limit(int(limit))
|
|
|
|
return q.run(cls._conn)
|
|
|
|
|
|
|
|
|
|
|
|
class Site(BaseModel):
|
|
|
|
__table__ = 'sites'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_by_name(cls, name):
|
|
|
|
return get_one(cls.filter({'name': name}, limit=1))
|