You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2022/07/08 19:19:42 UTC

[airavata-django-portal] 01/05: AIRAVATA-3567 Add UserProfile.is_ext_user_profile_valid

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch AIRAVATA-3562
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit bd823913ab74ee62947662ff4eb8c820e5042b37
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Tue Jun 14 14:01:50 2022 -0400

    AIRAVATA-3567 Add UserProfile.is_ext_user_profile_valid
---
 django_airavata/apps/auth/models.py            |  18 +++-
 django_airavata/apps/auth/tests/test_models.py | 143 +++++++++++++++++++++++++
 2 files changed, 160 insertions(+), 1 deletion(-)

diff --git a/django_airavata/apps/auth/models.py b/django_airavata/apps/auth/models.py
index 3bae9e25..d4cd2695 100644
--- a/django_airavata/apps/auth/models.py
+++ b/django_airavata/apps/auth/models.py
@@ -109,6 +109,19 @@ class UserProfile(models.Model):
             result.append('last_name')
         return result
 
+    @property
+    def is_ext_user_profile_valid(self):
+        fields = ExtendedUserProfileField.objects.filter(deleted=False)
+        for field in fields:
+            try:
+                value = self.extended_profile_values.filter(ext_user_profile_field=field).get()
+                if not value.valid:
+                    return False
+            except ExtendedUserProfileValue.DoesNotExist:
+                if field.required:
+                    return False
+        return True
+
     def is_non_empty(self, value: str):
         return value is not None and value.strip() != ""
 
@@ -248,7 +261,7 @@ class ExtendedUserProfileFieldLink(models.Model):
 
 class ExtendedUserProfileValue(models.Model):
     ext_user_profile_field = models.ForeignKey(ExtendedUserProfileField, on_delete=models.SET_NULL, null=True)
-    user_profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name="extended_profile")
+    user_profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name="extended_profile_values")
     created_date = models.DateTimeField(auto_now_add=True)
     updated_date = models.DateTimeField(auto_now=True)
 
@@ -300,6 +313,9 @@ class ExtendedUserProfileValue(models.Model):
 
     @property
     def valid(self):
+        # if the field is deleted, whatever the value, consider it valid
+        if self.ext_user_profile_field.deleted:
+            return True
         if self.ext_user_profile_field.required:
             if self.value_type == 'text':
                 return self.text.text_value and len(self.text.text_value.strip()) > 0
diff --git a/django_airavata/apps/auth/tests/test_models.py b/django_airavata/apps/auth/tests/test_models.py
index 73b18331..677fc8fc 100644
--- a/django_airavata/apps/auth/tests/test_models.py
+++ b/django_airavata/apps/auth/tests/test_models.py
@@ -134,6 +134,15 @@ class ExtendedUserProfileValueTestCase(TestCase):
             text_value="")
         self.assertFalse(value.valid)
 
+    def test_valid_of_text_empty_deleted(self):
+        """Invalid value but field is deleted so valid should be true."""
+        field = models.ExtendedUserProfileTextField.objects.create(
+            name="test", order=1, required=True, deleted=True)
+        value = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field, user_profile=self.user_profile,
+            text_value="")
+        self.assertTrue(value.valid, "Although value is empty but required, since the field is deleted, consider it not invalid")
+
     def test_valid_of_text_empty_no_required(self):
         field = models.ExtendedUserProfileTextField.objects.create(
             name="test", order=1, required=False)
@@ -238,3 +247,137 @@ class ExtendedUserProfileValueTestCase(TestCase):
             ext_user_profile_field=field, user_profile=self.user_profile,
             other_value="Some write-in value.")
         self.assertTrue(value.valid)
