From 1e20d0b7bf9b0402244bc4283efca44eae740542 Mon Sep 17 00:00:00 2001 From: Sketch <75850871+SketchMaster2001@users.noreply.github.com> Date: Sun, 30 Jun 2024 00:50:51 -0400 Subject: [PATCH] feat: Add remove field button to credits management --- templates/credits_add.html | 10 ++++++++++ templates/credits_edit.html | 10 ++++++++++ theunderground/credits.py | 11 ++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/templates/credits_add.html b/templates/credits_add.html index 32f1af48..0068b1ec 100644 --- a/templates/credits_add.html +++ b/templates/credits_add.html @@ -11,6 +11,7 @@ {{ form.role_and_name_list }} +


{{ form.submit(class_="button is-success") }}

@@ -55,5 +56,14 @@ document.addEventListener("DOMContentLoaded", createStaffPair); document.getElementById("addNewField").addEventListener("click", createStaffPair); + document.getElementById("removeField").addEventListener("click", function(){ + // Remove last field pair + for (var i = 0; i < 7; i++) { + document.getElementById("role_and_name_list").removeChild(document.getElementById("role_and_name_list").lastChild); + } + + // Finally decrement the fieldNum + fieldNum--; + }) {% endblock %} diff --git a/templates/credits_edit.html b/templates/credits_edit.html index 30798d55..05709ba9 100644 --- a/templates/credits_edit.html +++ b/templates/credits_edit.html @@ -10,6 +10,7 @@ {{ form.hidden_tag() }} {{ form.role_and_name_list }} +


{{ form.submit(class_="button is-success") }}

@@ -91,5 +92,14 @@ {% endfor %} }); document.getElementById("addNewField").addEventListener("click", createStaffPair); + document.getElementById("removeField").addEventListener("click", function(){ + // Remove last field pair + for (var i = 0; i < 7; i++) { + document.getElementById("role_and_name_list").removeChild(document.getElementById("role_and_name_list").lastChild); + } + + // Finally decrement the fieldNum + fieldNum--; + }) {% endblock %} diff --git a/theunderground/credits.py b/theunderground/credits.py index d1348348..115b219c 100644 --- a/theunderground/credits.py +++ b/theunderground/credits.py @@ -52,8 +52,17 @@ def edit_credits(movie_id): queried_credits = [] if form.validate_on_submit(): + received_length = len(form.role_and_name_list.data) / 2 + original_length = MovieCredits.query.filter_by(movie_id=movie_id).count() + if received_length < original_length: + # If the edited credits is less than what is in the database, we must delete the removed. + MovieCredits.query.filter_by(movie_id=movie_id).where( + MovieCredits.order > received_length + ).delete() + + # We can handle any edits now. order = 1 - for i in range(0, len(form.role_and_name_list.data), 2): + for i in range(0, int(received_length) * 2, 2): # Query the row data = ( MovieCredits.query.filter_by(movie_id=movie_id)