2017-06-27 15:43:02 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import falcon
|
2017-06-28 23:55:53 -05:00
|
|
|
from controllers import util
|
2017-09-30 23:14:44 -05:00
|
|
|
from models import main
|
2017-11-30 00:37:13 -06:00
|
|
|
from settings import MV, PATH_STATIC
|
2017-06-27 15:43:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
def handle_404(req, resp):
|
|
|
|
id_session = req.cookies.get('beaker.session.id', '')
|
|
|
|
if id_session:
|
|
|
|
raise falcon.HTTPTemporaryRedirect('/main')
|
|
|
|
raise falcon.HTTPTemporaryRedirect('/')
|
|
|
|
|
|
|
|
|
|
|
|
def get_template(req, resp, resource):
|
|
|
|
session = req.env['beaker.session']
|
|
|
|
resp.content_type = 'text/html'
|
|
|
|
data = {'username': session.get('user', '')}
|
|
|
|
resp.body = util.get_template(resource.template, data)
|
|
|
|
|
|
|
|
|
|
|
|
def static(req, res):
|
|
|
|
path = PATH_STATIC + req.path
|
|
|
|
if util.is_file(path):
|
|
|
|
res.content_type = util.get_mimetype(path)
|
|
|
|
res.stream, res.stream_len = util.get_stream(path)
|
|
|
|
res.status = falcon.HTTP_200
|
|
|
|
else:
|
|
|
|
res.status = falcon.HTTP_404
|
|
|
|
|
|
|
|
|
|
|
|
class AuthMiddleware(object):
|
|
|
|
|
|
|
|
def process_resource(self, req, resp, resource, params):
|
2017-12-03 23:47:41 -06:00
|
|
|
session = req.env['beaker.session']
|
|
|
|
user = session.get('userobj', None)
|
2017-06-27 15:43:02 -05:00
|
|
|
id_session = req.cookies.get('beaker.session.id', '')
|
2018-01-09 15:13:40 -06:00
|
|
|
# ~ print ('\n\n', id_session, session, '\n\n')
|
2017-12-03 00:24:23 -06:00
|
|
|
if req.path == '/empresas' or req.path == '/values/empresas':
|
2017-11-30 00:37:13 -06:00
|
|
|
if MV:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise falcon.HTTPTemporaryRedirect('/')
|
2017-12-03 23:47:41 -06:00
|
|
|
elif id_session and req.path == '/admin':
|
|
|
|
if user is None:
|
|
|
|
raise falcon.HTTPTemporaryRedirect('/')
|
2017-12-04 00:16:53 -06:00
|
|
|
elif not user.es_admin and not user.es_superusuario:
|
2017-12-03 23:47:41 -06:00
|
|
|
raise falcon.HTTPTemporaryRedirect('/main')
|
2017-11-30 00:37:13 -06:00
|
|
|
elif not id_session and req.path != '/':
|
2017-06-27 15:43:02 -05:00
|
|
|
raise falcon.HTTPTemporaryRedirect('/')
|
|
|
|
|
|
|
|
|
|
|
|
class JSONTranslator(object):
|
|
|
|
|
2017-10-05 15:33:25 -05:00
|
|
|
#~ def process_request(self, req, resp):
|
|
|
|
#~ pass
|
|
|
|
|
2017-06-27 15:43:02 -05:00
|
|
|
def process_response(self, req, resp, resource):
|
|
|
|
if 'result' not in req.context:
|
|
|
|
return
|
2017-10-10 22:42:11 -05:00
|
|
|
if '/doc/' in req.path:
|
|
|
|
resp.body = req.context['result']
|
|
|
|
return
|
2017-06-27 15:43:02 -05:00
|
|
|
resp.body = util.dumps(req.context['result'])
|
2017-09-30 23:14:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
class ConnectionMiddleware(object):
|
|
|
|
|
|
|
|
#~ def process_request(self, req, resp):
|
|
|
|
def process_resource(self, req, resp, resource, params):
|
|
|
|
id_session = req.cookies.get('beaker.session.id', '')
|
|
|
|
session = req.env['beaker.session']
|
|
|
|
rfc = session.get('rfc', '')
|
|
|
|
if id_session and rfc:
|
|
|
|
opt = util.get_con(rfc)
|
2017-12-01 12:45:13 -06:00
|
|
|
if opt:
|
|
|
|
main.conectar(opt)
|
|
|
|
else:
|
|
|
|
raise falcon.HTTPTemporaryRedirect('/')
|
2017-09-30 23:14:44 -05:00
|
|
|
|
|
|
|
def process_response(self, req, resp, resource):
|
|
|
|
main.desconectar()
|
|
|
|
|