From fd93f76214bc5743317e0bf81944ffb81aa3b93a Mon Sep 17 00:00:00 2001
From: sks444 <krishnasingh.ss30@gmail.com>
Date: Mon, 2 Nov 2020 11:56:36 +0530
Subject: [PATCH] Add yes and default field to screening status

---
 hypha/apply/funds/admin.py                    |  1 +
 hypha/apply/funds/admin_forms.py              | 20 +++++++++++++++-
 ..._and_default_fields_to_screening_status.py | 23 +++++++++++++++++++
 hypha/apply/funds/models/screening.py         | 12 ++++++++++
 4 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 hypha/apply/funds/migrations/0080_add_yes_and_default_fields_to_screening_status.py

diff --git a/hypha/apply/funds/admin.py b/hypha/apply/funds/admin.py
index 5a48fa379..f12075832 100644
--- a/hypha/apply/funds/admin.py
+++ b/hypha/apply/funds/admin.py
@@ -113,6 +113,7 @@ class ScreeningStatusPermissionHelper(PermissionHelper):
 class ScreeningStatusAdmin(ModelAdmin):
     model = ScreeningStatus
     menu_icon = 'tag'
+    list_display = ('title', 'yes', 'default')
     permission_helper_class = ScreeningStatusPermissionHelper
 
 
diff --git a/hypha/apply/funds/admin_forms.py b/hypha/apply/funds/admin_forms.py
index 1cca6f9d2..73c6da525 100644
--- a/hypha/apply/funds/admin_forms.py
+++ b/hypha/apply/funds/admin_forms.py
@@ -1,6 +1,7 @@
 from collections import Counter
 
-from wagtail.admin.forms import WagtailAdminPageForm
+from django.apps import apps
+from wagtail.admin.forms import WagtailAdminModelForm, WagtailAdminPageForm
 
 from .workflow import WORKFLOWS
 
@@ -82,3 +83,20 @@ class RoundBasePageAdminForm(WagtailAdminPageForm):
             self.add_error('start_date', 'Please select start date.')
 
         return cleaned_data
+
+
+class ScreeningStatusAdminForm(WagtailAdminModelForm):
+
+    def clean(self):
+        cleaned_data = super().clean()
+        default = cleaned_data['default']
+        yes = cleaned_data['yes']
+        ScreeningStatus = apps.get_model('funds', 'ScreeningStatus')
+        default_screening = ScreeningStatus.objects.filter(
+            default=True, yes=yes
+        ).first()
+        if default:
+            # Can't set two defaults for yes/no.
+            default_screening.default = False
+            default_screening.save()
+        return cleaned_data
diff --git a/hypha/apply/funds/migrations/0080_add_yes_and_default_fields_to_screening_status.py b/hypha/apply/funds/migrations/0080_add_yes_and_default_fields_to_screening_status.py
new file mode 100644
index 000000000..c44ee4cc1
--- /dev/null
+++ b/hypha/apply/funds/migrations/0080_add_yes_and_default_fields_to_screening_status.py
@@ -0,0 +1,23 @@
+# Generated by Django 2.2.16 on 2020-11-02 06:21
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('funds', '0079_add_reviewer_settings_for_submission_access'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='screeningstatus',
+            name='default',
+            field=models.BooleanField(default=False, help_text='Only one Yes and No screening status can be set as default.', verbose_name='Default Yes/No'),
+        ),
+        migrations.AddField(
+            model_name='screeningstatus',
+            name='yes',
+            field=models.BooleanField(default=False, help_text='Tick mark for Yes otherwise No.', verbose_name='Yes/No'),
+        ),
+    ]
diff --git a/hypha/apply/funds/models/screening.py b/hypha/apply/funds/models/screening.py
index c590704b1..3c0084f55 100644
--- a/hypha/apply/funds/models/screening.py
+++ b/hypha/apply/funds/models/screening.py
@@ -1,8 +1,20 @@
 from django.db import models
 
+from ..admin_forms import ScreeningStatusAdminForm
+
 
 class ScreeningStatus(models.Model):
     title = models.CharField(max_length=128)
+    yes = models.BooleanField(
+        default=False, verbose_name="Yes/No",
+        help_text='Tick mark for Yes otherwise No.'
+    )
+    default = models.BooleanField(
+        default=False, verbose_name="Default Yes/No",
+        help_text='Only one Yes and No screening status can be set as default.'
+    )
+
+    base_form_class = ScreeningStatusAdminForm
 
     class Meta:
         verbose_name_plural = "screening statuses"
-- 
GitLab