forked from elmau/empresa-libre
237 lines
6.2 KiB
Python
237 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import falcon
|
|
from middleware import get_template
|
|
|
|
|
|
class AppLogin(object):
|
|
template = 'login.html'
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
@falcon.after(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
|
|
values['rfc'] = values['rfc'].upper()
|
|
result = self._db.authenticate(values)
|
|
if result['login']:
|
|
session.save()
|
|
session['user'] = result['user']
|
|
session['rfc'] = values['rfc']
|
|
req.context['result'] = result
|
|
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 AppAdmin(object):
|
|
template = 'admin.html'
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
@falcon.after(get_template)
|
|
def on_get(self, req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
class AppMain(object):
|
|
template = 'main.html'
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
@falcon.after(get_template)
|
|
def on_get(self, req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
class AppValues(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
def on_get(self, req, resp, table):
|
|
values = req.params
|
|
req.context['result'] = self._db.get_values(table, values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_post(self, req, resp, table):
|
|
file_object = req.get_param('upload')
|
|
if file_object is None:
|
|
session = req.env['beaker.session']
|
|
values = req.params
|
|
if table == 'correo':
|
|
req.context['result'] = self._db.validate_email(values)
|
|
elif table == 'sendmail':
|
|
req.context['result'] = self._db.send_email(values, session)
|
|
else:
|
|
req.context['result'] = self._db.validate_cert(values, session)
|
|
else:
|
|
req.context['result'] = self._db.add_cert(file_object)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
class AppConfig(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
def on_get(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.get_config(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_post(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.add_config(values)
|
|
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
|
|
req.context['result'] = self._db.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
|
|
|
|
|
|
class AppProducts(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
def on_get(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.get_products(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_post(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.product(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_delete(self, req, resp):
|
|
values = req.params
|
|
if self._db.delete('product', values['id']):
|
|
resp.status = falcon.HTTP_200
|
|
else:
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class AppInvoices(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
def on_get(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.get_invoices(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_post(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.invoice(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_delete(self, req, resp):
|
|
values = req.params
|
|
if self._db.delete('invoice', values['id']):
|
|
resp.status = falcon.HTTP_200
|
|
else:
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class AppEmisor(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
def on_get(self, req, resp):
|
|
values = req.params
|
|
session = req.env['beaker.session']
|
|
req.context['result'] = self._db.get_emisor(session['rfc'])
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_post(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.emisor(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_delete(self, req, resp):
|
|
values = req.params
|
|
if self._db.delete('invoice', values['id']):
|
|
resp.status = falcon.HTTP_200
|
|
else:
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class AppFolios(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
|
|
def on_get(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.get_folios()
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_post(self, req, resp):
|
|
values = req.params
|
|
req.context['result'] = self._db.add_folios(values)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def on_delete(self, req, resp):
|
|
values = req.params
|
|
if self._db.delete('folios', values['id']):
|
|
resp.status = falcon.HTTP_200
|
|
else:
|
|
resp.status = falcon.HTTP_204
|
|
|
|
|
|
class AppDocumentos(object):
|
|
|
|
def __init__(self, db):
|
|
self._db = db
|
|
#~ self._not_json = True
|
|
|
|
def on_get(self, req, resp, type_doc, id_doc):
|
|
session = req.env['beaker.session']
|
|
req.context['result'], file_name, content_type = \
|
|
self._db.get_doc(type_doc, id_doc, session['rfc'])
|
|
resp.append_header('Content-Disposition',
|
|
'attachment; filename={}'.format(file_name))
|
|
resp.content_type = content_type
|
|
resp.status = falcon.HTTP_200
|