Changes Documents - llm and languagefields on tenant, processing on documents
first version of Adding Documents (excl. embeddings)
This commit is contained in:
@@ -22,6 +22,9 @@ def before_request():
|
||||
@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)
|
||||
@@ -39,17 +42,69 @@ def add_document():
|
||||
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
|
||||
db.session.add(new_doc)
|
||||
|
||||
# TODO: Continue from here on and complete add_document
|
||||
# Create the DocumentLanguage
|
||||
new_doc_lang = DocumentLanguage()
|
||||
language = form.language.data
|
||||
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
|
||||
|
||||
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
|
||||
return redirect(url_for('document_bp.add_document'))
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user