quick untested late night db adding function

This commit is contained in:
crunk 2024-05-28 23:27:21 +02:00
parent 2a018ab863
commit 659acc902e

View File

@ -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")