From 98512df06bccdf921a9149afa564256f8b9a1dd3 Mon Sep 17 00:00:00 2001 From: Jonas Carson Date: Thu, 25 Jan 2024 11:48:08 -0800 Subject: [PATCH] Make sure we apply no updates unless we are in update mode --- primed/users/audit.py | 48 ++++++++++--------- .../management/commands/sync-drupal-data.py | 18 ++++++- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/primed/users/audit.py b/primed/users/audit.py index 141b69d0..201c9c50 100644 --- a/primed/users/audit.py +++ b/primed/users/audit.py @@ -208,6 +208,7 @@ def audit_drupal_users(study_sites, json_api, apply_changes=False): # potential blocked user, but will no longer have a drupal uid # so we cover these below continue + sa = None try: sa = SocialAccount.objects.get( uid=user.attributes["drupal_internal__uid"], @@ -218,30 +219,33 @@ def audit_drupal_users(study_sites, json_api, apply_changes=False): drupal_user.username = drupal_username drupal_user.name = drupal_full_name drupal_user.email = drupal_email - drupal_user.save() + if apply_changes is True: + drupal_user.save() is_new_user = True - sa = SocialAccount.objects.create( - user=drupal_user, - uid=user.attributes["drupal_internal__uid"], - provider=CustomProvider.id, - ) + if apply_changes is True: + sa = SocialAccount.objects.create( + user=drupal_user, + uid=user.attributes["drupal_internal__uid"], + provider=CustomProvider.id, + ) audit_results.add_new(data=user) - - user_changed = drupal_adapter.update_user_info( - user=sa.user, - extra_data={ - "preferred_username": drupal_username, - "first_name": drupal_firstname, - "last_name": drupal_lastname, - "email": drupal_email, - }, - apply_update=apply_changes, - ) - - user_sites_changed = drupal_adapter.update_user_study_sites( - user=sa.user, - extra_data={"study_site_or_center": drupal_user_study_site_shortnames}, - ) + if sa: + user_changed = drupal_adapter.update_user_info( + user=sa.user, + extra_data={ + "preferred_username": drupal_username, + "first_name": drupal_firstname, + "last_name": drupal_lastname, + "email": drupal_email, + }, + apply_update=apply_changes, + ) + if sa: + user_sites_changed = drupal_adapter.update_user_study_sites( + user=sa.user, + extra_data={"study_site_or_center": drupal_user_study_site_shortnames}, + apply_update=apply_changes + ) if user_changed or user_sites_changed and not is_new_user: audit_results.add_update(data=user) diff --git a/primed/users/management/commands/sync-drupal-data.py b/primed/users/management/commands/sync-drupal-data.py index b409775d..4af3a6b4 100644 --- a/primed/users/management/commands/sync-drupal-data.py +++ b/primed/users/management/commands/sync-drupal-data.py @@ -2,7 +2,11 @@ from django.core.management.base import BaseCommand -from primed.users.audit import drupal_data_study_site_audit, drupal_data_user_audit +from primed.users.audit import ( + AuditResults, + drupal_data_study_site_audit, + drupal_data_user_audit, +) logger = logging.getLogger(__name__) @@ -23,6 +27,18 @@ def handle(self, *args, **options): user_audit_results = drupal_data_user_audit(apply_changes=should_update) print(f"User Audit Results {user_audit_results}") + if user_audit_results.encountered_issues(): + print( + user_audit_results.rows_by_result_type( + result_type=AuditResults.RESULT_TYPE_ISSUE + ) + ) site_audit_results = drupal_data_study_site_audit(apply_changes=should_update) print(f"Site Audit Results {site_audit_results}") + if site_audit_results.encountered_issues(): + print( + site_audit_results.rows_by_result_type( + result_type=AuditResults.RESULT_TYPE_ISSUE + ) + )