From 77123835eaf69e2befecbbfe98d1070fc9521df1 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 14 Jun 2023 15:47:41 +0200
Subject: [PATCH] add django messages on stream property change

---
 ...tionstream_change_invitations_officer.html |   7 -
 .../_hx_productionstream_change_officer.html  |   7 -
 .../_hx_productionstream_change_status.html   |   7 -
 ...hx_productionstream_change_supervisor.html |   7 -
 scipost_django/production/views.py            | 127 +++++++-----------
 5 files changed, 45 insertions(+), 110 deletions(-)

diff --git a/scipost_django/production/templates/production/_hx_productionstream_change_invitations_officer.html b/scipost_django/production/templates/production/_hx_productionstream_change_invitations_officer.html
index c2ec64a40..7f6e4df72 100644
--- a/scipost_django/production/templates/production/_hx_productionstream_change_invitations_officer.html
+++ b/scipost_django/production/templates/production/_hx_productionstream_change_invitations_officer.html
@@ -16,10 +16,3 @@
         </form>
     </div>
 {% endif %}
-
-{% if message %}
-    <div id="productionstream-{{ stream.id }}-actions-message"
-         class="alert alert-{{ message.type }}"
-         role="alert"
-         hx-swap-oob="true">{{ message.text }}</div>
-{% endif %}
diff --git a/scipost_django/production/templates/production/_hx_productionstream_change_officer.html b/scipost_django/production/templates/production/_hx_productionstream_change_officer.html
index 869ab3255..8a1c7d8df 100644
--- a/scipost_django/production/templates/production/_hx_productionstream_change_officer.html
+++ b/scipost_django/production/templates/production/_hx_productionstream_change_officer.html
@@ -16,10 +16,3 @@
         </form>
     </div>
 {% endif %}
-
-{% if message %}
-    <div id="productionstream-{{ stream.id }}-actions-message"
-         class="alert alert-{{ message.type }}"
-         role="alert"
-         hx-swap-oob="true">{{ message.text }}</div>
-{% endif %}
diff --git a/scipost_django/production/templates/production/_hx_productionstream_change_status.html b/scipost_django/production/templates/production/_hx_productionstream_change_status.html
index c7bcde48f..59fd5c29d 100644
--- a/scipost_django/production/templates/production/_hx_productionstream_change_status.html
+++ b/scipost_django/production/templates/production/_hx_productionstream_change_status.html
@@ -18,10 +18,3 @@
         </div>
     </form>
 {% endif %}
- 
-{% if message %}
-    <div id="productionstream-{{ stream.id }}-actions-message"
-         class="alert alert-{{ message.type }}"
-         role="alert"
-         hx-swap-oob="true">{{ message.text }}</div>
-{% endif %}
diff --git a/scipost_django/production/templates/production/_hx_productionstream_change_supervisor.html b/scipost_django/production/templates/production/_hx_productionstream_change_supervisor.html
index e7c8743a7..aa86793c1 100644
--- a/scipost_django/production/templates/production/_hx_productionstream_change_supervisor.html
+++ b/scipost_django/production/templates/production/_hx_productionstream_change_supervisor.html
@@ -16,10 +16,3 @@
         </form>
     </div>
 {% endif %}
-
-{% if message %}
-    <div id="productionstream-{{ stream.id }}-actions-message"
-         class="alert alert-{{ message.type }}"
-         role="alert"
-         hx-swap-oob="true">{{ message.text }}</div>
-{% endif %}
diff --git a/scipost_django/production/views.py b/scipost_django/production/views.py
index 42db0e5c4..88ed8b8ad 100644
--- a/scipost_django/production/views.py
+++ b/scipost_django/production/views.py
@@ -428,9 +428,7 @@ def _hx_productionstream_actions_work_log(request, productionstream_id):
         log.content = productionstream
         log.user = request.user
         log.save()
-    else:
-        # messages.warning(request, "The form was invalidly filled.")
-        pass
+        messages.success(request, "Work Log added to Stream.")
 
     context = {
         "productionstream": productionstream,
@@ -469,25 +467,18 @@ def update_status(request, stream_id):
             auto_id=f"productionstream_{productionstream.id}_id_%s",
         )
 
-        if status_form.is_valid():
-            stream = status_form.save()
-            status_form.fields["status"].choices = status_form.get_available_statuses()
-            message = {
-                "type": "success",
-                "text": "Production Stream succesfully changed status.",
-            }
+    if status_form.is_valid():
+        status_form.save()
+        status_form.fields["status"].choices = status_form.get_available_statuses()
+        messages.success(request, "Production Stream succesfully changed status.")
 
-        else:
-            message = {
-                "type": "danger",
-                "text": "\\n".join(status_form.errors.values()),
-            }
+    else:
+        messages.error(request, "\\n".join(status_form.errors.values()))
 
