import os from datetime import datetime as dt, timezone as tz from flask import request, redirect, url_for, flash, render_template, Blueprint, session, current_app from flask_security import hash_password, roles_required, roles_accepted, current_user from werkzeug.utils import secure_filename from ..models.document import Document, DocumentLanguage, DocumentVersion from ..extensions import db from .document_forms import AddDocumentForm from ..utils.database import Database from ..utils.middleware import mw_before_request document_bp = Blueprint('document_bp', __name__, url_prefix='/document') @document_bp.before_request def before_request(): mw_before_request() @document_bp.route('/add_document', methods=['GET', 'POST']) @roles_accepted('Super User', 'Tenant Admin') def add_document(): form = AddDocumentForm() error = None # If the form is submitted if request.method == 'POST' and form.validate_on_submit(): file = form.file.data filename = secure_filename(file.filename) extension = filename.rsplit('.', 1)[1].lower() # Create the Document new_doc = Document() if form.name.data == '': new_doc.name = filename.rsplit('.', 1)[0] else: new_doc.name = form.name.data timestamp = dt.now(tz.utc) if form.valid_from.data or form.valid_from.data != '': new_doc.valid_from = form.valid_from.data else: new_doc.valid_from = timestamp new_doc.tenant_id = session['tenant']['id'] new_doc.created_at = timestamp new_doc.updated_at = timestamp new_doc.created_by = current_user.id new_doc.updated_by = current_user.id # Create the DocumentLanguage new_doc_lang = DocumentLanguage() if form.language.data == '': new_doc_lang.language = session['default_language'] else: new_doc_lang.language = form.language.data new_doc_lang.document = new_doc new_doc_lang.created_at = timestamp new_doc_lang.updated_at = timestamp new_doc_lang.created_by = current_user.id new_doc_lang.updated_by = current_user.id # Create the DocumentVersion new_doc_vers = DocumentVersion() new_doc_vers.document_language = new_doc_lang new_doc_vers.created_at = timestamp new_doc_vers.updated_at = timestamp new_doc_vers.created_by = current_user.id new_doc_vers.updated_by = current_user.id try: db.session.add(new_doc) db.session.add(new_doc_lang) db.session.add(new_doc_vers) db.session.commit() except Exception as e: db.session.rollback() error = e.args # Save the file and process the document if error is None: flash('Document added successfully.') new_doc_vers.file_type = extension new_doc_vers.file_name = new_doc_vers.calc_file_name() new_doc_vers.file_location = new_doc_vers.calc_file_location() upload_path = os.path.join(current_app.config['UPLOAD_FOLDER'], new_doc_vers.file_location) if not os.path.exists(upload_path): os.makedirs(upload_path, exist_ok=True) file.save(os.path.join(upload_path, new_doc_vers.file_name)) try: db.session.commit() except Exception as e: db.session.rollback() error = e.args if error is None: flash('Document saved successfully.') # TODO: processing of document to embeddings (async) flash('Document processing started.') else: flash('Error saving document.') else: flash('Error adding document.') return render_template('document/add_document.html', form=form)