- Catalog functionality integrated into document and document_version views
- small bugfixes and improvements
This commit is contained in:
@@ -5,6 +5,7 @@ from babel.messages.setuptools_frontend import update_catalog
|
||||
from flask import request, redirect, flash, render_template, Blueprint, session, current_app
|
||||
from flask_security import roles_accepted, current_user
|
||||
from sqlalchemy import desc
|
||||
from sqlalchemy.orm import aliased
|
||||
from werkzeug.utils import secure_filename
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
import requests
|
||||
@@ -26,6 +27,7 @@ from common.utils.middleware import mw_before_request
|
||||
from common.utils.celery_utils import current_celery
|
||||
from common.utils.nginx_utils import prefixed_url_for
|
||||
from common.utils.view_assistants import form_validation_failed, prepare_table_for_macro, form_to_dict
|
||||
from .document_list_view import DocumentListView
|
||||
from .document_version_list_view import DocumentVersionListView
|
||||
|
||||
document_bp = Blueprint('document_bp', __name__, url_prefix='/document')
|
||||
@@ -286,22 +288,23 @@ def add_urls():
|
||||
@document_bp.route('/documents', methods=['GET', 'POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def documents():
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = request.args.get('per_page', 10, type=int)
|
||||
|
||||
pagination = get_documents_list(page, per_page)
|
||||
docs = pagination.items
|
||||
|
||||
rows = prepare_table_for_macro(docs, [('id', ''), ('name', ''), ('valid_from', ''), ('valid_to', '')])
|
||||
|
||||
return render_template('document/documents.html', rows=rows, pagination=pagination)
|
||||
view = DocumentListView(Document, 'document/documents.html', per_page=10)
|
||||
return view.get()
|
||||
|
||||
|
||||
@document_bp.route('/handle_document_selection', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_document_selection():
|
||||
document_identification = request.form['selected_row']
|
||||
doc_id = ast.literal_eval(document_identification).get('value')
|
||||
if isinstance(document_identification, int) or document_identification.isdigit():
|
||||
doc_id = int(document_identification)
|
||||
else:
|
||||
# If it's not an integer, assume it's a string representation of a dictionary
|
||||
try:
|
||||
doc_id = ast.literal_eval(document_identification).get('value')
|
||||
except (ValueError, AttributeError):
|
||||
flash('Invalid document selection.', 'error')
|
||||
return redirect(prefixed_url_for('document_bp.documents'))
|
||||
|
||||
action = request.form['action']
|
||||
|
||||
@@ -323,9 +326,25 @@ def handle_document_selection():
|
||||
@document_bp.route('/edit_document/<int:document_id>', methods=['GET', 'POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def edit_document_view(document_id):
|
||||
doc = Document.query.get_or_404(document_id)
|
||||
# Use an alias for the Catalog to avoid column name conflicts
|
||||
CatalogAlias = aliased(Catalog)
|
||||
|
||||
# Query for the document and its catalog
|
||||
result = db.session.query(Document, CatalogAlias.name.label('catalog_name')) \
|
||||
.join(CatalogAlias, Document.catalog_id == CatalogAlias.id) \
|
||||
.filter(Document.id == document_id) \
|
||||
.first_or_404()
|
||||
|
||||
doc, catalog_name = result
|
||||
|
||||
form = EditDocumentForm(obj=doc)
|
||||
|
||||
if request.method == 'GET':
|
||||
# Populate form with current values
|
||||
form.name.data = doc.name
|
||||
form.valid_from.data = doc.valid_from
|
||||
form.valid_to.data = doc.valid_to
|
||||
|
||||
if form.validate_on_submit():
|
||||
updated_doc, error = edit_document(
|
||||
document_id,
|
||||
@@ -341,7 +360,7 @@ def edit_document_view(document_id):
|
||||
else:
|
||||
form_validation_failed(request, form)
|
||||
|
||||
return render_template('document/edit_document.html', form=form, document_id=document_id)
|
||||
return render_template('document/edit_document.html', form=form, document_id=document_id, catalog_name=catalog_name)
|
||||
|
||||
|
||||
@document_bp.route('/edit_document_version/<int:document_version_id>', methods=['GET', 'POST'])
|
||||
@@ -395,7 +414,15 @@ def document_versions(document_id):
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_document_version_selection():
|
||||
document_version_identification = request.form['selected_row']
|
||||
doc_vers_id = ast.literal_eval(document_version_identification).get('value')
|
||||
if isinstance(document_version_identification, int) or document_version_identification.isdigit():
|
||||
doc_vers_id = int(document_version_identification)
|
||||
else:
|
||||
# If it's not an integer, assume it's a string representation of a dictionary
|
||||
try:
|
||||
doc_vers_id = ast.literal_eval(document_version_identification).get('value')
|
||||
except (ValueError, AttributeError):
|
||||
flash('Invalid document version selection.', 'error')
|
||||
return redirect(prefixed_url_for('document_bp.document_versions_list'))
|
||||
|
||||
action = request.form['action']
|
||||
|
||||
@@ -447,7 +474,7 @@ def refresh_all_documents():
|
||||
|
||||
|
||||
def refresh_document_view(document_id):
|
||||
new_version, result = refresh_document(document_id)
|
||||
new_version, result = refresh_document(document_id, session['tenant']['id'])
|
||||
if new_version:
|
||||
flash(f'Document refreshed. New version: {new_version.id}. Task ID: {result}', 'success')
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user