2013-10-05 08:04:38 +03:00
|
|
|
import json
|
|
|
|
|
2013-10-03 17:58:07 +03:00
|
|
|
import rethinkdb as rdb
|
2013-10-04 05:57:19 +03:00
|
|
|
import bcrypt
|
2013-10-05 00:42:45 +03:00
|
|
|
from flask import session, flash
|
2013-10-05 08:04:38 +03:00
|
|
|
|
2013-10-05 00:42:45 +03:00
|
|
|
from flask.ext.login import login_user, logout_user
|
2013-10-05 08:04:38 +03:00
|
|
|
|
2013-10-02 04:50:48 +03:00
|
|
|
from rethinkORM import RethinkModel
|
2013-10-05 08:04:38 +03:00
|
|
|
|
2013-10-05 00:42:45 +03:00
|
|
|
from util import gravatar_url
|
2013-10-05 08:04:38 +03:00
|
|
|
from services import db, cache
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-10-04 05:57:19 +03:00
|
|
|
|
2013-10-05 08:04:38 +03:00
|
|
|
def to_json(res, first=False):
|
|
|
|
"""
|
|
|
|
Jsonify query result.
|
|
|
|
"""
|
|
|
|
res = to_dict(res, first)
|
|
|
|
return json.dumps(res, separators=(',',':'))
|
|
|
|
|
|
|
|
|
|
|
|
def to_dict(cur, first=False):
|
|
|
|
if not cur:
|
|
|
|
return None
|
2013-10-03 17:58:07 +03:00
|
|
|
ret = []
|
|
|
|
for row in cur:
|
|
|
|
ret.append(row)
|
|
|
|
if ret and first:
|
|
|
|
return ret[0]
|
|
|
|
else:
|
|
|
|
return ret
|
2013-10-02 07:32:53 +03:00
|
|
|
|
2013-10-08 06:06:54 +03:00
|
|
|
|
2013-10-05 08:04:38 +03:00
|
|
|
def cache_it(fn):
|
|
|
|
def wrap(*args, **kw):
|
|
|
|
key = "%s:%s" % (args[0].table, args[1])
|
|
|
|
data = cache.get(key)
|
|
|
|
# Assume strings are JSON encoded
|
|
|
|
try:
|
|
|
|
data = json.loads(data)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
data = fn(*args)
|
2013-10-08 06:06:54 +03:00
|
|
|
print data
|
2013-10-05 08:04:38 +03:00
|
|
|
ret = data
|
|
|
|
if data is None:
|
|
|
|
data = ''
|
|
|
|
if not isinstance(data, basestring):
|
|
|
|
try:
|
|
|
|
data = json.dumps(data, separators=(',', ':'))
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
cache.set(key, data)
|
|
|
|
return ret
|
|
|
|
return wrap
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-10-08 06:06:54 +03:00
|
|
|
|
2013-10-02 04:50:48 +03:00
|
|
|
class BaseModel(RethinkModel):
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-10-05 00:42:45 +03:00
|
|
|
_conn = db
|
|
|
|
|
2013-10-02 04:50:48 +03:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
if not kwargs.get('conn'):
|
2013-10-05 00:42:45 +03:00
|
|
|
kwargs['conn'] = db
|
2013-10-02 04:50:48 +03:00
|
|
|
super(BaseModel, self).__init__(**kwargs)
|
2013-09-29 00:09:02 +03:00
|
|
|
|
|
|
|
@classmethod
|
2013-10-02 04:50:48 +03:00
|
|
|
def create(cls, **kwargs):
|
|
|
|
return super(BaseModel, cls).create(**kwargs)
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-10-05 08:04:38 +03:00
|
|
|
@cache_it
|
|
|
|
def get_by_id(self, id):
|
2013-10-08 06:06:54 +03:00
|
|
|
return rdb.table(self.table).get(id).run(self._conn)
|
2013-10-05 08:04:38 +03:00
|
|
|
|
2013-10-03 17:58:07 +03:00
|
|
|
def get_all(self, arg, index):
|
|
|
|
return rdb.table(self.table).get_all(arg, index=index).run(self._conn)
|
|
|
|
|
2013-10-05 08:04:38 +03:00
|
|
|
#@cache_it
|
2013-10-03 17:58:07 +03:00
|
|
|
def get_one(self, arg, index):
|
|
|
|
return rdb.table(self.table).get_all(arg, index=index).limit(1).run(self._conn)
|
|
|
|
|
2013-09-29 00:09:02 +03:00
|
|
|
|
|
|
|
class Site(BaseModel):
|
2013-10-02 04:50:48 +03:00
|
|
|
table = 'sites'
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-10-04 04:19:33 +03:00
|
|
|
def get_by_name(self, name):
|
|
|
|
return to_dict(self.get_one(name, 'name'), True)
|
|
|
|
|
2013-10-02 04:50:48 +03:00
|
|
|
|
2013-10-03 17:58:07 +03:00
|
|
|
class CurrentUser():
|
|
|
|
id = None
|
|
|
|
|
|
|
|
def __init__(self, id):
|
|
|
|
self.id = id
|
2013-10-08 06:06:54 +03:00
|
|
|
if id:
|
|
|
|
user = User()
|
|
|
|
session['user'] = user.get_by_id(id)
|
2013-10-03 17:58:07 +03:00
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
def is_active(self):
|
2013-10-05 00:42:45 +03:00
|
|
|
return True if self.id else False
|
2013-10-03 17:58:07 +03:00
|
|
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
return False if self.id else True
|
|
|
|
|
|
|
|
def is_authenticated(self):
|
|
|
|
return True if self.id else False
|
|
|
|
|
2013-10-05 08:04:38 +03:00
|
|
|
@staticmethod
|
|
|
|
def get(key):
|
|
|
|
try:
|
|
|
|
return session['user'][key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2013-10-03 17:58:07 +03:00
|
|
|
|
2013-10-02 04:50:48 +03:00
|
|
|
class User(BaseModel):
|
|
|
|
table = 'users'
|
|
|
|
|
2013-10-03 17:58:07 +03:00
|
|
|
def get_by_email(self, email):
|
|
|
|
return to_dict(self.get_one(email, 'email'), True)
|
|
|
|
|
|
|
|
def get_by_username(self, username):
|
|
|
|
return to_dict(self.get_one(username, 'username'), True)
|
2013-10-02 04:50:48 +03:00
|
|
|
|
|
|
|
def login(self, login, password):
|
2013-10-03 17:58:07 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, id):
|
|
|
|
print id
|
|
|
|
return cls(id=id)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def auth(cls, username, password):
|
|
|
|
u = User()
|
|
|
|
data = u.get_by_email(username)
|
|
|
|
if not data:
|
|
|
|
return False
|
|
|
|
|
2013-10-04 05:57:19 +03:00
|
|
|
if bcrypt.checkpw(password, data['password']):
|
2013-10-08 06:06:54 +03:00
|
|
|
cls.login(data['id'])
|
2013-10-03 17:58:07 +03:00
|
|
|
return True
|
|
|
|
else:
|
2013-10-05 00:42:45 +03:00
|
|
|
return False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def register(cls, username, email, password):
|
|
|
|
user = User()
|
|
|
|
email = email.lower()
|
|
|
|
if user.get_by_email(email):
|
|
|
|
flash('Email is already taken')
|
|
|
|
return False
|
|
|
|
if user.get_by_username(username):
|
|
|
|
flash('Username is already taken')
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Create user and login
|
|
|
|
u = User.create(email=email,
|
|
|
|
username=username,
|
|
|
|
password=bcrypt.hashpw(password, bcrypt.gensalt(10)),
|
|
|
|
avatar=gravatar_url(email))
|
|
|
|
|
2013-10-08 06:06:54 +03:00
|
|
|
User.login(u.id)
|
2013-10-05 00:42:45 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def login(cls, id, data=None):
|
|
|
|
login_user(CurrentUser(id), True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def logout(cls):
|
|
|
|
logout_user()
|
|
|
|
session.pop('user', None)
|