distribusi-verse: medium-tech web app content management system for the web
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

156 lines
5.2 KiB

import os
from flask import (
Blueprint,
render_template,
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 exif import Image
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
from describer.forms.redistribusi_form import ReDistribusiForm
from distribusikan.distribusi_workflow import run_distribusi, get_css_file
describer = Blueprint(
"describer",
__name__,
template_folder="templates/describe_files",
static_folder="static",
)
@describer.route("/<string:distribusiname>")
@login_required
def show_distribusi_files(distribusiname):
if not current_user.is_authenticated:
return redirect(url_for("index"))
distribusi = Distribusis.query.filter_by(
distribusiname=distribusiname
).first()
redistribusi_form = ReDistribusiForm()
distribusi_file_forms = get_distribusi_file_forms(distribusi.id)
return render_template(
"describe.html",
distribusiname=distribusiname,
redistribusi_form=redistribusi_form,
distribusi_file_forms=distribusi_file_forms,
)
@describer.route("/redistribusi/<string:distribusiname>", methods=["POST"])
@login_required
def re_distribusi_files(distribusiname):
distribusi = Distribusis.query.filter_by(
distribusiname=distribusiname
).first()
redistribusi_form = ReDistribusiForm()
if redistribusi_form.validate_on_submit():
userfolder = os.path.join("stash", distribusi.distribusiname)
cssfile = get_css_file(distribusi)
run_distribusi(userfolder, cssfile)
return redirect(
url_for(
"describer.show_distribusi_files",
distribusiname=distribusi.distribusiname,
)
)
@describer.route("/describe_file/<int:file_id>", methods=["POST"])
@login_required
def describe_file(file_id):
distribusi_file = DistribusiFiles.query.filter_by(id=file_id).first()
describe_form = DescribeFilesForm(
distribusi_file.id, distribusi_file.path, distribusi_file.type
)
save_described_file_to_db(describe_form, distribusi_file)
add_exif_to_image(describe_form, distribusi_file)
distribusi = Distribusis.query.filter_by(
id=distribusi_file.distribusi
).first()
return redirect(
url_for(
"describer.show_distribusi_files",
distribusiname=distribusi.distribusiname,
)
)
@describer.route("/stash/<path:path>")
def send_stash_file(path):
return send_from_directory("stash", path)
def get_distribusi_file_forms(distribusi_id):
distribusi_file_forms = {}
distribusi_files = DistribusiFiles.query.filter_by(
distribusi=distribusi_id
).all()
for distribusi_file in distribusi_files:
describe_form = DescribeFilesForm(
distribusi_file.id, distribusi_file.path, distribusi_file.type
)
describe_form.description.data = distribusi_file.description
describe_form.alttext.data = distribusi_file.alttext
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):
try:
if describe_form.description.data:
print(distribusi_file.id)
distribusi_file.description = describe_form.description.data
if describe_form.alttext.data:
distribusi_file.alttext = describe_form.alttext.data
if describe_form.searchtags.data:
distribusi_file.tags = describe_form.searchtags.data
db.session.add(distribusi_file)
db.session.commit()
except (InvalidRequestError, IntegrityError):
db.session.rollback()
describe_form.save.errors.append("Something went wrong!")
flash("Something went wrong!", "danger")
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")
def add_exif_to_image(describe_form, distribusi_file):
if not describe_form.alttext.data:
return
if distribusi_file.type != "image":
return
updating_image = bytearray()
with open(distribusi_file.path, "rb") as image_file:
image_bytes = image_file.read()
updating_image = Image(image_bytes)
updating_image.communication = describe_form.alttext.data
with open(distribusi_file.path, "wb") as new_image_file:
new_image_file.write(updating_image.get_file())
print(
f"{updating_image.communication} is set from form data: {describe_form.alttext.data}"
)
return