From 7ed9d365d9dbbaf0103cf8baa5a1970a5725f41f Mon Sep 17 00:00:00 2001
From: Sandeep Chauhan <sandeepsajan0@gmail.com>
Date: Wed, 17 Apr 2024 22:35:51 +0530
Subject: [PATCH] Add draft determinations to todo list (#3846)

Fixes #3828
---
 .../templates/dashboard/dashboard.html        |  7 +++-
 hypha/apply/determinations/views.py           | 21 ++++++++++
 .../todo/migrations/0002_alter_task_code.py   | 41 +++++++++++++++++++
 hypha/apply/todo/options.py                   | 10 +++++
 hypha/apply/todo/views.py                     | 15 +++++++
 hypha/templates/includes/sprites.html         |  4 ++
 6 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 hypha/apply/todo/migrations/0002_alter_task_code.py

diff --git a/hypha/apply/dashboard/templates/dashboard/dashboard.html b/hypha/apply/dashboard/templates/dashboard/dashboard.html
index 5f713ea80..39534f753 100644
--- a/hypha/apply/dashboard/templates/dashboard/dashboard.html
+++ b/hypha/apply/dashboard/templates/dashboard/dashboard.html
@@ -30,7 +30,12 @@
                     {% for task in my_tasks.data %}
                         <div class="bg-white p-1 flex mb-1 items-center border border-gray-600">
                             <svg class="icon icon--dashboard-tasks"><use xlink:href="#{{ task.icon }}"></use></svg>
-                            <div class="flex-1">{{ task.text|markdown|nh3 }}</div>
+                            <div class="flex flex-1 items-center">
+                                {{ task.text|markdown|nh3 }}
+                                {% if task.type == "Draft" %}
+                                    <span class="bg-red-200 hover:opacity-70 transition-opacity rounded-full whitespace-nowrap inline-block ms-1 my-2 px-2 pt-0.5 pb-1 text-sm font-medium text-gray-800">{{ task.type }}</span>
+                                {% endif %}
+                            </div>
                             <a class="button button-primary m-2" href="{{ task.url }}">{% trans "View" %}</a>
                         </div>
                     {% endfor %}
diff --git a/hypha/apply/determinations/views.py b/hypha/apply/determinations/views.py
index e7bed495a..078222974 100644
--- a/hypha/apply/determinations/views.py
+++ b/hypha/apply/determinations/views.py
@@ -21,6 +21,12 @@ from hypha.apply.funds.models import ApplicationSubmission
 from hypha.apply.funds.workflow import DETERMINATION_OUTCOMES, Concept
 from hypha.apply.projects.models import Project
 from hypha.apply.stream_forms.models import BaseStreamForm
+from hypha.apply.todo.options import DETERMINATION_DRAFT
+from hypha.apply.todo.views import (
+    add_task_to_user,
+    remove_tasks_for_user,
+    remove_tasks_of_related_obj_for_specific_code,
+)
 from hypha.apply.users.decorators import staff_required
 from hypha.apply.utils.views import CreateOrUpdateView, ViewDispatcher
 
@@ -425,6 +431,11 @@ class DeterminationCreateOrUpdateView(BaseStreamForm, CreateOrUpdateView):
 
         super().form_valid(form)
         if self.object.is_draft:
+            add_task_to_user(
+                code=DETERMINATION_DRAFT,
+                user=self.object.author,
+                related_obj=self.object,
+            )
             return HttpResponseRedirect(self.submission.get_absolute_url())
 
         with transaction.atomic():
@@ -460,6 +471,16 @@ class DeterminationCreateOrUpdateView(BaseStreamForm, CreateOrUpdateView):
             if self.submission.accepted_for_funding and settings.PROJECTS_AUTO_CREATE:
                 Project.create_from_submission(self.submission)
 
+        # remove current users's task for determination
+        remove_tasks_for_user(
+            code=DETERMINATION_DRAFT, user=self.object.author, related_obj=self.object
+        )
+
+        # remove all current determination tasks for the submission
+        remove_tasks_of_related_obj_for_specific_code(
+            code=DETERMINATION_DRAFT, related_obj=self.object
+        )
+
         messenger(
             MESSAGES.DETERMINATION_OUTCOME,
             request=self.request,
diff --git a/hypha/apply/todo/migrations/0002_alter_task_code.py b/hypha/apply/todo/migrations/0002_alter_task_code.py
new file mode 100644
index 000000000..c70d422fb
--- /dev/null
+++ b/hypha/apply/todo/migrations/0002_alter_task_code.py
@@ -0,0 +1,41 @@
+# Generated by Django 4.2.11 on 2024-04-03 12:39
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("todo", "0001_initial"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="task",
+            name="code",
+            field=models.CharField(
+                choices=[
+                    ("determination_draft", "Determination draft"),
+                    ("project_waiting_paf", "Project waiting PAF"),
+                    ("project_submit_paf", "Project submit PAF"),
+                    ("paf_required_changes", "PAF required changes"),
+                    ("paf_waiting_assignee", "PAF waiting assignee"),
+                    ("paf_waiting_approval", "PAF waiting approval"),
+                    ("project_waiting_contract", "Project waiting contract"),
+                    (
+                        "project_waiting_contract_document",
+                        "Project waiting contract document",
+                    ),
+                    (
+                        "project_waiting_contract_review",
+                        "Project waiting contract review",
+                    ),
+                    ("project_waiting_invoice", "Project waiting invoice"),
+                    ("invoice_required_changes", "Invoice required changes"),
+                    ("invoice_waiting_approval", "Invoice waiting approval"),
+                    ("invoice_waiting_paid", "Invoice waiting paid"),
+                    ("report_due", "Report due"),
+                ],
+                max_length=50,
+            ),
+        ),
+    ]
diff --git a/hypha/apply/todo/options.py b/hypha/apply/todo/options.py
index 6d8b6a325..d97d5b077 100644
--- a/hypha/apply/todo/options.py
+++ b/hypha/apply/todo/options.py
@@ -4,6 +4,7 @@ from django.utils.translation import gettext as _
 
 from hypha.apply.activity.adapters.utils import link_to
 
+DETERMINATION_DRAFT = "determination_draft"
 PROJECT_WAITING_PAF = "project_waiting_paf"
 PROJECT_SUBMIT_PAF = "project_submit_paf"
 PAF_REQUIRED_CHANGES = "paf_required_changes"
@@ -19,6 +20,7 @@ INVOICE_WAITING_PAID = "invoice_waiting_paid"
 REPORT_DUE = "report_due"
 
 TASKS_CODE_CHOICES = (
+    (DETERMINATION_DRAFT, "Determination draft"),
     (PROJECT_WAITING_PAF, "Project waiting PAF"),
     (PROJECT_SUBMIT_PAF, "Project submit PAF"),
     (PAF_REQUIRED_CHANGES, "PAF required changes"),
@@ -38,6 +40,14 @@ TASKS_CODE_CHOICES = (
 template_map = {
     # SUBMISSIONS ACTIONS
     # :todo: actions for mupltiple stages of submission
+    DETERMINATION_DRAFT: {
+        "text": _(
+            'Determination draft for submission [<span class=" truncate inline-block w-32 align-bottom ">{related.submission.title}</span>]({link} "{related.submission.title}") is waiting to be submitted'
+        ),
+        "icon": "edit-draft",
+        "url": "{link}",
+        "type": _("Draft"),
+    },
     # PROJECT actions
     # draft state (staff action)
     PROJECT_WAITING_PAF: {
diff --git a/hypha/apply/todo/views.py b/hypha/apply/todo/views.py
index 4ec9f2d00..1ed151c24 100644
--- a/hypha/apply/todo/views.py
+++ b/hypha/apply/todo/views.py
@@ -91,6 +91,21 @@ def remove_tasks_for_user_group(code, user_group, related_obj):
     return None
 
 
+def remove_tasks_of_related_obj_for_specific_code(code, related_obj):
+    """
+    Remove all tasks of a related object with the provide code irrespective to their users
+    input:
+        code: TASKS_CODE_CHOICES.keys()
+        related_obj: Object - Submission, Project, Invoice, Report
+    """
+    Task.objects.filter(
+        code=code,
+        related_content_type=ContentType.objects.get_for_model(related_obj).id,
+        related_object_id=related_obj.id,
+    ).delete()
+    return None
+
+
 def remove_tasks_of_related_obj(related_obj):
     """
     Remove all tasks of a related object irrespective of their code and users
diff --git a/hypha/templates/includes/sprites.html b/hypha/templates/includes/sprites.html
index ee94938f6..ba718828e 100644
--- a/hypha/templates/includes/sprites.html
+++ b/hypha/templates/includes/sprites.html
@@ -33,4 +33,8 @@
         <path d="M7.54625 9.5375L7.54625 4.11249L8.47594 4.11249L8.47594 9.5375L7.54625 9.5375ZM8.64 11.4937C8.64 11.6354 8.58531 11.7542 8.47594 11.85C8.36135 11.95 8.2051 12 8.00719 12C7.80927 12 7.65302 11.95 7.53844 11.85C7.41865 11.7542 7.35875 11.6354 7.35875 11.4937C7.35875 11.3562 7.41865 11.2375 7.53844 11.1375C7.65302 11.0417 7.80927 10.9937 8.00719 10.9937C8.2051 10.9937 8.36135 11.0417 8.47594 11.1375C8.58531 11.2375 8.64 11.3562 8.64 11.4937Z" fill="#F05E54"/>
     </symbol>
 
+    <svg id="edit-draft" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
+        <path d="M12 20h9"/>
+        <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"/>
+    </svg>
 </svg>
-- 
GitLab