From 73bf117c392fa4cf3d2fc55b47ec9d70d40b8fa4 Mon Sep 17 00:00:00 2001 From: mfraezz Date: Fri, 11 Aug 2023 13:58:53 -0400 Subject: [PATCH] [ENG-4618] User Search metadata (#10428) * Add property to user * Add allow_indexing field --- api/users/serializers.py | 1 + api_tests/users/views/test_user_detail.py | 37 +++++++++++++++++++ osf/migrations/0015_osfuser_allow_indexing.py | 18 +++++++++ osf/models/user.py | 7 ++++ 4 files changed, 63 insertions(+) create mode 100644 osf/migrations/0015_osfuser_allow_indexing.py diff --git a/api/users/serializers.py b/api/users/serializers.py index 1abce9569fe8..88578a326e9f 100644 --- a/api/users/serializers.py +++ b/api/users/serializers.py @@ -87,6 +87,7 @@ class UserSerializer(JSONAPISerializer): social = SocialField(required=False, min_version='2.10') employment = JSONAPIListField(required=False, source='jobs') education = JSONAPIListField(required=False, source='schools') + allow_indexing = ShowIfCurrentUser(ser.BooleanField(required=False, allow_null=True)) can_view_reviews = ShowIfCurrentUser(ser.SerializerMethodField(help_text='Whether the current user has the `view_submissions` permission to ANY reviews provider.')) accepted_terms_of_service = ShowIfCurrentUser(ser.SerializerMethodField()) diff --git a/api_tests/users/views/test_user_detail.py b/api_tests/users/views/test_user_detail.py index 56c57d9175ba..e83eb3734cea 100644 --- a/api_tests/users/views/test_user_detail.py +++ b/api_tests/users/views/test_user_detail.py @@ -1126,6 +1126,43 @@ def test_update_accepted_tos_false( assert res.status_code == 200 assert user_one.accepted_terms_of_service is None + def test_update_allow_indexing_sets_field( + self, app, user_one, url_user_one): + assert user_one.allow_indexing is None + res = app.patch_json_api( + url_user_one, + { + 'data': { + 'id': user_one._id, + 'type': 'users', + 'attributes': { + 'allow_indexing': True, + } + } + }, + auth=user_one.auth + ) + user_one.reload() + assert res.status_code == 200 + assert user_one.allow_indexing is True + res = app.patch_json_api( + url_user_one, + { + 'data': { + 'id': user_one._id, + 'type': 'users', + 'attributes': { + 'allow_indexing': False, + } + } + }, + auth=user_one.auth + ) + user_one.reload() + assert res.status_code == 200 + assert user_one.allow_indexing is False + + @pytest.mark.django_db class TestDeactivatedUser: diff --git a/osf/migrations/0015_osfuser_allow_indexing.py b/osf/migrations/0015_osfuser_allow_indexing.py new file mode 100644 index 000000000000..651774d81db9 --- /dev/null +++ b/osf/migrations/0015_osfuser_allow_indexing.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.17 on 2023-08-11 14:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('osf', '0014_institution_storage_regions'), + ] + + operations = [ + migrations.AddField( + model_name='osfuser', + name='allow_indexing', + field=models.BooleanField(blank=True, default=None, null=True), + ), + ] diff --git a/osf/models/user.py b/osf/models/user.py index 271f7dff23b3..8c24a0c15e1a 100644 --- a/osf/models/user.py +++ b/osf/models/user.py @@ -143,6 +143,7 @@ class OSFUser(DirtyFieldsMixin, GuidMixin, BaseModel, AbstractBaseUser, Permissi 'jobs', 'schools', 'social', + 'allow_indexing', } # Overrides DirtyFieldsMixin, Foreign Keys checked by '_id' rather than typical name. @@ -398,6 +399,8 @@ class OSFUser(DirtyFieldsMixin, GuidMixin, BaseModel, AbstractBaseUser, Permissi chronos_user_id = models.TextField(null=True, blank=True, db_index=True) + allow_indexing = models.BooleanField(null=True, blank=True, default=None) + objects = OSFUserManager() is_active = models.BooleanField(default=False) @@ -454,6 +457,10 @@ def is_merged(self): """ return self.merged_by is not None + @property + def is_public(self): + return self.is_active and self.allow_indexing is not False + @property def unconfirmed_emails(self): # Handle when email_verifications field is None