Skip to content

Commit

Permalink
fix(projectOwnership): set new owner's usage storage correctly (#5308)
Browse files Browse the repository at this point in the history
### 📣 Summary
Fixed an issue where the usage storage for the new owner was not updated
correctly during project ownership transfers.
  • Loading branch information
noliveleger authored Nov 27, 2024
1 parent 0f1275b commit 8f19a41
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 2 additions & 4 deletions kobo/apps/openrosa/apps/logger/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ def pre_delete_attachment(instance, **kwargs):

if file_size and attachment.deleted_at is None:
with transaction.atomic():
"""
Update both counters at the same time (in a transaction) to avoid
desynchronization as much as possible
"""
# Update both counters simultaneously within a transaction to minimize
# the risk of desynchronization.
UserProfile.objects.filter(
user_id=xform.user_id
).update(
Expand Down
7 changes: 6 additions & 1 deletion kobo/apps/openrosa/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,6 @@ def get_soft_deleted_attachments(instance: Instance) -> list[Attachment]:

# Update Attachment objects to hide them if they are not used anymore.
# We do not want to delete them until the instance itself is deleted.

# FIXME Temporary hack to leave background-audio files and audit files alone
# Bug comes from `get_xform_media_question_xpaths()`
queryset = Attachment.objects.filter(instance=instance).exclude(
Expand All @@ -744,6 +743,12 @@ def get_soft_deleted_attachments(instance: Instance) -> list[Attachment]:
| Q(media_file_basename__regex=r'^\d{10,}\.(m4a|amr)$')
)
soft_deleted_attachments = list(queryset.all())

# The query below updates only the database records, not the in-memory
# `Attachment` objects.
# As a result, the `deleted_at` attribute of `Attachment` objects remains `None`
# in memory after the update.
# This behavior is necessary to allow the signal to handle file deletion from storage.
queryset.update(deleted_at=dj_timezone.now())

return soft_deleted_attachments
Expand Down
2 changes: 1 addition & 1 deletion kpi/deployment_backends/kobocat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ def transfer_counters_ownership(self, new_owner: 'kobo_auth.User'):
attachment_storage_bytes=F('attachment_storage_bytes')
- self.xform.attachment_storage_bytes
)
KobocatUserProfile.objects.filter(user_id=self.asset.owner.pk).update(
KobocatUserProfile.objects.filter(user_id=new_owner.pk).update(
attachment_storage_bytes=F('attachment_storage_bytes')
+ self.xform.attachment_storage_bytes
)
Expand Down

0 comments on commit 8f19a41

Please sign in to comment.