forked from elmau/empresa-libre
100 lines
2.0 KiB
Python
100 lines
2.0 KiB
Python
|
#!/usr/bin/env python
|
||
|
|
||
|
import datetime
|
||
|
import getpass
|
||
|
import json
|
||
|
import mimetypes
|
||
|
import os
|
||
|
import sqlite3
|
||
|
import uuid
|
||
|
|
||
|
import bcrypt
|
||
|
|
||
|
from settings import template_lookup, PATH_CP
|
||
|
|
||
|
|
||
|
def _get_hash(password):
|
||
|
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||
|
|
||
|
|
||
|
def validate_password(hashed, password):
|
||
|
return bcrypt.hashpw(password.encode(), hashed.encode()) == hashed.encode()
|
||
|
|
||
|
|
||
|
def get_pass():
|
||
|
password = getpass.getpass('Introduce la contraseña: ')
|
||
|
pass2 = getpass.getpass('Confirma la contraseña: ')
|
||
|
|
||
|
if password != pass2:
|
||
|
msg = 'Las contraseñas son diferentes'
|
||
|
return False, msg
|
||
|
password = password.strip()
|
||
|
if not password:
|
||
|
msg = 'La contraseña es necesaria'
|
||
|
return False, msg
|
||
|
|
||
|
return True, _get_hash(password)
|
||
|
|
||
|
|
||
|
def now():
|
||
|
return datetime.datetime.now()
|
||
|
|
||
|
|
||
|
def get_token():
|
||
|
return _get_hash(uuid.uuid4().hex)
|
||
|
|
||
|
|
||
|
def get_mimetype(path):
|
||
|
mt = mimetypes.guess_type(path)[0]
|
||
|
return mt or 'application/octet-stream'
|
||
|
|
||
|
|
||
|
def is_file(path):
|
||
|
return os.path.isfile(path)
|
||
|
|
||
|
|
||
|
def get_stream(path):
|
||
|
return get_file(path), get_size(path)
|
||
|
|
||
|
|
||
|
def get_file(path):
|
||
|
return open(path, 'rb')
|
||
|
|
||
|
|
||
|
def get_size(path):
|
||
|
return os.path.getsize(path)
|
||
|
|
||
|
|
||
|
def get_template(name, data={}):
|
||
|
#~ print ('NAME', name, data)
|
||
|
template = template_lookup.get_template(name)
|
||
|
return template.render(**data)
|
||
|
|
||
|
|
||
|
def dumps(data):
|
||
|
return json.dumps(data, default=str)
|
||
|
|
||
|
|
||
|
def clean(values):
|
||
|
for k, v in values.items():
|
||
|
if isinstance(v, str):
|
||
|
values[k] = v.strip()
|
||
|
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
|