MyCoServer/mycoserver/controllers.py
2014-01-12 01:33:07 +01:00

70 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
import logging
from webob.dec import wsgify
from . import conv, router, templates, wsgi_helpers
import json
log = logging.getLogger(__name__)
@wsgify
def home(req):
return templates.render(req.ctx, '/home.mako', data={})
@wsgify
def login(req):
params = req.params
log.debug(u'params = {}'.format(params))
inputs = {
'email': params.get('email'),
'password': params.get('password'),
}
log.debug(u'inputs = {}'.format(inputs))
data, errors = conv.inputs_to_login_data(inputs)
if errors is not None:
return wsgi_helpers.bad_request(req.ctx, comment=errors)
log.debug(u'data = {}'.format(data))
login_data=req.ctx.db.login(data['email'],data['password'])
return wsgi_helpers.respond_json(req.ctx,login_data,headers=[('Access-Control-Allow-Origin','*')])
@wsgify
def sync(req):
params = req.params
log.debug(u'params = {}'.format(params))
inputs = {
'email': params.get('email'),
'password': params.get('password'),
'groups': params.get('groups')
}
log.debug(u'inputs = {}'.format(inputs))
data, errors = conv.inputs_to_sync_data(inputs)
if errors is not None or data['groups'] is None:
return wsgi_helpers.bad_request(req.ctx, comment=errors)
data['groups']=json.loads(data['groups'])
log.debug(u'data = {}'.format(data))
login_data=req.ctx.db.login(data['email'],data['password'])
if 'email' in login_data:
ret=req.ctx.db.sync_group(data['email'],data['groups'])
return wsgi_helpers.respond_json(req.ctx,ret,headers=[('Access-Control-Allow-Origin','*')])
else:
return wsgi_helpers.respond_json(
req.ctx,
login_data,
headers=[('Access-Control-Allow-Origin','*')]
)
def make_router():
return router.make_router(
('GET', '^/$', home),
('GET', '^/login$', login),
('GET', '^/sync$', sync),
)