broke currently

This commit is contained in:
2025-06-22 16:11:29 -05:00
parent e7a0f5b1be
commit 2bb7a29141
77 changed files with 1748 additions and 2298 deletions

View File

@ -1,29 +1,35 @@
# plugins/submission/routes.py
# Standard library
from datetime import datetime
# Thirdparty
from flask import (
Blueprint,
render_template,
request,
redirect,
url_for,
flash,
jsonify
Blueprint, render_template, request,
redirect, url_for, flash, jsonify
)
from flask_login import login_required, current_user
# Application
from app import db
# Plugins
from plugins.media.routes import _process_upload_file
# Local
from .models import Submission, SubmissionImage
from .forms import SubmissionForm
from datetime import datetime
import os
from werkzeug.utils import secure_filename
from plugins.media.utils import generate_random_filename, strip_metadata_and_save
bp = Blueprint("submission", __name__, template_folder="templates", url_prefix="/submission")
# We store only "YYYY/MM/DD/<uuid>.ext" in SubmissionImage.file_url.
# All files live under "/app/static/uploads/YYYY/MM/DD/<uuid>.ext" in the container.
BASE_UPLOAD_FOLDER = "static/uploads"
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "gif"}
bp = Blueprint(
"submission",
__name__,
template_folder="templates",
url_prefix="/submission"
)
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg", "gif", "webp"}
def allowed_file(filename):
return (
@ -31,11 +37,13 @@ def allowed_file(filename):
and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
)
@bp.route("/", methods=["GET"])
@login_required
def submission_index():
return redirect(url_for("submission.new_submission"))
@bp.route("/new", methods=["GET", "POST"])
@bp.route("/new/", methods=["GET", "POST"])
@login_required
@ -45,7 +53,6 @@ def new_submission():
plant_types = {"market_price", "name_correction", "new_plant", "mutation"}
t = form.submission_type.data
# Only require plant_name if the type is plantrelated
if t in plant_types and not form.plant_name.data.strip():
flash("Common Name is required for this submission type.", "danger")
return render_template("submission/new.html", form=form)
@ -69,34 +76,20 @@ def new_submission():
db.session.add(submission)
db.session.flush()
# date subfolder: "YYYY/MM/DD"
today = datetime.utcnow().strftime("%Y/%m/%d")
# Write into "/app/static/uploads/YYYY/MM/DD", not "/app/app/static/uploads..."
save_dir = os.path.join(os.getcwd(), BASE_UPLOAD_FOLDER, today)
os.makedirs(save_dir, exist_ok=True)
# Handle any uploaded images
files = request.files.getlist("images")
for f in files:
if f and allowed_file(f.filename):
orig_name = secure_filename(f.filename)
rand_name = generate_random_filename(orig_name)
# Temporarily save under "/app/temp_<uuid>.ext"
temp_path = os.path.join(os.getcwd(), "temp_" + rand_name)
f.save(temp_path)
final_path = os.path.join(save_dir, rand_name)
strip_metadata_and_save(temp_path, final_path)
os.remove(temp_path)
# Store only "YYYY/MM/DD/<uuid>.ext"
rel_url = f"{today}/{rand_name}"
media = _process_upload_file(
file = f,
uploader_id = current_user.id,
plugin = "submission",
related_id = submission.id
)
img = SubmissionImage(
submission_id=submission.id,
file_url=rel_url,
uploaded_at=datetime.utcnow()
submission_id = submission.id,
file_url = media.filename,
uploaded_at = media.uploaded_at
)
db.session.add(img)
@ -106,6 +99,7 @@ def new_submission():
return render_template("submission/new.html", form=form)
@bp.route("/list", methods=["GET"])
@bp.route("/list/", methods=["GET"])
@login_required
@ -132,6 +126,7 @@ def list_submissions():
all_types=all_types
)
@bp.route("/view/<int:submission_id>", methods=["GET"])
@bp.route("/view/<int:submission_id>/", methods=["GET"])
@login_required
@ -140,5 +135,6 @@ def view_submission(submission_id):
if sub.user_id != current_user.id and current_user.role != "admin":
flash("Not authorized to view this submission.", "danger")
return redirect(url_for("submission.list_submissions"))
images = SubmissionImage.query.filter_by(submission_id=sub.id).all()
return render_template("submission/view.html", submission=sub, images=images)