124 lines
2.9 KiB
Python
124 lines
2.9 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import falcon
|
||
|
from beaker.middleware import SessionMiddleware
|
||
|
|
||
|
import util
|
||
|
import middleware as MW
|
||
|
from models import StorageEngine, get_cp
|
||
|
from settings import DEBUG, WITH_LOGIN
|
||
|
|
||
|
|
||
|
class AppPostalCode(object):
|
||
|
|
||
|
def on_get(self, req, resp):
|
||
|
values = req.params
|
||
|
req.context['result'] = get_cp(values['cp'])
|
||
|
resp.status = falcon.HTTP_200
|
||
|
|
||
|
|
||
|
class AppLogin(object):
|
||
|
template = 'login.html'
|
||
|
|
||
|
def __init__(self, db):
|
||
|
self._db = db
|
||
|
|
||
|
@falcon.after(MW.get_template)
|
||
|
def on_get(self, req, resp):
|
||
|
resp.status = falcon.HTTP_200
|
||
|
|
||
|
def on_post(self, req, resp):
|
||
|
session = req.env['beaker.session']
|
||
|
values = req.params
|
||
|
result = self._db.authenticate(values['user'], values['pass'])
|
||
|
resp.body = util.dumps(result)
|
||
|
if result['login']:
|
||
|
session.save()
|
||
|
session['user'] = result['user']
|
||
|
resp.status = falcon.HTTP_200
|
||
|
|
||
|
|
||
|
class AppLogout(object):
|
||
|
|
||
|
def __init__(self, db):
|
||
|
self._db = db
|
||
|
|
||
|
def on_get(self, req, resp):
|
||
|
session = req.env['beaker.session']
|
||
|
session.delete()
|
||
|
resp.status = falcon.HTTP_200
|
||
|
raise falcon.HTTPTemporaryRedirect('/')
|
||
|
|
||
|
|
||
|
class AppMain(object):
|
||
|
template = 'main.html'
|
||
|
|
||
|
def __init__(self, db):
|
||
|
self._db = db
|
||
|
|
||
|
@falcon.after(MW.get_template)
|
||
|
def on_get(self, req, resp):
|
||
|
resp.status = falcon.HTTP_200
|
||
|
|
||
|
|
||
|
class AppPartners(object):
|
||
|
|
||
|
def __init__(self, db):
|
||
|
self._db = db
|
||
|
|
||
|
def on_get(self, req, resp):
|
||
|
values = req.params
|
||
|
req.context['result'] = self._db.get_partners(values)
|
||
|
resp.status = falcon.HTTP_200
|
||
|
|
||
|
def on_post(self, req, resp):
|
||
|
values = req.params
|
||
|
if values['id'] == '0':
|
||
|
del values['id']
|
||
|
req.context['result'] = self._db.new_partner(values)
|
||
|
else:
|
||
|
req.context['result'] = self._db.update_partner(values)
|
||
|
resp.status = falcon.HTTP_200
|
||
|
|
||
|
def on_delete(self, req, resp):
|
||
|
values = req.params
|
||
|
if self._db.delete_partner(values['id']):
|
||
|
resp.status = falcon.HTTP_200
|
||
|
else:
|
||
|
resp.status = falcon.HTTP_204
|
||
|
|
||
|
|
||
|
db = StorageEngine()
|
||
|
pc = AppPostalCode()
|
||
|
login = AppLogin(db)
|
||
|
logout = AppLogout(db)
|
||
|
main = AppMain(db)
|
||
|
partners = AppPartners(db)
|
||
|
|
||
|
api = falcon.API(middleware=[MW.AuthMiddleware(), MW.JSONTranslator()])
|
||
|
api.req_options.auto_parse_form_urlencoded = True
|
||
|
api.add_sink(MW.handle_404, '')
|
||
|
if WITH_LOGIN:
|
||
|
api.add_route('/', login)
|
||
|
api.add_route('/logout', logout)
|
||
|
api.add_route('/main', main)
|
||
|
api.add_route('/partners', partners)
|
||
|
api.add_route('/cp', pc)
|
||
|
|
||
|
|
||
|
if DEBUG:
|
||
|
api.add_sink(MW.static, '/static')
|
||
|
|
||
|
|
||
|
# Configure the SessionMiddleware
|
||
|
session_opts = {
|
||
|
'session.type': 'file',
|
||
|
'session.cookie_expires': True,
|
||
|
'session.data_dir': '/tmp/cache/data',
|
||
|
'session.lock_dir': '/tmp/cache/lock',
|
||
|
#~ 'session.httponly': True,
|
||
|
#~ 'session.secure': True,
|
||
|
}
|
||
|
app = SessionMiddleware(api, session_opts)
|
||
|
|