Browse Source

quick untested late night db adding function

pull/12/head
crunk 5 months ago
parent
commit
659acc902e
  1. 49
      verse/describer/describe_files.py

49
verse/describer/describe_files.py

@ -4,12 +4,22 @@ from flask import (
redirect, redirect,
url_for, url_for,
send_from_directory, send_from_directory,
flash,
) )
from flask_login import current_user, login_required 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_model import Distribusis
from models.distribusi_file_model import DistribusiFiles from models.distribusi_file_model import DistribusiFiles
from describer.forms.describe_files_form import DescribeFilesForm from describer.forms.describe_files_form import DescribeFilesForm
describer = Blueprint( describer = Blueprint(
"describer", "describer",
__name__, __name__,
@ -40,7 +50,7 @@ def describe_file(file_id):
describe_form = DescribeFilesForm( describe_form = DescribeFilesForm(
distribusi_file.id, distribusi_file.path, distribusi_file.type distribusi_file.id, distribusi_file.path, distribusi_file.type
) )
save_described_file_to_db(describe_form, distribusi_file)
distribusi = Distribusis.query.filter_by( distribusi = Distribusis.query.filter_by(
id=distribusi_file.distribusi id=distribusi_file.distribusi
).first() ).first()
@ -71,3 +81,40 @@ def get_distribusi_file_forms(distribusi_id):
describe_form.searchtags.data = distribusi_file.tags describe_form.searchtags.data = distribusi_file.tags
distribusi_file_forms[distribusi_file.id] = describe_form distribusi_file_forms[distribusi_file.id] = describe_form
return distribusi_file_forms 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")

Loading…
Cancel
Save