From ed6504e022480481e589b8635434f7fa776d7b4e Mon Sep 17 00:00:00 2001
From: sks444 <krishnasingh.ss30@gmail.com>
Date: Mon, 9 Nov 2020 10:26:18 +0530
Subject: [PATCH] Fix migrations and the new screening flow after rebase

---
 hypha/apply/api/v1/screening/serializers.py   | 11 ++++++-
 ...y => 0081_add_screening_statuses_field.py} |  4 +--
 ...screening_status_to_screening_statuses.py} |  4 +--
 .../migrations/0083_auto_20201021_1129.py     | 23 -------------
 ... => 0083_remove_screening_status_field.py} |  4 +--
 .../applicationsubmission_admin_detail.html   |  8 +++--
 hypha/apply/funds/utils.py                    | 32 +++++++++++++++++++
 hypha/apply/funds/views.py                    | 12 ++-----
 8 files changed, 57 insertions(+), 41 deletions(-)
 rename hypha/apply/funds/migrations/{0080_add_screening_statuses_field.py => 0081_add_screening_statuses_field.py} (75%)
 rename hypha/apply/funds/migrations/{0081_migrate_screening_status_to_screening_statuses.py => 0082_migrate_screening_status_to_screening_statuses.py} (87%)
 delete mode 100644 hypha/apply/funds/migrations/0083_auto_20201021_1129.py
 rename hypha/apply/funds/migrations/{0082_remove_screening_status_field.py => 0083_remove_screening_status_field.py} (70%)

diff --git a/hypha/apply/api/v1/screening/serializers.py b/hypha/apply/api/v1/screening/serializers.py
index 03f78157b..0a5fed73d 100644
--- a/hypha/apply/api/v1/screening/serializers.py
+++ b/hypha/apply/api/v1/screening/serializers.py
@@ -1,4 +1,4 @@
-from rest_framework import serializers
+from rest_framework import exceptions, serializers
 
 from hypha.apply.funds.models import ScreeningStatus
 
@@ -12,6 +12,15 @@ class ScreeningStatusListSerializer(serializers.ModelSerializer):
 class ScreeningStatusSerializer(serializers.Serializer):
     title = serializers.CharField()
 
+    def validate_title(self, value):
+        try:
+            ScreeningStatus.objects.get(title=value)
+        except ScreeningStatus.DoesNotExist:
+            raise exceptions.ValidationError({
+                'detail': 'Title is not valid'
+            })
+        return value
+
 
 class ScreeningStatusDefaultSerializer(serializers.Serializer):
     yes = serializers.BooleanField()
diff --git a/hypha/apply/funds/migrations/0080_add_screening_statuses_field.py b/hypha/apply/funds/migrations/0081_add_screening_statuses_field.py
similarity index 75%
rename from hypha/apply/funds/migrations/0080_add_screening_statuses_field.py
rename to hypha/apply/funds/migrations/0081_add_screening_statuses_field.py
index dfe45884b..61bc366e8 100644
--- a/hypha/apply/funds/migrations/0080_add_screening_statuses_field.py
+++ b/hypha/apply/funds/migrations/0081_add_screening_statuses_field.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.2.16 on 2020-10-21 10:45
+# Generated by Django 2.2.16 on 2020-11-09 05:33
 
 from django.db import migrations, models
 