-        context = {
-            "stream": productionstream,
-            "form": status_form,
-            "message": message,
-        }
+    context = {
+        "stream": productionstream,
+        "form": status_form,
+    }
 
     return render(
         request,
@@ -581,13 +572,10 @@ def update_officer(request, stream_id):
             officer_form.save()
             officer = officer_form.cleaned_data.get("officer")
 
-            # Add officer to stream if they exist.
-            if officer is not None:
-                assign_perm("can_work_for_stream", officer.user, productionstream)
-                message = {
-                    "type": "success",
-                    "text": f"Officer {officer} has been assigned.",
-                }
+        # Add officer to stream if they exist.
+        if officer is not None:
+            assign_perm("can_work_for_stream", officer.user, productionstream)
+            messages.success(request, f"Officer {officer} has been assigned.")
 
                 event = ProductionEvent(
                     stream=productionstream,
@@ -603,24 +591,18 @@ def update_officer(request, stream_id):
                 ProductionUtils.load({"request": request, "stream": productionstream})
                 ProductionUtils.email_assigned_production_officer()
 
-            # Remove old officer.
-            else:
-                remove_perm("can_work_for_stream", prev_officer.user, productionstream)
-                message = {
-                    "type": "success",
-                    "text": f"Officer {prev_officer} has been removed.",
-                }
+        # Remove old officer.
         else:
-            message = {
-                "type": "danger",
-                "text": "\\n".join(officer_form.errors.values()),
-            }
+            remove_perm("can_work_for_stream", prev_officer.user, productionstream)
+            messages.success(request, "Officer {prev_officer} has been removed.")
 
-        context = {
-            "stream": productionstream,
-            "form": officer_form,
-            "message": message,
-        }
+    else:
+        messages.error(request, "\\n".join(officer_form.errors.values()))
+
+    context = {
+        "stream": productionstream,
+        "form": officer_form,
+    }
 
     return render(
         request,
@@ -737,13 +719,12 @@ def update_invitations_officer(request, stream_id):
                 "invitations_officer"
             )
 
-            # Add invitations officer to stream if they exist.
-            if inv_officer is not None:
-                assign_perm("can_work_for_stream", inv_officer.user, productionstream)
-                message = {
-                    "type": "success",
-                    "text": f"Invitations Officer {inv_officer} has been assigned.",
-                }
+        # Add invitations officer to stream if they exist.
+        if inv_officer is not None:
+            assign_perm("can_work_for_stream", inv_officer.user, productionstream)
+            messages.success(
+                request, f"Invitations Officer {inv_officer} has been assigned."
+            )
 
                 event = ProductionEvent(
                     stream=productionstream,
@@ -759,27 +740,19 @@ def update_invitations_officer(request, stream_id):
                 ProductionUtils.load({"request": request, "stream": productionstream})
                 ProductionUtils.email_assigned_invitation_officer()
 
-            # Remove old invitations officer.
-            else:
-                remove_perm(
-                    "can_work_for_stream", prev_inv_officer.user, productionstream
-                )
-                message = {
-                    "type": "success",
-                    "text": f"Invitations Officer {prev_inv_officer} has been removed.",
-                }
-
+        # Remove old invitations officer.
         else:
-            message = {
-                "type": "danger",
-                "text": "\\n".join(invitations_officer_form.errors.values()),
-            }
+            remove_perm("can_work_for_stream", prev_inv_officer.user, productionstream)
+            messages.success(
+                request, f"Invitations Officer {prev_inv_officer} has been removed."
+            )
+    else:
+        messages.error(request, "\\n".join(invitations_officer_form.errors.values()))
 
-        context = {
-            "stream": productionstream,
-            "form": invitations_officer_form,
-            "message": message,
-        }
+    context = {
+        "stream": productionstream,
+        "form": invitations_officer_form,
+    }
 
     return render(
         request,
@@ -882,10 +855,7 @@ def update_supervisor(request, stream_id):
 
         # Add supervisor to stream if they exist.
         if supervisor is not None:
-            message = {
-                "type": "success",
-                "text": f"Supervisor {supervisor} has been assigned.",
-            }
+            messages.success(request, f"Supervisor {supervisor} has been assigned.")
 
             assign_perm("can_work_for_stream", supervisor.user, productionstream)
             assign_perm(
@@ -914,21 +884,14 @@ def update_supervisor(request, stream_id):
                 prev_supervisor.user,
                 productionstream,
             )
-            message = {
-                "type": "success",
-                "text": f"Supervisor {prev_supervisor} has been removed.",
-            }
+            messages.success(request, f"Supervisor {prev_supervisor} has been removed.")
 
     else:
-        message = {
-            "type": "danger",
-            "text": "\\n".join(supervisor_form.errors.values()),
-        }
+        messages.error(request, "\\n".join(supervisor_form.errors.values()))
 
     context = {
         "stream": productionstream,
         "form": supervisor_form,
-        "message": message,
     }
 
     return render(
-- 
GitLab