From d919e8f4dedf9287f22006c679f6400cff681b1c Mon Sep 17 00:00:00 2001 From: mkovalua Date: Mon, 6 Apr 2026 15:22:17 +0300 Subject: [PATCH 1/2] =?UTF-8?q?delete=20user=20social=20orcid=20using=20ma?= =?UTF-8?q?nagement=20command=20(=20need=20recatalog=5Fmetadata=20--users?= =?UTF-8?q?=C2=A0run=20to=20sync=20the=20data=20to=20SHARE=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/management/urls.py | 4 +- admin/management/views.py | 9 ++++ admin/templates/management/commands.html | 13 ++++++ .../commands/remove_orcid_from_user_social.py | 6 +++ .../0038_remove_orcid_from_social.py | 46 ------------------- 5 files changed, 31 insertions(+), 47 deletions(-) create mode 100644 osf/management/commands/remove_orcid_from_user_social.py delete mode 100644 osf/migrations/0038_remove_orcid_from_social.py diff --git a/admin/management/urls.py b/admin/management/urls.py index d583deb2ce0..c046b3bed18 100644 --- a/admin/management/urls.py +++ b/admin/management/urls.py @@ -19,5 +19,7 @@ re_path(r'^empty_metadata_dataarchive_registration_bulk_resync', views.EmptyMetadataDataarchiveRegistrationBulkResync.as_view(), name='empty-metadata-dataarchive-registration-bulk-resync'), re_path(r'^sync_notification_templates', views.SyncNotificationTemplates.as_view(), - name='sync_notification_templates') + name='sync_notification_templates'), + re_path(r'^remove_orcid_from_user_social', views.RemoveOrcidFromUserSocial.as_view(), + name='remove_orcid_from_user_social') ] diff --git a/admin/management/views.py b/admin/management/views.py index 36f3d893f24..f2052822f37 100644 --- a/admin/management/views.py +++ b/admin/management/views.py @@ -12,6 +12,7 @@ from osf.management.commands.fetch_cedar_metadata_templates import ingest_cedar_metadata_templates from osf.management.commands.sync_doi_metadata import sync_doi_metadata, sync_doi_empty_metadata_dataarchive_registrations from osf.management.commands.populate_notification_types import populate_notification_types +from osf.management.commands.remove_orcid_from_user_social import remove_orcid_from_user_social from scripts.find_spammy_content import manage_spammy_content from django.urls import reverse from django.shortcuts import redirect @@ -181,3 +182,11 @@ def post(self, request): populate_notification_types() messages.success(request, 'Notification templates have been successfully synced.') return redirect(reverse('management:commands')) + + +class RemoveOrcidFromUserSocial(ManagementCommandPermissionView): + + def post(self, request): + remove_orcid_from_user_social() + messages.success(request, 'Orcid from user social have been successfully removed.') + return redirect(reverse('management:commands')) diff --git a/admin/templates/management/commands.html b/admin/templates/management/commands.html index dd90affd5ff..edf242abfdd 100644 --- a/admin/templates/management/commands.html +++ b/admin/templates/management/commands.html @@ -165,6 +165,19 @@

Sync Notification Templates

+
+

Remove existing orcid info from user social

+

+ Use this management command to remove existing orcid info from user social. +

+
+ {% csrf_token %} + +
+
{% endblock %} diff --git a/osf/management/commands/remove_orcid_from_user_social.py b/osf/management/commands/remove_orcid_from_user_social.py new file mode 100644 index 00000000000..aa35eef67ed --- /dev/null +++ b/osf/management/commands/remove_orcid_from_user_social.py @@ -0,0 +1,6 @@ +from django.db.models.expressions import RawSQL +from osf.models import OSFUser + + +def remove_orcid_from_user_social(): + OSFUser.objects.filter(social__has_key='orcid').update(social=RawSQL("""social #- '{orcid}'""", [])) diff --git a/osf/migrations/0038_remove_orcid_from_social.py b/osf/migrations/0038_remove_orcid_from_social.py deleted file mode 100644 index 87b941cc898..00000000000 --- a/osf/migrations/0038_remove_orcid_from_social.py +++ /dev/null @@ -1,46 +0,0 @@ -from django.db import migrations, transaction - - -def remove_orcid_from_social(apps, schema_editor): - from osf.models import OSFUser - - user_ids = [] - batch = [] - CHUNK_SIZE = 1000 - - for user in OSFUser.objects.filter(social__has_key='orcid').iterator(chunk_size=CHUNK_SIZE): - user.social.pop('orcid', None) - batch.append(user) - user_ids.append(user.id) - if len(batch) >= 1000: - OSFUser.objects.bulk_update(batch, ['social']) - batch.clear() - - if batch: - OSFUser.objects.bulk_update(batch, ['social']) - - def reindex(): - for start in range(0, len(user_ids), CHUNK_SIZE): - chunked_ids = user_ids[start:start + CHUNK_SIZE] - for user in OSFUser.objects.filter(id__in=chunked_ids): - user.update_search() - - if user_ids: - # if update is successfully saved in database reindex share and elastic too - transaction.on_commit(reindex) - - -def reverse(apps, schema_editor): - """ - This is a no-op since we can't restore deleted records. - """ - - -class Migration(migrations.Migration): - dependencies = [ - ('osf', '0037_notification_refactor_post_release'), - ] - - operations = [ - migrations.RunPython(remove_orcid_from_social, reverse), - ] From 6b2b6751be8d751eee0f3a3993759c6799e807ae Mon Sep 17 00:00:00 2001 From: mkovalua Date: Mon, 6 Apr 2026 20:26:37 +0300 Subject: [PATCH 2/2] add terminal command run option with 'python3 manage.py remove_orcid_from_social' --- osf/management/commands/remove_orcid_from_user_social.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/osf/management/commands/remove_orcid_from_user_social.py b/osf/management/commands/remove_orcid_from_user_social.py index aa35eef67ed..2f1a943eb4c 100644 --- a/osf/management/commands/remove_orcid_from_user_social.py +++ b/osf/management/commands/remove_orcid_from_user_social.py @@ -1,6 +1,13 @@ +from django.core.management.base import BaseCommand from django.db.models.expressions import RawSQL from osf.models import OSFUser def remove_orcid_from_user_social(): OSFUser.objects.filter(social__has_key='orcid').update(social=RawSQL("""social #- '{orcid}'""", [])) + + +class Command(BaseCommand): + def handle(self, *args, **kwargs): + breakpoint() + remove_orcid_from_user_social()