From d75c6d3a4e00359d3aad16efe7c56305e19fd652 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sun, 14 May 2023 22:14:12 +0200
Subject: [PATCH] Add ProductionEvent editing capabilities in new production
 page

---
 .../colleges/_hx_nomination_form.html         |  1 -
 scipost_django/production/forms.py            |  8 ++-
 ..._hx_productionstream_details_contents.html | 30 +++++-----
 .../_hx_productionstream_event_form.html      | 17 ++++++
 .../production/_productionstream_events.html  | 19 ++++++-
 scipost_django/production/urls.py             | 30 +++++++++-
 scipost_django/production/views.py            | 57 +++++++++++++------
 7 files changed, 122 insertions(+), 40 deletions(-)
 create mode 100644 scipost_django/production/templates/production/_hx_productionstream_event_form.html

diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_form.html b/scipost_django/colleges/templates/colleges/_hx_nomination_form.html
index 876a069a9..5543d1846 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_form.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_form.html
@@ -6,7 +6,6 @@
       hx-target="#nomination_form_response"
       hx-indicator="#nomination_form_response-indicator"
   >
-    {% csrf_token %}
     {% crispy nomination_form %}
   </form>
 </div>
diff --git a/scipost_django/production/forms.py b/scipost_django/production/forms.py
index d454ff127..d839a2245 100644
--- a/scipost_django/production/forms.py
+++ b/scipost_django/production/forms.py
@@ -32,7 +32,11 @@ today = datetime.datetime.today()
 class ProductionEventForm(forms.ModelForm):
     class Meta:
         model = ProductionEvent
-        fields = ("comments",)
+        fields = (
+            "stream",
+            "comments",
+            "noted_by",
+        )
         widgets = {
             "comments": TextareaWithPreview(attrs={"rows": 4}),
         }
@@ -41,7 +45,9 @@ class ProductionEventForm(forms.ModelForm):
         super().__init__(*args, **kwargs)
         self.helper = FormHelper()
         self.helper.layout = Layout(
+            Field("stream", type="hidden"),
             Field("comments"),
+            Field("noted_by", type="hidden"),
             Submit("submit", "Submit"),
         )
 
diff --git a/scipost_django/production/templates/production/_hx_productionstream_details_contents.html b/scipost_django/production/templates/production/_hx_productionstream_details_contents.html
index 5e1506dc2..29b10ba51 100644
--- a/scipost_django/production/templates/production/_hx_productionstream_details_contents.html
+++ b/scipost_django/production/templates/production/_hx_productionstream_details_contents.html
@@ -1,18 +1,18 @@
-{% load crispy_forms_tags %}
+
+<p>
+  <strong class="text-warning">While the new production page is being built</strong>: go the the <a href="{{ productionstream.get_absolute_url }}" target="_blank">(old) stream detail page</a> to manage this stream.
+</p>
+
 
 <h3>Events</h3>
-{% include 'production/_productionstream_events.html' with events=productionstream.events.all_without_duration %}
+{% include 'production/_productionstream_events.html' with productionstream=productionstream events=productionstream.events.all_without_duration %}
+
 
-<details class="border border-2 mt-4">
-  <summary class="bg-light p-2">Add a comment to this stream</summary>
-  <div class="p-2">
-    <form
-	hx-post="{% url 'production:_hx_events_add' productionstream_id=productionstream.id %}"
-	hx-target="#productionstream-{{ productionstream.id }}-details-contents"
-    >
-      <div id="productionevent-form">
-	{% crispy productionevent_form %}
-      </div>
-    </form>
-  </div>
-</details>
+<button
+    hx-get="{% url 'production:_hx_event_form' productionstream_id=productionstream.id %}"
+    hx-target="#productionstream-{{ productionstream.id }}-event-form"
+    hx-trigger="click"
+>
+  Add a comment to this stream
+</button>
+<div id="productionstream-{{ productionstream.id }}-event-form"></div>
diff --git a/scipost_django/production/templates/production/_hx_productionstream_event_form.html b/scipost_django/production/templates/production/_hx_productionstream_event_form.html
new file mode 100644
index 000000000..58bf102e0
--- /dev/null
+++ b/scipost_django/production/templates/production/_hx_productionstream_event_form.html
@@ -0,0 +1,17 @@
+{% load crispy_forms_tags %}
+
+{% if productionevent_form.instance and productionevent_form.instance.id %}
+  <form
+      hx-post="{% url 'production:_hx_event_form' productionstream_id=productionstream.id  event_id=productionevent_form.instance.id %}"
+      hx-target="#productionstream-{{ productionstream.id }}-details-contents"
+  >
+    {% crispy productionevent_form %}
+  </form>
+{% else %}
+  <form
+      hx-post="{% url 'production:_hx_event_form' productionstream_id=productionstream.id %}"
+      hx-target="#productionstream-{{ productionstream.id }}-details-contents"
+  >
+    {% crispy productionevent_form %}
+  </form>
+{% endif %}
diff --git a/scipost_django/production/templates/production/_productionstream_events.html b/scipost_django/production/templates/production/_productionstream_events.html
index 06ac2746c..4976228fc 100644
--- a/scipost_django/production/templates/production/_productionstream_events.html
+++ b/scipost_django/production/templates/production/_productionstream_events.html
@@ -30,8 +30,22 @@
           {% if not non_editable %}
             {% if event.noted_by == request.user.production_user and event.editable %}
               <div class="ps-2">
