Skip to content

Commit

Permalink
fix: better handling of unchanged records in cli; fix for validation …
Browse files Browse the repository at this point in the history
…error when existing profile data doesn't match schema
  • Loading branch information
monotasker committed Aug 27, 2024
1 parent b26724f commit 99c6bff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
21 changes: 17 additions & 4 deletions invenio_remote_user_data/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def update_user_data(
)
counter = 0
successes = []
unchanged = []
failures = []
not_found_remote = []
not_found_local = []
Expand All @@ -111,7 +112,7 @@ def update_user_data(
for j in range(int(start), int(end) + 1):
expanded_ids.append(j)
else:
expanded_ids.append(int(i))
expanded_ids.append(i)
ids = expanded_ids

# eliminate duplicates
Expand All @@ -132,12 +133,12 @@ def update_user_data(
).one_or_none()
else:
user_ident = UserIdentity.query.filter_by(
id_user=i, method=source
id_user=int(i), method=source
).one_or_none()
if not user_ident:
print(f"No remote registration found for {i}")
not_found_local.append(i)
break
continue

update_result = user_data_service.update_user_from_remote(
system_identity, user_ident.id_user, source, user_ident.id
Expand All @@ -154,7 +155,7 @@ def update_user_data(
if not user_ident:
print(f"No remote registration found for {u.id}")
not_found_local.append(i)
break
continue

try:
update_result = user_data_service.update_user_from_remote(
Expand All @@ -171,6 +172,12 @@ def update_user_data(
elif update_result[1].get("error", "") == "invalid_response":
print(f"Invalid response updating {u.id}")
invalid_responses.append(u.id)
elif (
len(update_result[1].keys()) == 0
and len(update_result[2]) == 0
) and "error" not in update_result[1].keys():
print(f"No new data on remote server for {u.id}")
unchanged.append(u.id)
elif update_result:
print(f"Updated user {u.id}")
pprint(update_result)
Expand All @@ -179,6 +186,12 @@ def update_user_data(
print(f"Failed to update {u.id}")
failures.append(u.id)
print(f"All done updating {counter} {'users' if not groups else 'groups'}")
if len(successes):
print(f"Successfully updated {len(successes)} records.")
if len(unchanged):
print(
f"No updates necessary for {len(unchanged)} records: {unchanged}"
)
if len(not_found_local):
print(
f"No remote registration found in Invenio for "
Expand Down
12 changes: 10 additions & 2 deletions invenio_remote_user_data/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,21 @@ def compare_remote_with_local(
group memberships.
"""
initial_user_data = {
"user_profile": user.user_profile,
"username": user.username,
"preferences": user.preferences,
"roles": user.roles,
"email": user.email,
"active": user.active,
}
try:
initial_user_data["user_profile"] = user.user_profile
except ValueError:
self.logger.error(
f"Error fetching initial user profile data for user {user.id}. "
f"Some data in db was invalid. Starting fresh with incoming "
"data."
)

new_data: dict = {"active": True}

local_groups = [r.name for r in user.roles]
Expand Down Expand Up @@ -630,7 +638,7 @@ def compare_remote_with_local(
for r in local_groups
if r not in group_changes["dropped_groups"]
]
new_data["user_profile"] = user.user_profile
new_data["user_profile"] = initial_user_data["user_profile"]
self.logger.debug(f"users data: {pformat(users)}")
new_data["user_profile"].update(
{
Expand Down

0 comments on commit 99c6bff

Please sign in to comment.