Refactory models and controllers
This commit is contained in:
parent
65489b5a3a
commit
3a4cede680
|
@ -2,5 +2,6 @@ falcon
|
||||||
Beaker
|
Beaker
|
||||||
Mako
|
Mako
|
||||||
peewee
|
peewee
|
||||||
|
click
|
||||||
logbook
|
logbook
|
||||||
bcrypt
|
bcrypt
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import falcon
|
||||||
|
from models.main import get_cp
|
||||||
|
|
||||||
|
|
||||||
|
class AppPostalCode(object):
|
||||||
|
|
||||||
|
def on_get(self, req, resp):
|
||||||
|
values = req.params
|
||||||
|
req.context['result'] = get_cp(values['cp'])
|
||||||
|
resp.status = falcon.HTTP_200
|
|
@ -0,0 +1,78 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import falcon
|
||||||
|
|
||||||
|
from . import util
|
||||||
|
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
|
||||||
|
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(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
|
||||||
|
print ('VALUES', values)
|
||||||
|
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
|
|
@ -5,7 +5,6 @@ import getpass
|
||||||
import json
|
import json
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
@ -82,18 +81,3 @@ def clean(values):
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
|
||||||
def get_cp(cp):
|
|
||||||
con = sqlite3.connect(PATH_CP)
|
|
||||||
cursor = con.cursor()
|
|
||||||
sql = """
|
|
||||||
SELECT colonia, municipio, estado
|
|
||||||
FROM colonias, municipios, estados
|
|
||||||
WHERE colonias.id_municipio=municipios.id
|
|
||||||
AND municipios.id_estado=estados.id
|
|
||||||
AND cp=?
|
|
||||||
ORDER BY colonia"""
|
|
||||||
cursor.execute(sql, (cp,))
|
|
||||||
data = cursor.fetchall()
|
|
||||||
cursor.close()
|
|
||||||
con.close()
|
|
||||||
return data
|
|
|
@ -3,91 +3,13 @@
|
||||||
import falcon
|
import falcon
|
||||||
from beaker.middleware import SessionMiddleware
|
from beaker.middleware import SessionMiddleware
|
||||||
|
|
||||||
import util
|
from middleware import AuthMiddleware, JSONTranslator, static, handle_404
|
||||||
import middleware as MW
|
from models.db import StorageEngine
|
||||||
from models import StorageEngine, get_cp
|
from controllers.main import AppLogin, AppLogout, AppMain, AppPartners
|
||||||
|
from controllers.helper import AppPostalCode
|
||||||
from settings import DEBUG, WITH_LOGIN
|
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()
|
db = StorageEngine()
|
||||||
pc = AppPostalCode()
|
pc = AppPostalCode()
|
||||||
login = AppLogin(db)
|
login = AppLogin(db)
|
||||||
|
@ -95,22 +17,21 @@ logout = AppLogout(db)
|
||||||
main = AppMain(db)
|
main = AppMain(db)
|
||||||
partners = AppPartners(db)
|
partners = AppPartners(db)
|
||||||
|
|
||||||
api = falcon.API(middleware=[MW.AuthMiddleware(), MW.JSONTranslator()])
|
api = falcon.API(middleware=[AuthMiddleware(), JSONTranslator()])
|
||||||
api.req_options.auto_parse_form_urlencoded = True
|
api.req_options.auto_parse_form_urlencoded = True
|
||||||
api.add_sink(MW.handle_404, '')
|
api.add_sink(handle_404, '')
|
||||||
if WITH_LOGIN:
|
if WITH_LOGIN:
|
||||||
api.add_route('/', login)
|
api.add_route('/', login)
|
||||||
api.add_route('/logout', logout)
|
api.add_route('/logout', logout)
|
||||||
api.add_route('/main', main)
|
api.add_route('/main', main)
|
||||||
api.add_route('/partners', partners)
|
api.add_route('/partners', partners)
|
||||||
api.add_route('/cp', pc)
|
api.add_route('/pc', pc)
|
||||||
|
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
api.add_sink(MW.static, '/static')
|
api.add_sink(static, '/static')
|
||||||
|
|
||||||
|
|
||||||
# Configure the SessionMiddleware
|
|
||||||
session_opts = {
|
session_opts = {
|
||||||
'session.type': 'file',
|
'session.type': 'file',
|
||||||
'session.cookie_expires': True,
|
'session.cookie_expires': True,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import falcon
|
import falcon
|
||||||
import util
|
from controllers import util
|
||||||
from settings import PATH_STATIC
|
from settings import PATH_STATIC
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,263 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import util
|
|
||||||
from peewee import *
|
|
||||||
from conf import DATABASE
|
|
||||||
from settings import log
|
|
||||||
|
|
||||||
|
|
||||||
database = DATABASE
|
|
||||||
class BaseModel(Model):
|
|
||||||
class Meta:
|
|
||||||
database = database
|
|
||||||
|
|
||||||
|
|
||||||
class Users(BaseModel):
|
|
||||||
username = CharField(max_length=50, unique=True)
|
|
||||||
first_name = CharField(max_length=50, default='')
|
|
||||||
last_name = CharField(max_length=50, default='')
|
|
||||||
email = CharField(max_length=200, default='')
|
|
||||||
password = CharField(max_length=100)
|
|
||||||
is_superuser = BooleanField(default=False)
|
|
||||||
is_admin = BooleanField(default=False)
|
|
||||||
is_staff = BooleanField(default=False)
|
|
||||||
is_active = BooleanField(default=True)
|
|
||||||
date_joined = DateTimeField(default=util.now)
|
|
||||||
last_login = DateTimeField(null=True)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
t = '{} {} ({})'
|
|
||||||
return t.format(self.first_name, self.last_name, self.username)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
order_by = ('username',)
|
|
||||||
|
|
||||||
|
|
||||||
class Clients(BaseModel):
|
|
||||||
id = IntegerField(db_column='cliente_id', primary_key=True)
|
|
||||||
cia = CharField(max_length=11, default='', index=True)
|
|
||||||
rfc = CharField(max_length=13, index=True)
|
|
||||||
name = CharField(db_column='nombre', max_length=60)
|
|
||||||
street = CharField(db_column='calle', max_length=60, default='')
|
|
||||||
num_ext = CharField(db_column='noExterior', max_length=10, default='')
|
|
||||||
num_int = CharField(db_column='noInterior', max_length=10, default='')
|
|
||||||
colonia = CharField(max_length=60, default='')
|
|
||||||
localidad = CharField(max_length=60, default='')
|
|
||||||
municipio = CharField(max_length=60, default='')
|
|
||||||
state = CharField(db_column='estado', max_length=40, default='')
|
|
||||||
country = CharField(db_column='pais', max_length=30, default='')
|
|
||||||
postal_code = CharField(db_column='codigoPostal', max_length=6, default='')
|
|
||||||
email = CharField(db_column='correos', max_length=250, default='')
|
|
||||||
contact = CharField(db_column='nombre_contacto', max_length=60, default='')
|
|
||||||
phone = CharField(db_column='telefono', max_length=40, default='')
|
|
||||||
CUS_ErpId = FixedCharField(max_length=20, default='')
|
|
||||||
CUS_SatClas = FixedCharField(max_length=2, default='')
|
|
||||||
CUS_SatClasNombre = FixedCharField(max_length=50, default='')
|
|
||||||
CUS_DefaultCurrency = FixedCharField(max_length=3, default='')
|
|
||||||
CUS_DefaultCurrencyNombre = FixedCharField(max_length=120, default='')
|
|
||||||
CUS_AccHabe = FixedCharField(max_length=20, default='')
|
|
||||||
CUS_AccHabeNombre = FixedCharField(max_length=60, default='')
|
|
||||||
CUS_AccDebe = FixedCharField(max_length=20, default='')
|
|
||||||
CUS_AccDebeNombre = FixedCharField(max_length=60, default='')
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
t = '{} ({})'
|
|
||||||
return t.format(self.name, self.rfc)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
db_table = 'clientes'
|
|
||||||
order_by = ('name',)
|
|
||||||
|
|
||||||
|
|
||||||
class StorageEngine(object):
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
database.connect()
|
|
||||||
|
|
||||||
def authenticate(self, username, password):
|
|
||||||
data = {'login': False, 'msg': 'No Autorizado', 'user': ''}
|
|
||||||
try:
|
|
||||||
obj = Users.get(Users.username==username, Users.is_active==True)
|
|
||||||
except Users.DoesNotExist:
|
|
||||||
return data
|
|
||||||
|
|
||||||
if not util.validate_password(obj.password, password):
|
|
||||||
return data
|
|
||||||
|
|
||||||
obj.last_login = util.now()
|
|
||||||
obj.save()
|
|
||||||
data['msg'] = ''
|
|
||||||
data['login'] = True
|
|
||||||
data['user'] = str(obj)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def get_partners(self, values):
|
|
||||||
if values:
|
|
||||||
id = int(values['id'])
|
|
||||||
row = Clients.select().where(Clients.id==id).dicts()[0]
|
|
||||||
return row
|
|
||||||
else:
|
|
||||||
rows = Clients.select(
|
|
||||||
Clients.id, Clients.cia, Clients.rfc, Clients.name).dicts()
|
|
||||||
return {'ok': True, 'rows': tuple(rows)}
|
|
||||||
|
|
||||||
def new_partner(self, values):
|
|
||||||
fields = util.clean(values)
|
|
||||||
fields['rfc'] = fields['rfc'].upper()
|
|
||||||
Clients.create(**fields)
|
|
||||||
#~ Wend custom ID
|
|
||||||
obj = Clients.select().order_by(Clients.id.desc()).get()
|
|
||||||
row = {
|
|
||||||
'id': obj.id,
|
|
||||||
'cia': obj.cia,
|
|
||||||
'rfc': obj.rfc,
|
|
||||||
'name': obj.name,
|
|
||||||
}
|
|
||||||
data = {'ok': True, 'row': row, 'new': True}
|
|
||||||
return data
|
|
||||||
|
|
||||||
def update_partner(self, values):
|
|
||||||
id = int(values['id'])
|
|
||||||
del values['id']
|
|
||||||
fields = util.clean(values)
|
|
||||||
fields['rfc'] = fields['rfc'].upper()
|
|
||||||
q = Clients.update(**values).where(Clients.id==id)
|
|
||||||
q.execute()
|
|
||||||
row = {
|
|
||||||
'id': id,
|
|
||||||
'cia': fields['cia'],
|
|
||||||
'rfc': fields['rfc'],
|
|
||||||
'name': fields['name'],
|
|
||||||
}
|
|
||||||
data = {'ok': True, 'row': row, 'new': False}
|
|
||||||
return data
|
|
||||||
|
|
||||||
def delete_partner(self, id):
|
|
||||||
q = Clients.delete().where(Clients.id==id)
|
|
||||||
return bool(q.execute())
|
|
||||||
|
|
||||||
|
|
||||||
def get_cp(cp):
|
|
||||||
data = {}
|
|
||||||
rows = util.get_cp(cp)
|
|
||||||
if rows:
|
|
||||||
data = {
|
|
||||||
'estado': rows[0][2],
|
|
||||||
'municipio': rows[0][1],
|
|
||||||
}
|
|
||||||
if len(rows) == 1:
|
|
||||||
data['colonia'] = rows[0][0]
|
|
||||||
else:
|
|
||||||
data['colonia'] = [r[0] for r in rows]
|
|
||||||
#~ print ('DATA', data)
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def _connect():
|
|
||||||
global database
|
|
||||||
log.info('Connect to DB...')
|
|
||||||
database.connect()
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def create_tables():
|
|
||||||
_connect()
|
|
||||||
tables = [Users, Clients]
|
|
||||||
database.create_tables(tables, True)
|
|
||||||
log.info('Create tables OK...')
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def migrate_tables():
|
|
||||||
_connect()
|
|
||||||
log.info('Migrate tables OK...')
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def create_superuser(username):
|
|
||||||
_connect()
|
|
||||||
ok, password = util.get_pass()
|
|
||||||
if not ok:
|
|
||||||
log.error(password)
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
obj = Users.create(username=username, password=password, is_superuser=True)
|
|
||||||
except IntegrityError:
|
|
||||||
msg = 'El usuario ya existe'
|
|
||||||
log.error(msg)
|
|
||||||
return
|
|
||||||
|
|
||||||
log.info('Create super user OK...')
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def change_password(username):
|
|
||||||
_connect()
|
|
||||||
try:
|
|
||||||
obj = Users.get(username=username)
|
|
||||||
except Users.DoesNotExist:
|
|
||||||
msg = 'El usuario no existe'
|
|
||||||
log.error(msg)
|
|
||||||
return
|
|
||||||
|
|
||||||
ok, password = util.get_pass()
|
|
||||||
if not ok:
|
|
||||||
log.error(password)
|
|
||||||
return
|
|
||||||
|
|
||||||
obj.password = password
|
|
||||||
obj.save()
|
|
||||||
|
|
||||||
log.info('Change password OK...')
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def main(opt, username=''):
|
|
||||||
if opt == '-c':
|
|
||||||
create_tables()
|
|
||||||
elif opt == '-m':
|
|
||||||
migrate_tables()
|
|
||||||
elif opt == '-su':
|
|
||||||
create_superuser(username)
|
|
||||||
elif opt == '-cc':
|
|
||||||
change_password(username)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
opt = sys.argv
|
|
||||||
if len(opt) == 1:
|
|
||||||
msg = 'Agrega un argumento'
|
|
||||||
log.error(msg)
|
|
||||||
sys.exit(0)
|
|
||||||
ov = ('-h', '-c', '-m', '-su', '-cc')
|
|
||||||
if not opt[1] in ov:
|
|
||||||
msg = 'Opción no válida. Usa -h para ver las opciones'
|
|
||||||
log.error(msg)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
if opt[1] == '-h':
|
|
||||||
msg = '-h Muestra esta ayuda.\n\t' \
|
|
||||||
'-c Crea tablas.\n\t' \
|
|
||||||
'-m Migra tablas.\n\t' \
|
|
||||||
'-su USERNAME Crea Super Usuario.\n\t' \
|
|
||||||
'-cc USERNAME Cambiar contraseña.\n\t'
|
|
||||||
log.info(msg)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
if opt[1] == '-su' or opt[1] == '-cc' :
|
|
||||||
if len(opt) == 2:
|
|
||||||
msg = 'Falta el argumento: nombre de usuario'
|
|
||||||
log.error(msg)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
try:
|
|
||||||
main(opt[1], opt[2])
|
|
||||||
sys.exit(0)
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
msg = 'La contraseña es necesaria'
|
|
||||||
log.error(msg)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
main(opt[1])
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from . import main
|
||||||
|
|
||||||
|
|
||||||
|
class StorageEngine(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
main.connect()
|
||||||
|
|
||||||
|
def authenticate(self, username, password):
|
||||||
|
return main.authenticate(username, password)
|
||||||
|
|
||||||
|
def get_partners(self, values):
|
||||||
|
return main.get_partners(values)
|
||||||
|
|
||||||
|
def new_partner(self, values):
|
||||||
|
return main.new_partner(values)
|
||||||
|
|
||||||
|
def update_partner(self, values):
|
||||||
|
id = int(values['id'])
|
||||||
|
del values['id']
|
||||||
|
fields = util.clean(values)
|
||||||
|
fields['rfc'] = fields['rfc'].upper()
|
||||||
|
q = Clients.update(**values).where(Clients.id==id)
|
||||||
|
q.execute()
|
||||||
|
row = {
|
||||||
|
'id': id,
|
||||||
|
'cia': fields['cia'],
|
||||||
|
'rfc': fields['rfc'],
|
||||||
|
'name': fields['name'],
|
||||||
|
}
|
||||||
|
data = {'ok': True, 'row': row, 'new': False}
|
||||||
|
return data
|
||||||
|
|
||||||
|
def delete_partner(self, id):
|
||||||
|
q = Clients.delete().where(Clients.id==id)
|
||||||
|
return bool(q.execute())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,303 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
import click
|
||||||
|
from peewee import *
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import os, sys
|
||||||
|
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
sys.path.insert(0, parent_dir)
|
||||||
|
|
||||||
|
from controllers import util
|
||||||
|
from conf import DATABASE, __version__
|
||||||
|
from settings import log, PATH_CP
|
||||||
|
|
||||||
|
|
||||||
|
database = DATABASE
|
||||||
|
class BaseModel(Model):
|
||||||
|
class Meta:
|
||||||
|
database = database
|
||||||
|
|
||||||
|
|
||||||
|
def connect():
|
||||||
|
global database
|
||||||
|
database.connect()
|
||||||
|
log.info('Conectado a la BD...')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class Configuration(BaseModel):
|
||||||
|
key = FixedCharField(max_length=50)
|
||||||
|
value = FixedCharField(max_length=100)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
order_by = ('key',)
|
||||||
|
indexes = (
|
||||||
|
(('key', 'value'), True),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Tags(BaseModel):
|
||||||
|
tag = FixedCharField(max_length=25, index=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
order_by = ('tag',)
|
||||||
|
|
||||||
|
|
||||||
|
class Users(BaseModel):
|
||||||
|
username = CharField(max_length=50, unique=True)
|
||||||
|
first_name = CharField(max_length=50, default='')
|
||||||
|
last_name = CharField(max_length=50, default='')
|
||||||
|
email = CharField(max_length=200, default='')
|
||||||
|
password = CharField(max_length=100)
|
||||||
|
is_superuser = BooleanField(default=False)
|
||||||
|
is_admin = BooleanField(default=False)
|
||||||
|
is_staff = BooleanField(default=False)
|
||||||
|
is_active = BooleanField(default=True)
|
||||||
|
date_joined = DateTimeField(default=util.now)
|
||||||
|
last_login = DateTimeField(null=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
t = '{} {} ({})'
|
||||||
|
return t.format(self.first_name, self.last_name, self.username)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
order_by = ('username',)
|
||||||
|
|
||||||
|
|
||||||
|
class Clients(BaseModel):
|
||||||
|
person_type = IntegerField(default=0)
|
||||||
|
rfc = FixedCharField(max_length=13, index=True)
|
||||||
|
name = CharField(max_length=1000, index=True)
|
||||||
|
street = FixedCharField(max_length=200, default='')
|
||||||
|
num_ext = FixedCharField(max_length=25, default='')
|
||||||
|
num_int = FixedCharField(max_length=25, default='')
|
||||||
|
postal_code = FixedCharField(max_length=15, default='')
|
||||||
|
colonia = FixedCharField(max_length=100, default='')
|
||||||
|
municipio = FixedCharField(max_length=100, default='')
|
||||||
|
state = FixedCharField(max_length=100, default='')
|
||||||
|
country = FixedCharField(max_length=100, default='')
|
||||||
|
conditions_pay = FixedCharField(max_length=250, default='')
|
||||||
|
days_pay = IntegerField(default=0)
|
||||||
|
business_days = BooleanField(default=False)
|
||||||
|
is_active = BooleanField(default=True)
|
||||||
|
commercial_name = CharField(max_length=1000, default='')
|
||||||
|
phones = CharField(max_length=500, default='')
|
||||||
|
web_page = CharField(max_length=500, default='')
|
||||||
|
invoice_email = CharField(max_length=500, default='')
|
||||||
|
is_client = BooleanField(default=False)
|
||||||
|
is_supplier = BooleanField(default=False)
|
||||||
|
is_ong = BooleanField(default=False)
|
||||||
|
account_client = FixedCharField(max_length=100, default='')
|
||||||
|
account_supplier = FixedCharField(max_length=100, default='')
|
||||||
|
notes = CharField(max_length=5000)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
t = '{} ({})'
|
||||||
|
return t.format(self.name, self.rfc)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
order_by = ('name',)
|
||||||
|
indexes = (
|
||||||
|
(('rfc', 'name'), True),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ClientsTags(BaseModel):
|
||||||
|
client = ForeignKeyField(Clients)
|
||||||
|
tag = ForeignKeyField(Tags)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
indexes = (
|
||||||
|
(('client', 'tag'), True),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def authenticate(username, password):
|
||||||
|
data = {'login': False, 'msg': 'No Autorizado', 'user': ''}
|
||||||
|
try:
|
||||||
|
obj = Users.get(Users.username==username, Users.is_active==True)
|
||||||
|
except Users.DoesNotExist:
|
||||||
|
return data
|
||||||
|
|
||||||
|
if not util.validate_password(obj.password, password):
|
||||||
|
return data
|
||||||
|
|
||||||
|
obj.last_login = util.now()
|
||||||
|
obj.save()
|
||||||
|
data['msg'] = ''
|
||||||
|
data['login'] = True
|
||||||
|
data['user'] = str(obj)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def get_partners(values):
|
||||||
|
if values:
|
||||||
|
id = int(values['id'])
|
||||||
|
row = Clients.select().where(Clients.id==id).dicts()[0]
|
||||||
|
return row
|
||||||
|
|
||||||
|
rows = Clients.select(Clients.id, Clients.rfc, Clients.name).dicts()
|
||||||
|
return {'ok': True, 'rows': tuple(rows)}
|
||||||
|
|
||||||
|
|
||||||
|
def new_partner(values):
|
||||||
|
fields = util.clean(values)
|
||||||
|
fields['rfc'] = fields['rfc'].upper()
|
||||||
|
obj = Clients.create(**fields)
|
||||||
|
row = {
|
||||||
|
'id': obj.id,
|
||||||
|
'cia': obj.cia,
|
||||||
|
'rfc': obj.rfc,
|
||||||
|
'name': obj.name,
|
||||||
|
}
|
||||||
|
data = {'ok': True, 'row': row, 'new': True}
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def get_cp(cp):
|
||||||
|
con = sqlite3.connect(PATH_CP)
|
||||||
|
cursor = con.cursor()
|
||||||
|
sql = """
|
||||||
|
SELECT colonia, municipio, estado
|
||||||
|
FROM colonias, municipios, estados
|
||||||
|
WHERE colonias.id_municipio=municipios.id
|
||||||
|
AND municipios.id_estado=estados.id
|
||||||
|
AND cp=?
|
||||||
|
ORDER BY colonia"""
|
||||||
|
cursor.execute(sql, (cp,))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
cursor.close()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
data = {}
|
||||||
|
if rows:
|
||||||
|
data = {
|
||||||
|
'estado': rows[0][2],
|
||||||
|
'municipio': rows[0][1],
|
||||||
|
}
|
||||||
|
if len(rows) == 1:
|
||||||
|
data['colonia'] = rows[0][0]
|
||||||
|
else:
|
||||||
|
data['colonia'] = [r[0] for r in rows]
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def _init_values():
|
||||||
|
data = (
|
||||||
|
{'key': 'version', 'value': __version__},
|
||||||
|
{'key': 'rfc_publico', 'value': 'XAXX010101000'},
|
||||||
|
{'key': 'rfc_extranjero', 'value': 'XEXX010101000'},
|
||||||
|
)
|
||||||
|
for row in data:
|
||||||
|
try:
|
||||||
|
Configuration.create(**row)
|
||||||
|
except IntegrityError:
|
||||||
|
pass
|
||||||
|
log.info('Valores iniciales insertados...')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def create_tables():
|
||||||
|
connect()
|
||||||
|
tables = [Configuration, Tags, Users, Clients, ClientsTags]
|
||||||
|
database.create_tables(tables, True)
|
||||||
|
log.info('Tablas creadas correctamente...')
|
||||||
|
_init_values()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_tables():
|
||||||
|
connect()
|
||||||
|
log.info('Tablas migradas correctamente...')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def create_superuser():
|
||||||
|
connect()
|
||||||
|
username = input('Introduce el nuevo nombre para el superusuario: ').strip()
|
||||||
|
if not username:
|
||||||
|
msg = 'El nombre de usuario es requerido'
|
||||||
|
log.erro(msg)
|
||||||
|
return
|
||||||
|
ok, password = util.get_pass()
|
||||||
|
if not ok:
|
||||||
|
log.error(password)
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
obj = Users.create(username=username, password=password, is_superuser=True)
|
||||||
|
except IntegrityError:
|
||||||
|
msg = 'El usuario ya existe'
|
||||||
|
log.error(msg)
|
||||||
|
return
|
||||||
|
|
||||||
|
log.info('SuperUsuario creado correctamente...')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def change_password():
|
||||||
|
connect()
|
||||||
|
username = input('Introduce el nombre de usuario: ').strip()
|
||||||
|
if not username:
|
||||||
|
msg = 'El nombre de usuario es requerido'
|
||||||
|
log.error(msg)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
obj = Users.get(username=username)
|
||||||
|
except Users.DoesNotExist:
|
||||||
|
msg = 'El usuario no existe'
|
||||||
|
log.error(msg)
|
||||||
|
return
|
||||||
|
|
||||||
|
ok, password = util.get_pass()
|
||||||
|
if not ok:
|
||||||
|
log.error(password)
|
||||||
|
return
|
||||||
|
|
||||||
|
obj.password = password
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
log.info('Contraseña cambiada correctamente...')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
|
||||||
|
help_create_tables = 'Crea las tablas en la base de datos'
|
||||||
|
help_migrate_db = 'Migra las tables en la base de datos'
|
||||||
|
help_superuser = 'Crea un nuevo super usuario'
|
||||||
|
help_change_pass = 'Cambia la contraseña a un usuario'
|
||||||
|
|
||||||
|
@click.command(context_settings=CONTEXT_SETTINGS)
|
||||||
|
@click.option('-bd', '--iniciar-bd',help=help_create_tables,
|
||||||
|
is_flag=True, default=False)
|
||||||
|
@click.option('-m', '--migrar-bd', help=help_migrate_db,
|
||||||
|
is_flag=True, default=False)
|
||||||
|
@click.option('-ns', '--nuevo-superusuario', help=help_superuser,
|
||||||
|
is_flag=True, default=False)
|
||||||
|
@click.option('-cc', '--cambiar-contraseña', help=help_change_pass,
|
||||||
|
is_flag=True, default=False)
|
||||||
|
def main(iniciar_bd, migrar_bd, nuevo_superusuario, cambiar_contraseña):
|
||||||
|
opt = locals()
|
||||||
|
|
||||||
|
if opt['iniciar_bd']:
|
||||||
|
create_tables()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
if opt['migrar_bd']:
|
||||||
|
migrate_tables()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
if opt['nuevo_superusuario']:
|
||||||
|
create_superuser()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
if opt['cambiar_contraseña']:
|
||||||
|
change_password()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
function cmd_new_partner_click(id, e, node){
|
function cmd_new_partner_click(id, e, node){
|
||||||
$$('form_partner').setValues({
|
$$('form_partner').setValues({
|
||||||
id:0, country:'México', opt_person_type: 1, is_active: true})
|
id: 0, country: 'México', person_type: 1, is_active: true})
|
||||||
$$('grid_partners').clearSelection()
|
$$('grid_partners').clearSelection()
|
||||||
$$('multi_partners').setValue('partners_new')
|
$$('multi_partners').setValue('partners_new')
|
||||||
};
|
};
|
||||||
|
@ -83,7 +83,7 @@ function cmd_save_partner_click(id, e, node){
|
||||||
}
|
}
|
||||||
|
|
||||||
var values = form.getValues();
|
var values = form.getValues();
|
||||||
webix.ajax().post("partners", values, {
|
webix.ajax().post('/partners', values, {
|
||||||
error:function(text, data, XmlHttpRequest){
|
error:function(text, data, XmlHttpRequest){
|
||||||
msg = 'Ocurrio un error, consulta a soporte técnico';
|
msg = 'Ocurrio un error, consulta a soporte técnico';
|
||||||
webix.message({type:'error', text:msg});
|
webix.message({type:'error', text:msg});
|
||||||
|
@ -129,7 +129,7 @@ function postal_code_key_up(){
|
||||||
var value = this.getValue()
|
var value = this.getValue()
|
||||||
var msg = ''
|
var msg = ''
|
||||||
if( value.length == 5 ){
|
if( value.length == 5 ){
|
||||||
webix.ajax().get('/cp', {cp:value}, {
|
webix.ajax().get('/pc', {cp:value}, {
|
||||||
error: function(text, data, xhr) {
|
error: function(text, data, xhr) {
|
||||||
webix.message({type:'error', text:'Error al consultar el C.P.'})
|
webix.message({type:'error', text:'Error al consultar el C.P.'})
|
||||||
},
|
},
|
||||||
|
|
|
@ -53,8 +53,8 @@ var persons_type = [
|
||||||
|
|
||||||
|
|
||||||
var controls_fiscales = [
|
var controls_fiscales = [
|
||||||
{template: 'Tipo de Persona', type: 'section' },
|
{template: 'Tipo de Persona', type: 'section'},
|
||||||
{view: 'radio', id: 'opt_person_type', name: 'opt_person_type', label: 'Tipos: ',
|
{view: 'radio', name: 'person_type', label: 'Tipos: ',
|
||||||
labelWidth: 150, value: 1, options: persons_type, required: true,
|
labelWidth: 150, value: 1, options: persons_type, required: true,
|
||||||
invalidMessage: 'El Tipo de Persona es requerido'},
|
invalidMessage: 'El Tipo de Persona es requerido'},
|
||||||
{template: 'Dirección Fiscal', type: 'section'},
|
{template: 'Dirección Fiscal', type: 'section'},
|
||||||
|
@ -76,18 +76,16 @@ var controls_fiscales = [
|
||||||
value: 'México', readonly: true},
|
value: 'México', readonly: true},
|
||||||
{template: 'Condiciones Comerciales', type: 'section'},
|
{template: 'Condiciones Comerciales', type: 'section'},
|
||||||
{cols: [
|
{cols: [
|
||||||
{view: 'combo', id: 'cbo_method_pay', name: 'cbo_method_pay',
|
{view: 'combo', id: 'form_pay', name: 'form_pay',
|
||||||
label: 'Método de Pago: ', editable: false, required: true},
|
label: 'Forma de Pago: ', editable: false, required: true},
|
||||||
{view: 'text', id: 'account_pay', name: 'account_pay',
|
{view: 'combo', id: 'conditions_pay', name: 'conditions_pay',
|
||||||
label: 'Cuenta de Pago: '},
|
label: 'Condiciones de Pago: '},
|
||||||
]},
|
]},
|
||||||
{cols: [
|
{cols: [
|
||||||
{view: 'combo', id: 'cbo_conditions_pay', name: 'cbo_conditions_pay',
|
{view: 'counter', name: 'days_pay', label: 'Días de pago', step:5, value: 0, min: 0,
|
||||||
label: 'Condiciones de Pago: '},
|
max: 365, tooltip: 'Permite calcular las fechas de pago'},
|
||||||
{view: 'counter', label: 'Días de pago', step:5, value: 0, min: 0,
|
{view: 'checkbox', id: 'business_days', name: 'business_days',
|
||||||
max: 365, maxWidth: 250, tooltip: 'Permite calcular las fechas de pago'},
|
label: 'Hábiles: ', value: false},
|
||||||
{view: 'checkbox', id: 'habiles', name: 'habiles',
|
|
||||||
label: 'Hábiles: ', maxWidth: 200, value: false},
|
|
||||||
]}
|
]}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -95,8 +93,9 @@ var controls_fiscales = [
|
||||||
var controls_others = [
|
var controls_others = [
|
||||||
{view: 'checkbox', id: 'is_active', name: 'is_active', label: 'Activo: ',
|
{view: 'checkbox', id: 'is_active', name: 'is_active', label: 'Activo: ',
|
||||||
value: true, bottomLabel: ' Se recomienda solo desactivar y no eliminar'},
|
value: true, bottomLabel: ' Se recomienda solo desactivar y no eliminar'},
|
||||||
{view: 'text', id: 'name2', name: 'name2', label: 'Nombre Comercial: '},
|
{view: 'text', id: 'commercial_name', name: 'commercial_name', label: 'Nombre Comercial: '},
|
||||||
{view: 'text', id: 'page_web', name: 'page_web', label: 'Página Web: '},
|
{view: 'text', id: 'phones', name: 'phones', label: 'Teléfonos: '},
|
||||||
|
{view: 'text', id: 'web_page', name: 'web_page', label: 'Página Web: '},
|
||||||
{view: 'text', id: 'invoice_email', name: 'invoice_email',
|
{view: 'text', id: 'invoice_email', name: 'invoice_email',
|
||||||
label: 'Correos para Facturas: ', tooltip: 'Separados por comas'},
|
label: 'Correos para Facturas: ', tooltip: 'Separados por comas'},
|
||||||
{cols: [
|
{cols: [
|
||||||
|
|
Loading…
Reference in New Issue