+
+
+class UserProfileTestCase(TestCase):
+
+    def setUp(self) -> None:
+        User = get_user_model()
+        user = User.objects.create_user("testuser")
+        self.user_profile: models.UserProfile = models.UserProfile.objects.create(user=user)
+
+    def test_is_ext_user_profile_valid_no_fields(self):
+        self.assertTrue(self.user_profile.is_ext_user_profile_valid)
+
+    def test_is_ext_user_profile_valid_some_fields_some_values_valid(self):
+        """Values for all fields, but only some are valid"""
+        field1 = models.ExtendedUserProfileTextField.objects.create(
+            name="test1", order=1, required=True)
+        field2 = models.ExtendedUserProfileTextField.objects.create(
+            name="test2", order=2, required=True)
+        field3 = models.ExtendedUserProfileTextField.objects.create(
+            name="test3", order=3, required=True)
+        value1 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field1, user_profile=self.user_profile,
+            text_value="Answer #1"
+        )
+        value2 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field2, user_profile=self.user_profile,
+            text_value="Answer #2"
+        )
+        value3 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field3, user_profile=self.user_profile,
+            text_value=""  # intentionally blank
+        )
+        self.assertTrue(value1.valid)
+        self.assertTrue(value2.valid)
+        self.assertFalse(value3.valid)
+        self.assertFalse(self.user_profile.is_ext_user_profile_valid)
+
+    def test_is_ext_user_profile_valid_some_fields_all_values_valid(self):
+        """Values for all fields, and all values are valid."""
+        field1 = models.ExtendedUserProfileTextField.objects.create(
+            name="test1", order=1, required=True)
+        field2 = models.ExtendedUserProfileTextField.objects.create(
+            name="test2", order=2, required=True)
+        field3 = models.ExtendedUserProfileTextField.objects.create(
+            name="test3", order=3, required=True)
+        value1 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field1, user_profile=self.user_profile,
+            text_value="Answer #1"
+        )
+        value2 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field2, user_profile=self.user_profile,
+            text_value="Answer #2"
+        )
+        value3 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field3, user_profile=self.user_profile,
+            text_value="Answer #3"
+        )
+        self.assertTrue(value1.valid)
+        self.assertTrue(value2.valid)
+        self.assertTrue(value3.valid)
+        self.assertTrue(self.user_profile.is_ext_user_profile_valid)
+
+    def test_is_ext_user_profile_valid_some_fields_some_not_required_values_missing(self):
+        """Some values are missing but they are optional."""
+        field1 = models.ExtendedUserProfileTextField.objects.create(
+            name="test1", order=1, required=True)
+        field2 = models.ExtendedUserProfileTextField.objects.create(
+            name="test2", order=2, required=False)
+        field3 = models.ExtendedUserProfileTextField.objects.create(
+            name="test3", order=3, required=False)
+        value1 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field1, user_profile=self.user_profile,
+            text_value="Answer #1"
+        )
+        self.assertTrue(value1.valid)
+        self.assertFalse(models.ExtendedUserProfileValue.objects.filter(
+            user_profile=self.user_profile, ext_user_profile_field=field2).exists(),
+            "No value for field2")
+        self.assertFalse(models.ExtendedUserProfileValue.objects.filter(
+            user_profile=self.user_profile, ext_user_profile_field=field3).exists(),
+            "No value for field3")
+        self.assertTrue(self.user_profile.is_ext_user_profile_valid)
+
+    def test_is_ext_user_profile_valid_some_fields_some_required_values_missing(self):
+        """Some required values are missing."""
+        field1 = models.ExtendedUserProfileTextField.objects.create(
+            name="test1", order=1, required=True)
+        field2 = models.ExtendedUserProfileTextField.objects.create(
+            name="test2", order=2, required=True)
+        field3 = models.ExtendedUserProfileTextField.objects.create(
+            name="test3", order=3, required=False)
+        value1 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field1, user_profile=self.user_profile,
+            text_value="Answer #1"
+        )
+        self.assertTrue(value1.valid)
+        self.assertFalse(models.ExtendedUserProfileValue.objects.filter(
+            user_profile=self.user_profile, ext_user_profile_field=field2).exists(),
+            "No value for field2, but field2 is required")
+        self.assertFalse(models.ExtendedUserProfileValue.objects.filter(
+            user_profile=self.user_profile, ext_user_profile_field=field3).exists(),
+            "No value for field3")
+        self.assertFalse(self.user_profile.is_ext_user_profile_valid)
+
+    def test_is_ext_user_profile_valid_some_fields_invalid_value_but_field_deleted(self):
+        """Value is invalid but field is deleted so it shouldn't count."""
+        field1 = models.ExtendedUserProfileTextField.objects.create(
+            name="test1", order=1, required=True)
+        field2 = models.ExtendedUserProfileTextField.objects.create(
+            name="test2", order=2, required=True)
+        field3 = models.ExtendedUserProfileTextField.objects.create(
+            name="test3", order=3, required=True)
+
+        value1 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field1, user_profile=self.user_profile,
+            text_value="Answer #1"
+        )
+        value2 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field2, user_profile=self.user_profile,
+            text_value="Answer #2"
+        )
+        value3 = models.ExtendedUserProfileTextValue.objects.create(
+            ext_user_profile_field=field3, user_profile=self.user_profile,
+            text_value=""
+        )
+        self.assertTrue(value1.valid)
+        self.assertTrue(value2.valid)
+        self.assertFalse(value3.valid)
+        self.assertFalse(self.user_profile.is_ext_user_profile_valid)
+
+        field3.deleted = True
+        field3.save()
+        self.assertTrue(value3.valid)
+        self.assertTrue(self.user_profile.is_ext_user_profile_valid)