broke currently
This commit is contained in:
@ -1,29 +1,35 @@
|
||||
# plugins/submission/routes.py
|
||||
|
||||
# Standard library
|
||||
from datetime import datetime
|
||||
|
||||
# Third‐party
|
||||
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 plant‐related
|
||||
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)
|
||||
|
@ -1,4 +1,6 @@
|
||||
{# plugins/submission/templates/submission/view.html #}
|
||||
{% extends 'core_ui/base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container mt-4">
|
||||
<h2>Submission Details</h2>
|
||||
@ -73,13 +75,14 @@
|
||||
<div class="row">
|
||||
{% if images %}
|
||||
{% for img in images %}
|
||||
{# img.file_url == "YYYY/MM/DD/<uuid>.ext" #}
|
||||
<div class="col-md-3 mb-3">
|
||||
<div class="card shadow-sm">
|
||||
<a href="{{ url_for('media.media_file', filename=img.file_url) }}" target="_blank">
|
||||
<img src="{{ url_for('media.media_file', filename=img.file_url) }}"
|
||||
class="card-img-top"
|
||||
alt="Submission Image">
|
||||
<a href="{{ generate_image_url(img) }}" target="_blank">
|
||||
<img
|
||||
src="{{ generate_image_url(img) }}"
|
||||
class="card-img-top"
|
||||
alt="Submission Image"
|
||||
>
|
||||
</a>
|
||||
<div class="card-body p-2">
|
||||
<p class="card-text text-center">
|
||||
|
Reference in New Issue
Block a user