From 2c42f17cd903acda0ced1c5400d968e86aa21a7f Mon Sep 17 00:00:00 2001 From: Noelle Leigh Date: Mon, 1 Nov 2021 18:02:32 -0400 Subject: [PATCH] Preserve job note upon resubmission When the user clicks on "Edit & resubmit", the note associated with the previous build will prepopulate the "Add Note" field with "(resubmitted)" as a suffix. --- buildsrht/blueprints/jobs.py | 27 ++++++++++++++++++++++----- buildsrht/templates/submit.html | 3 ++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/buildsrht/blueprints/jobs.py b/buildsrht/blueprints/jobs.py index 22580df..9ec3af1 100644 --- a/buildsrht/blueprints/jobs.py +++ b/buildsrht/blueprints/jobs.py @@ -20,6 +20,7 @@ import hashlib import requests import yaml import json +import textwrap jobs = Blueprint("jobs", __name__) @@ -164,22 +165,37 @@ def index(): Job.query.filter(Job.owner_id == current_user.id), "index.html", rss_feed=rss_feed) + @jobs.route("/submit") @loginrequired def submit_GET(): - manifest = session.get("manifest") - if manifest: - del session["manifest"] - else: - manifest = request.args.get("manifest") + manifest = session.pop("manifest") + note = session.pop("note") status = 200 payment_required = requires_payment(current_user) if payment_required: status = 402 return render_template("submit.html", manifest=manifest, + note=note, payment_required=payment_required), status +def addsuffix(note: str, suffix: str) -> str: + """ + Given a note and a suffix, return the note with the suffix concatenated/ + + The returned string is guaranteed to fit in the Job.note DB field. + """ + maxlen = Job.note.prop.columns[0].type.length + assert len(suffix) + 1 <= maxlen, f"Suffix was too long ({len(suffix)})" + if note.endswith(suffix): + return note + result = f"{note} {suffix}" + if len(result) <= maxlen: + return result + note = textwrap.shorten(note, maxlen - len(suffix) - 1, placeholder="…") + return f"{note} {suffix}" + @jobs.route("/resubmit/") @loginrequired def resubmit_GET(job_id): @@ -187,6 +203,7 @@ def resubmit_GET(job_id): if not job: abort(404) session["manifest"] = job.manifest + session["note"] = addsuffix(job.note, "(resubmitted)") return redirect("/submit") @jobs.route("/submit", methods=["POST"]) diff --git a/buildsrht/templates/submit.html b/buildsrht/templates/submit.html index 2edc807..2e6f642 100644 --- a/buildsrht/templates/submit.html +++ b/buildsrht/templates/submit.html @@ -67,7 +67,8 @@ class="form-control" id="note" name="note" - placeholder="Submitted on the web" /> + placeholder="Submitted on the web" + value="{{note if note else ""}}" />