172 lines
7.1 KiB
Python
172 lines
7.1 KiB
Python
|
|
# coding: utf-8
|
||
|
|
from flask import send_file, render_template, flash, redirect, url_for, request, Blueprint, current_app, abort, g
|
||
|
|
from flask_login import login_required, current_user
|
||
|
|
from ilab_app import db
|
||
|
|
from ilab_app.models.gestion import Evidencia, Documento, Actividad, Objetivo
|
||
|
|
from werkzeug.utils import secure_filename
|
||
|
|
|
||
|
|
|
||
|
|
evidencia = Blueprint('evidencia', __name__)
|
||
|
|
|
||
|
|
@evidencia.before_request
|
||
|
|
@login_required
|
||
|
|
def verifica_permisos():
|
||
|
|
pass
|
||
|
|
|
||
|
|
@evidencia.route("/evidencia/subir", methods=['POST'])
|
||
|
|
def subir():
|
||
|
|
import datetime
|
||
|
|
import hashlib
|
||
|
|
for key, values in request.form.items():
|
||
|
|
current_app.logger.debug('request-form: {}: {}'.format(key, values))
|
||
|
|
|
||
|
|
for key, values in request.files.items():
|
||
|
|
current_app.logger.debug('request-file: {}: {}'.format(key, values))
|
||
|
|
|
||
|
|
uuid_nill = "00000000000000000000000000000000"
|
||
|
|
try:
|
||
|
|
if len(request.form['currenthash']) < 32:
|
||
|
|
currenthash = uuid_nill
|
||
|
|
else:
|
||
|
|
currenthash = request.form['currenthash']
|
||
|
|
except Exception as e:
|
||
|
|
currenthash = uuid_nill
|
||
|
|
|
||
|
|
previtem = Documento.query.filter_by(hash=currenthash).one_or_none()
|
||
|
|
item = None
|
||
|
|
sha1 = hashlib.sha1()
|
||
|
|
|
||
|
|
|
||
|
|
if 'documento' in request.files and len(request.files['documento'].filename) > 0:
|
||
|
|
from tempfile import NamedTemporaryFile
|
||
|
|
import os
|
||
|
|
uploaded_file = request.files['documento']
|
||
|
|
current_app.logger.debug('Archivo subido: procesando {}'.format(uploaded_file.filename))
|
||
|
|
fsize = 0
|
||
|
|
with NamedTemporaryFile() as tmp:
|
||
|
|
try:
|
||
|
|
BUF_SIZE = 65536
|
||
|
|
uploaded_file.save(tmp.name)
|
||
|
|
fsize = os.path.getsize(tmp.name)
|
||
|
|
current_app.logger.debug('Archivo subido: Tamaño {} bytes'.format(fsize))
|
||
|
|
with open(tmp.name, 'rb') as f:
|
||
|
|
while True:
|
||
|
|
data = f.read(BUF_SIZE)
|
||
|
|
if not data:
|
||
|
|
break
|
||
|
|
sha1.update(data)
|
||
|
|
current_app.logger.debug('Archivo subido: hash {}'.format(sha1.hexdigest()[:32]))
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error('Error leyendo el archivo: Name({}), Error({})'.format(uploaded_file.filename, str(e)))
|
||
|
|
item = None
|
||
|
|
else:
|
||
|
|
item = Documento.query.filter_by(hash=sha1.hexdigest()[:32]).one_or_none()
|
||
|
|
if item is None:
|
||
|
|
name = secure_filename(uploaded_file.filename)
|
||
|
|
item = Documento(hash=sha1.hexdigest()[:32], nombre=name, descripcion=request.form['descripcion'].strip(), tamano=fsize, autorid=current_user.id, reemplazahash=previtem.hash)
|
||
|
|
db.session.add(item)
|
||
|
|
|
||
|
|
actid = int(request.form['aid'])
|
||
|
|
newevidencia = Evidencia(actividadid=actid, documentohash=item.hash)
|
||
|
|
db.session.add(newevidencia)
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
try:
|
||
|
|
from shutil import copyfile
|
||
|
|
current_app.logger.debug('Directorio: {}'.format(os.path.abspath(item.get_dir())))
|
||
|
|
os.makedirs(os.path.abspath(item.get_dir()))
|
||
|
|
current_app.logger.debug('Archivo: {}'.format(os.path.abspath(item.get_file())))
|
||
|
|
copyfile(tmp.name, os.path.abspath(item.get_file()))
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error('Error al guardar el archivo: Name({}), Error({})'.format(uploaded_file.filename, str(e)))
|
||
|
|
item = None
|
||
|
|
|
||
|
|
else:
|
||
|
|
current_app.logger.debug('Archivo subido: guardado {}'.format(item.get_file()))
|
||
|
|
else:
|
||
|
|
item.descripcion=request.form['descripcion'].strip()
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
elif previtem.tamano == 0 and len(request.form['url']) > 5:
|
||
|
|
sha1.update(request.form['url'].strip().encode('utf-8'))
|
||
|
|
item = Documento.query.filter_by(hash=sha1.hexdigest()[:32]).one_or_none()
|
||
|
|
if item is None:
|
||
|
|
item = Documento(hash=sha1.hexdigest()[:32], nombre=request.form['url'].strip(), descripcion=request.form['descripcion'].strip(), tamano=0, autorid=current_user.id, reemplazahash=previtem.hash)
|
||
|
|
db.session.add(item)
|
||
|
|
|
||
|
|
actid = int(request.form['aid'])
|
||
|
|
newevidencia = Evidencia(actividadid=actid, documentohash=item.hash)
|
||
|
|
db.session.add(newevidencia)
|
||
|
|
|
||
|
|
db.session.commit()
|
||
|
|
else:
|
||
|
|
item.descripcion=request.form['descripcion'].strip()
|
||
|
|
db.session.commit()
|
||
|
|
elif currenthash != uuid_nill:
|
||
|
|
previtem.descripcion = request.form['descripcion']
|
||
|
|
db.session.commit()
|
||
|
|
item = None
|
||
|
|
|
||
|
|
if item != None and item.hash != previtem.hash and not previtem.placeholder:
|
||
|
|
from ilab_app.models.system import get_comisiones
|
||
|
|
actid = int(request.form['aid'])
|
||
|
|
|
||
|
|
comisiones = get_comisiones()
|
||
|
|
|
||
|
|
evidencias = Evidencia.query.filter(Evidencia.documentohash==previtem.hash).\
|
||
|
|
join(Evidencia.actividad).join(Actividad.objetivo).\
|
||
|
|
filter(Objetivo.estado==0, Objetivo.responsableid.in_(comisiones)).all()
|
||
|
|
|
||
|
|
for evidencia in evidencias:
|
||
|
|
if evidencia.actividadid != actid:
|
||
|
|
newevidencia = Evidencia(actividadid=evidencia.actividadid, documentohash=item.hash)
|
||
|
|
db.session.add(newevidencia)
|
||
|
|
db.session.delete(evidencia)
|
||
|
|
|
||
|
|
db.session.commit()
|
||
|
|
elif item != None and item.hash != previtem.hash:
|
||
|
|
actid = int(request.form['aid'])
|
||
|
|
prevev = Evidencia.query.filter_by(actividadid=actid, documentohash=previtem.hash).one_or_none()
|
||
|
|
if prevev != None:
|
||
|
|
db.session.delete(prevev)
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return redirect(url_for(request.form['redirect']))
|
||
|
|
|
||
|
|
|
||
|
|
@evidencia.route("/evidencia/<string:filehash>")
|
||
|
|
def get(filehash=0):
|
||
|
|
from ilab_app.models.system import get_comisiones
|
||
|
|
from sqlalchemy import or_
|
||
|
|
from sqlalchemy.orm import aliased
|
||
|
|
|
||
|
|
Hijo = aliased(Objetivo)
|
||
|
|
|
||
|
|
comisiones = get_comisiones()
|
||
|
|
|
||
|
|
cani = Objetivo.query.join(Hijo, Objetivo.hijos).\
|
||
|
|
filter(or_(Objetivo.responsableid.in_(comisiones), Hijo.responsableid.in_(comisiones), Hijo.invitadosid.in_(comisiones))).\
|
||
|
|
join(Hijo.actividades).join(Actividad.evidencias).\
|
||
|
|
filter(Evidencia.documentohash==filehash).first()
|
||
|
|
|
||
|
|
if cani is None:
|
||
|
|
abort(403)
|
||
|
|
else:
|
||
|
|
doc = Documento.query.get(filehash)
|
||
|
|
if doc.placeholder:
|
||
|
|
return render_template('system/placeholder.html', doc=doc)
|
||
|
|
elif doc.tamano == 0:
|
||
|
|
return redirect(doc.nombre)
|
||
|
|
else:
|
||
|
|
try:
|
||
|
|
import os
|
||
|
|
return send_file(os.path.abspath(doc.get_file()), as_attachment=True, download_name=doc.nombre, last_modified=doc.creado)
|
||
|
|
except FileNotFoundError:
|
||
|
|
abort(404)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|