2024-06-23 18:24:05 +02:00
|
|
|
import os
|
2024-05-27 18:54:55 +02:00
|
|
|
from flask import (
|
2024-05-28 23:02:17 +02:00
|
|
|
Blueprint,
|
|
|
|
render_template,
|
|
|
|
redirect,
|
|
|
|
url_for,
|
|
|
|
send_from_directory,
|
2024-05-28 23:27:21 +02:00
|
|
|
flash,
|
2024-05-27 18:54:55 +02:00
|
|
|
)
|
2024-05-27 14:25:25 +02:00
|
|
|
from flask_login import current_user, login_required
|
2024-05-28 23:27:21 +02:00
|
|
|
from sqlalchemy.exc import (
|
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
IntegrityError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
|
|
|
)
|
|
|
|
from app import db
|
2024-05-19 11:13:21 +02:00
|
|
|
from models.distribusi_model import Distribusis
|
|
|
|
from models.distribusi_file_model import DistribusiFiles
|
2024-05-27 17:25:27 +02:00
|
|
|
from describer.forms.describe_files_form import DescribeFilesForm
|
2024-06-23 14:42:30 +02:00
|
|
|
from describer.forms.redistribusi_form import ReDistribusiForm
|
2024-06-23 18:24:05 +02:00
|
|
|
from distribusikan.distribusi_workflow import run_distribusi, get_css_file
|
2024-05-28 23:27:21 +02:00
|
|
|
|
2024-05-19 11:13:21 +02:00
|
|
|
describer = Blueprint(
|
2024-05-28 23:02:17 +02:00
|
|
|
"describer",
|
|
|
|
__name__,
|
|
|
|
template_folder="templates/describe_files",
|
|
|
|
static_folder="static",
|
2024-05-19 11:13:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@describer.route("/<string:distribusiname>")
|
2024-05-27 14:25:25 +02:00
|
|
|
@login_required
|
2024-05-27 17:25:27 +02:00
|
|
|
def show_distribusi_files(distribusiname):
|
2024-05-28 23:02:17 +02:00
|
|
|
if not current_user.is_authenticated:
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
distribusi = Distribusis.query.filter_by(
|
|
|
|
distribusiname=distribusiname
|
|
|
|
).first()
|
2024-06-23 14:42:30 +02:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-06-23 18:24:05 +02:00
|
|
|
@describer.route("/redistribusi/<string:distribusiname>", methods=["POST"])
|
2024-06-23 14:42:30 +02:00
|
|
|
@login_required
|
|
|
|
def re_distribusi_files(distribusiname):
|
|
|
|
distribusi = Distribusis.query.filter_by(
|
|
|
|
distribusiname=distribusiname
|
|
|
|
).first()
|
|
|
|
redistribusi_form = ReDistribusiForm()
|
2024-06-23 18:24:05 +02:00
|
|
|
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,
|
|
|
|
)
|
2024-05-28 23:02:17 +02:00
|
|
|
)
|
2024-05-27 14:25:25 +02:00
|
|
|
|
|
|
|
|
2024-05-27 17:25:27 +02:00
|
|
|
@describer.route("/describe_file/<int:file_id>", methods=["POST"])
|
2024-05-27 14:25:25 +02:00
|
|
|
@login_required
|
2024-05-27 17:25:27 +02:00
|
|
|
def describe_file(file_id):
|
2024-05-28 23:02:17 +02:00
|
|
|
distribusi_file = DistribusiFiles.query.filter_by(id=file_id).first()
|
|
|
|
describe_form = DescribeFilesForm(
|
|
|
|
distribusi_file.id, distribusi_file.path, distribusi_file.type
|
|
|
|
)
|
2024-05-28 23:27:21 +02:00
|
|
|
save_described_file_to_db(describe_form, distribusi_file)
|
2024-06-23 22:31:12 +02:00
|
|
|
add_alttext_to_file(describe_form, distribusi_file)
|
2024-06-27 11:03:35 +02:00
|
|
|
add_description_to_file(describe_form, distribusi_file)
|
2024-05-28 23:02:17 +02:00
|
|
|
distribusi = Distribusis.query.filter_by(
|
|
|
|
id=distribusi_file.distribusi
|
|
|
|
).first()
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"describer.show_distribusi_files",
|
|
|
|
distribusiname=distribusi.distribusiname,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-05-27 17:25:27 +02:00
|
|
|
|
2024-05-27 18:54:55 +02:00
|
|
|
@describer.route("/stash/<path:path>")
|
|
|
|
def send_stash_file(path):
|
2024-05-28 23:02:17 +02:00
|
|
|
return send_from_directory("stash", path)
|
2024-05-27 18:54:55 +02:00
|
|
|
|
2024-05-27 17:25:27 +02:00
|
|
|
|
|
|
|
def get_distribusi_file_forms(distribusi_id):
|
2024-05-28 23:02:17 +02:00
|
|
|
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
|
2024-05-28 23:27:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def save_described_file_to_db(describe_form, distribusi_file):
|
2024-06-02 21:49:27 +02:00
|
|
|
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")
|
2024-06-23 14:19:38 +02:00
|
|
|
|
|
|
|
|
2024-06-23 22:31:12 +02:00
|
|
|
def add_alttext_to_file(describe_form, distribusi_file):
|
2024-06-23 14:19:38 +02:00
|
|
|
if not describe_form.alttext.data:
|
|
|
|
return
|
2024-07-08 21:07:00 +02:00
|
|
|
filename = os.path.join("stash", distribusi_file.path)
|
|
|
|
filename_no_ext = os.path.splitext(filename)[0]
|
2024-06-27 11:03:35 +02:00
|
|
|
with open(f"{filename_no_ext}_alttext.txt", "w") as alttext_file:
|
2024-06-23 22:31:12 +02:00
|
|
|
alttext_file.write(describe_form.alttext.data)
|
|
|
|
return
|
2024-06-27 11:03:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def add_description_to_file(describe_form, distribusi_file):
|
|
|
|
if not describe_form.description.data:
|
|
|
|
return
|
2024-07-08 21:07:00 +02:00
|
|
|
filename = os.path.join("stash", distribusi_file.path)
|
|
|
|
filename_no_ext = os.path.splitext(filename)[0]
|
2024-06-27 11:03:35 +02:00
|
|
|
with open(
|
|
|
|
f"{filename_no_ext}_dv_description.txt", "w"
|
|
|
|
) as description_file:
|
|
|
|
description_file.write(describe_form.description.data)
|
|
|
|
return
|