MyCoServer/mycoserver/controllers.py

94 lines
2.5 KiB
Python
Raw Normal View History

2014-01-12 01:33:07 +01:00
# -*- 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','*')])
2014-01-18 20:14:09 +01:00
@wsgify
def subscribe(req):
params = req.params
log.debug(u'params = {}'.format(params))
inputs = {
'email': params.get('email'),
'name': params.get('name'),
'password': params.get('password'),
}
log.debug(u'inputs = {}'.format(inputs))
data, errors = conv.inputs_to_subscribe_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.subscribe(data['email'],data['name'],data['password'])
return wsgi_helpers.respond_json(req.ctx,login_data,headers=[('Access-Control-Allow-Origin','*')])
2014-01-12 01:33:07 +01:00
@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:
log.debug(u'Login successful : sync groups')
2014-01-12 01:33:07 +01:00
ret=req.ctx.db.sync_group(data['email'],data['groups'])
return wsgi_helpers.respond_json(req.ctx,ret,headers=[('Access-Control-Allow-Origin','*')])
else:
log.debug(u'Login error : return login error')
2014-01-12 01:33:07 +01:00
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),
2014-01-18 20:14:09 +01:00
('GET', '^/subscribe$', subscribe),
2014-01-27 23:55:06 +01:00
('POST', '^/sync$', sync),
2014-01-12 01:33:07 +01:00
)