-		<a href="{% url 'production:update_event' event.id %}"><span aria-hidden="true">{% include 'bi/pencil-square.html' %}</span></a>
-		<a class="text-danger" href="{% url 'production:delete_event' event.id %}"><span aria-hidden="true">{% include 'bi/trash-fill.html' %}</span></a>
+		<a hx-get="{% url 'production:_hx_event_form' productionstream_id=productionstream.id event_id=event.id %}"
+		   hx-target="#productionstream-{{ productionstream.id }}-event-{{ event.id }}-form"
+		>
+		  <span aria-hidden="true">
+		    {% include 'bi/pencil-square.html' %}
+		  </span>
+		</a>
+		<a class="text-danger"
+		   hx-get="{% url 'production:_hx_event_delete' productionstream_id=productionstream.id event_id=event.id %}"
+		   hx-target="#productionstream-{{ productionstream.id }}-details-contents"
+		   hx-confirm="Delete this Event?"
+		>
+		  <span aria-hidden="true">
+		    {% include 'bi/trash-fill.html' %}
+		  </span>
+		</a>
               </div>
             {% endif %}
           {% endif %}
@@ -56,6 +70,7 @@
               {% endfor %}
             </ul>
 	  {% endif %}
+	  <div id="productionstream-{{ productionstream.id }}-event-{{ event.id }}-form"></div>
 	</td>
       </tr>
     {% empty %}
diff --git a/scipost_django/production/urls.py b/scipost_django/production/urls.py
index 1f3b1302e..dd60eae85 100644
--- a/scipost_django/production/urls.py
+++ b/scipost_django/production/urls.py
@@ -30,9 +30,33 @@ urlpatterns = [
                     name="_hx_productionstream_details_contents",
                 ),
                 path(
-                    "events/add",
-                    production_views._hx_events_add,
-                    name="_hx_events_add",
+                    "events/",
+                    include(
+                        [
+                            path(
+                                "form",
+                                production_views._hx_event_form,
+                                name="_hx_event_form",
+                            ),
+                            path(
+                                "<int:event_id>/",
+                                include(
+                                    [
+                                        path(
+                                            "update",
+                                            production_views._hx_event_form,
+                                            name="_hx_event_form",
+                                        ),
+                                        path(
+                                            "delete",
+                                            production_views._hx_event_delete,
+                                            name="_hx_event_delete",
+                                        ),
+                                    ]
+                                ),
+                            ),
+                        ]
+                    ),
                 ),
             ]
         ),
diff --git a/scipost_django/production/views.py b/scipost_django/production/views.py
index a2d4adede..f546f01b6 100644
--- a/scipost_django/production/views.py
+++ b/scipost_django/production/views.py
@@ -79,11 +79,9 @@ def _hx_productionstream_list(request):
 @is_production_user()
 @permission_required("scipost.can_view_production", raise_exception=True)
 def _hx_productionstream_details_contents(request, productionstream_id):
-    stream = get_object_or_404(ProductionStream, pk=productionstream_id)
-    productionevent_form = ProductionEventForm()
+    productionstream = get_object_or_404(ProductionStream, pk=productionstream_id)
     context = {
-        "productionstream": stream,
-        "productionevent_form": productionevent_form,
+        "productionstream": productionstream,
     }
     return render(
         request,
@@ -94,21 +92,44 @@ def _hx_productionstream_details_contents(request, productionstream_id):
 
 @is_production_user()
 @permission_required("scipost.can_view_production", raise_exception=True)
-def _hx_events_add(request, productionstream_id):
-    qs = ProductionStream.objects.ongoing()
-    if not request.user.has_perm("scipost.can_assign_production_officer"):
-        qs = qs.filter_for_user(request.user.production_user)
-
-    productionstream = get_object_or_404(qs, pk=productionstream_id)
-    prodevent_form = ProductionEventForm(request.POST or None)
-    if prodevent_form.is_valid():
-        prodevent = prodevent_form.save(commit=False)
-        prodevent.stream = productionstream
-        prodevent.noted_by = request.user.production_user
-        prodevent.save()
-        messages.success(request, "Comment added to Stream.")
+def _hx_event_form(request, productionstream_id, event_id=None):
+    """
+    Create or update a ProductionEvent.
+    """
+    productionstream = get_object_or_404(ProductionStream, pk=productionstream_id)
+    if event_id:
+        productionevent = get_object_or_404(ProductionEvent, pk=event_id)
     else:
-        messages.warning(request, "The form was invalidly filled.")
+        productionevent = None
+    if request.method == "POST":
+        form = ProductionEventForm(request.POST, instance=productionevent)
+        if form.is_valid():
+            form.save()
+            return redirect(reverse(
+                "production:_hx_productionstream_details_contents",
+                kwargs={"productionstream_id": productionstream.id,},
+            ))
+    elif productionevent:
+        form = ProductionEventForm(instance=productionevent)
+    else:
+        form = ProductionEventForm(
+            initial={
+                "stream": productionstream,
+                "noted_by": request.user.production_user,
+            },
+        )
+    context = {
+        "productionstream": productionstream,
+        "productionevent_form": form,
+    }
+    return render(request, "production/_hx_productionstream_event_form.html", context)
+
+
+@is_production_user()
+@permission_required("scipost.can_view_production", raise_exception=True)
+def _hx_event_delete(request, productionstream_id, event_id):
+    productionstream = get_object_or_404(ProductionStream, pk=productionstream_id)
+    ProductionEvent.objects.filter(pk=event_id).delete()
     return redirect(reverse(
         "production:_hx_productionstream_details_contents",
         kwargs={"productionstream_id": productionstream.id,},
-- 
GitLab