2016-05-03 18:28:56 -07:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
import chryso.connection
|
2016-05-04 06:16:05 -07:00
|
|
|
import chryso.queryadapter
|
2016-05-03 18:28:56 -07:00
|
|
|
import passlib.apps
|
|
|
|
import sepiida.routing
|
|
|
|
|
|
|
|
import vanth.tables
|
|
|
|
|
|
|
|
|
2016-05-17 13:16:14 -07:00
|
|
|
def _to_dict(result):
|
|
|
|
return {
|
|
|
|
'username' : result[vanth.tables.User.c.username],
|
|
|
|
'password' : result[vanth.tables.User.c.password],
|
|
|
|
'name' : result[vanth.tables.User.c.name],
|
|
|
|
'uri' : sepiida.routing.uri('user', result[vanth.tables.User.c.uuid]),
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:16:05 -07:00
|
|
|
def by_filter(filters):
|
|
|
|
engine = chryso.connection.get()
|
|
|
|
|
|
|
|
query = vanth.tables.User.select()
|
|
|
|
query = chryso.queryadapter.map_and_filter(vanth.tables.User, filters, query)
|
|
|
|
results = engine.execute(query).fetchall()
|
2016-05-17 13:16:14 -07:00
|
|
|
return [_to_dict(result) for result in results]
|
|
|
|
|
|
|
|
def by_credentials(username, password):
|
|
|
|
engine = chryso.connection.get()
|
|
|
|
|
|
|
|
query = vanth.tables.User.select().where(vanth.tables.User.c.username == username)
|
|
|
|
result = engine.execute(query).first()
|
|
|
|
|
|
|
|
if not (result and passlib.apps.custom_app_context.verify(password, result[vanth.tables.User.c.password])):
|
|
|
|
return None
|
|
|
|
|
|
|
|
return _to_dict(result)
|
2016-05-04 06:16:05 -07:00
|
|
|
|
2016-05-03 18:28:56 -07:00
|
|
|
def create(name, username, password):
|
|
|
|
engine = chryso.connection.get()
|
|
|
|
|
|
|
|
_uuid = uuid.uuid4()
|
|
|
|
statement = vanth.tables.User.insert().values( #pylint: disable=no-value-for-parameter
|
|
|
|
name = name,
|
|
|
|
password = passlib.apps.custom_app_context.encrypt(password),
|
|
|
|
username = username,
|
|
|
|
uuid = str(_uuid),
|
|
|
|
)
|
|
|
|
engine.execute(statement)
|
|
|
|
|
|
|
|
return sepiida.routing.uri('user', _uuid)
|