From 659acc902e077d92a77a31ac6a6a9a28bdec9a4d Mon Sep 17 00:00:00 2001 From: crunk Date: Tue, 28 May 2024 23:27:21 +0200 Subject: [PATCH] quick untested late night db adding function --- verse/describer/describe_files.py | 49 ++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/verse/describer/describe_files.py b/verse/describer/describe_files.py index b2c40d0..f980dfe 100644 --- a/verse/describer/describe_files.py +++ b/verse/describer/describe_files.py @@ -4,12 +4,22 @@ from flask import ( redirect, url_for, send_from_directory, + flash, ) from flask_login import current_user, login_required +from sqlalchemy.exc import ( + DatabaseError, + DataError, + IntegrityError, + InterfaceError, + InvalidRequestError, +) +from app import db from models.distribusi_model import Distribusis from models.distribusi_file_model import DistribusiFiles from describer.forms.describe_files_form import DescribeFilesForm + describer = Blueprint( "describer", __name__, @@ -40,7 +50,7 @@ def describe_file(file_id): describe_form = DescribeFilesForm( distribusi_file.id, distribusi_file.path, distribusi_file.type ) - + save_described_file_to_db(describe_form, distribusi_file) distribusi = Distribusis.query.filter_by( id=distribusi_file.distribusi ).first() @@ -71,3 +81,40 @@ def get_distribusi_file_forms(distribusi_id): describe_form.searchtags.data = distribusi_file.tags distribusi_file_forms[distribusi_file.id] = describe_form return distribusi_file_forms + + +def save_described_file_to_db(describe_form, distribusi_file): + if describe_form.validate_on_submit(): + try: + if describe_form.description.data: + distribusi_file.description = describe_form.description + if describe_form.alttext.data: + distribusi_file.alttext = describe_form.alttext + if describe_form.searchtags.data: + distribusi_file.tags = describe_form.searchtags + db.session.add(distribusi_file) + db.session.commit() + except InvalidRequestError: + db.session.rollback() + describe_form.save.errors.append("Something went wrong!") + flash("Something went wrong!", "danger") + except IntegrityError: + db.session.rollback() + describe_form.save.errors.append("User already exists!") + flash("User already exists!", "warning") + except DataError: + db.session.rollback() + describe_form.save.errors.append("Invalid Entry") + flash("Invalid Entry", "warning") + except InterfaceError: + db.session.rollback() + describe_form.save.errors.append( + "Error connecting to the database" + ) + flash("Error connecting to the database", "danger") + except DatabaseError: + db.session.rollback() + describe_form.save.errors.append( + "Error connecting to the database" + ) + flash("Error connecting to the database", "danger")