@@ -6,7 +6,7 @@ from django.db import migrations, models
 class Migration(migrations.Migration):
 
     dependencies = [
-        ('funds', '0079_add_reviewer_settings_for_submission_access'),
+        ('funds', '0080_add_yes_and_default_fields_to_screening_status'),
     ]
 
     operations = [
diff --git a/hypha/apply/funds/migrations/0081_migrate_screening_status_to_screening_statuses.py b/hypha/apply/funds/migrations/0082_migrate_screening_status_to_screening_statuses.py
similarity index 87%
rename from hypha/apply/funds/migrations/0081_migrate_screening_status_to_screening_statuses.py
rename to hypha/apply/funds/migrations/0082_migrate_screening_status_to_screening_statuses.py
index 4729ad737..fab5a3315 100644
--- a/hypha/apply/funds/migrations/0081_migrate_screening_status_to_screening_statuses.py
+++ b/hypha/apply/funds/migrations/0082_migrate_screening_status_to_screening_statuses.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.2.16 on 2020-10-21 10:46
+# Generated by Django 2.2.16 on 2020-11-09 05:35
 
 from django.db import migrations
 
@@ -18,7 +18,7 @@ def make_many_screening_statuses(apps, schema_editor):
 class Migration(migrations.Migration):
 
     dependencies = [
-        ('funds', '0080_add_screening_statuses_field'),
+        ('funds', '0081_add_screening_statuses_field'),
     ]
 
     operations = [
diff --git a/hypha/apply/funds/migrations/0083_auto_20201021_1129.py b/hypha/apply/funds/migrations/0083_auto_20201021_1129.py
deleted file mode 100644
index 2444167f0..000000000
--- a/hypha/apply/funds/migrations/0083_auto_20201021_1129.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 2.2.16 on 2020-10-21 11:29
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('funds', '0082_remove_screening_status_field'),
-    ]
-
-    operations = [
-        migrations.AddField(
-            model_name='screeningstatus',
-            name='default',
-            field=models.BooleanField(default=False, verbose_name='Default Yes/No'),
-        ),
-        migrations.AddField(
-            model_name='screeningstatus',
-            name='yes',
-            field=models.BooleanField(default=False, verbose_name='Yes/No'),
-        ),
-    ]
diff --git a/hypha/apply/funds/migrations/0082_remove_screening_status_field.py b/hypha/apply/funds/migrations/0083_remove_screening_status_field.py
similarity index 70%
rename from hypha/apply/funds/migrations/0082_remove_screening_status_field.py
rename to hypha/apply/funds/migrations/0083_remove_screening_status_field.py
index 80e7c7a7f..2ebe21d05 100644
--- a/hypha/apply/funds/migrations/0082_remove_screening_status_field.py
+++ b/hypha/apply/funds/migrations/0083_remove_screening_status_field.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.2.16 on 2020-10-21 10:50
+# Generated by Django 2.2.16 on 2020-11-09 05:41
 
 from django.db import migrations
 
@@ -6,7 +6,7 @@ from django.db import migrations
 class Migration(migrations.Migration):
 
     dependencies = [
-        ('funds', '0081_migrate_screening_status_to_screening_statuses'),
+        ('funds', '0082_migrate_screening_status_to_screening_statuses'),
     ]
 
     operations = [
diff --git a/hypha/apply/funds/templates/funds/applicationsubmission_admin_detail.html b/hypha/apply/funds/templates/funds/applicationsubmission_admin_detail.html
index 3f2d4b1c4..5d0f783e2 100644
--- a/hypha/apply/funds/templates/funds/applicationsubmission_admin_detail.html
+++ b/hypha/apply/funds/templates/funds/applicationsubmission_admin_detail.html
@@ -17,7 +17,9 @@
     <div class="js-actions-sidebar sidebar__inner sidebar__inner--light-blue sidebar__inner--actions {% if mobile %}sidebar__inner--mobile{% endif %}">
         {% include "funds/includes/admin_primary_actions.html" %}
     </div>
-    {% include "funds/includes/screening_form.html" with submission=object default_yes=default_yes_screening_status default_no=default_no_screening_status %}
+    {% if default_screening_statuses.0 != None and default_screening_statuses.1 != None %}
+        {% include "funds/includes/screening_form.html" with submission=object default_yes=default_screening_statuses.0 default_no=default_screening_statuses.1 %}
+    {% endif %}
     {% include "funds/includes/progress_form.html" %}
     {% include "funds/includes/update_lead_form.html" %}
     {% include "funds/includes/update_reviewer_form.html" %}
@@ -49,7 +51,9 @@
 {% endblock %}
 
 {% block screening_status %}
-    {% include 'funds/includes/screening_status_block.html' with default_yes=default_yes_screening_status default_no=default_no_screening_status %}
+    {% if default_screening_statuses.0 != None and default_screening_statuses.1 != None %}
+        {% include 'funds/includes/screening_status_block.html' with default_yes=default_screening_statuses.0 default_no=default_screening_statuses.1 %}
+    {% endif %}
 {% endblock %}
 
 {% block meta_terms %}
diff --git a/hypha/apply/funds/utils.py b/hypha/apply/funds/utils.py
index b62410d9b..452d2a243 100644
--- a/hypha/apply/funds/utils.py
+++ b/hypha/apply/funds/utils.py
@@ -1,8 +1,40 @@
 from hypha.apply.utils.image import generate_image_tag
 
+from .models.screening import ScreeningStatus
+
 
 def render_icon(image):
     if not image:
         return ''
     filter_spec = 'fill-20x20'
     return generate_image_tag(image, filter_spec)
+
+
+def get_default_screening_statues():
+    """
+    Get the default screening statuses set.
+
+    If the default for yes and no doesn't exit. First yes and
+    first no screening statuses created should be set as default
+    """
+    yes_screening_statuses = ScreeningStatus.objects.filter(yes=True)
+    no_screening_statuses = ScreeningStatus.objects.filter(yes=False)
+    default_yes = None
+    default_no = None
+    if yes_screening_statuses.exists():
+        try:
+            default_yes = yes_screening_statuses.get(default=True)
+        except ScreeningStatus.DoesNotExist:
+            # Set first yes screening status as default
+            default_yes = yes_screening_statuses.first()
+            default_yes.default = True
+            default_yes.save()
+    if no_screening_statuses.exists():
+        try:
+            default_no = no_screening_statuses.get(default=True)
+        except ScreeningStatus.DoesNotExist:
+            # Set first no screening status as default
+            default_no = no_screening_statuses.first()
+            default_no.default = True
+            default_no.save()
+    return [default_yes, default_no]
diff --git a/hypha/apply/funds/views.py b/hypha/apply/funds/views.py
index 9abf1a94e..9d67b75fd 100644
--- a/hypha/apply/funds/views.py
+++ b/hypha/apply/funds/views.py
@@ -82,7 +82,6 @@ from .models import (
     ReviewerSettings,
     RoundBase,
     RoundsAndLabs,
-    ScreeningStatus,
 )
 from .permissions import is_user_has_access_to_view_submission
 from .tables import (
@@ -98,6 +97,7 @@ from .tables import (
     SubmissionReviewerFilterAndSearch,
     SummarySubmissionsTable,
 )
+from .utils import get_default_screening_statues
 from .workflow import (
     DRAFT_STATE,
     INITIAL_STATE,
@@ -754,17 +754,11 @@ class AdminSubmissionDetailView(ReviewContextMixin, ActivityContextMixin, Delega
             other_submissions = other_submissions.exclude(id=self.object.next.id)
 
         public_page = self.object.get_from_parent('detail')()
-        default_yes_screening_status = ScreeningStatus.objects.get(
-            default=True, yes=True
-        )
-        default_no_screening_status = ScreeningStatus.objects.get(
-            default=True, yes=False
-        )
+        default_screening_statuses = get_default_screening_statues()
         return super().get_context_data(
             other_submissions=other_submissions,
             public_page=public_page,
-            default_yes_screening_status=default_yes_screening_status,
-            default_no_screening_status=default_no_screening_status,
+            default_screening_statuses=default_screening_statuses,
             **kwargs,
         )
 
-- 
GitLab