python: Update tuple handling pattern; returned by a delete() query.

This commit updates the pattern for dealing with tuples
returned by the delete() query.

The '(num_deleted, ignored) = ModelName.objects.filter().delete()'
pattern is preferred due to better readability.

We avoid the pattern '(num_deleted, _)' because Django uses _
for translation, which may lead to future bugs.
This commit is contained in:
Prakhar Pratyush 2023-03-17 13:08:23 +05:30 committed by Tim Abbott
parent 83423bb45b
commit e45623fccc
2 changed files with 5 additions and 5 deletions

View File

@ -322,14 +322,14 @@ def delete_messages(msg_ids: List[int]) -> None:
def delete_expired_attachments(realm: Realm) -> None: def delete_expired_attachments(realm: Realm) -> None:
attachments_deleted, _ = Attachment.objects.filter( (num_deleted, ignored) = Attachment.objects.filter(
messages__isnull=True, messages__isnull=True,
realm_id=realm.id, realm_id=realm.id,
id__in=ArchivedAttachment.objects.filter(realm_id=realm.id), id__in=ArchivedAttachment.objects.filter(realm_id=realm.id),
).delete() ).delete()
if attachments_deleted > 0: if num_deleted > 0:
logger.info("Cleaned up %s attachments for realm %s", attachments_deleted, realm.string_id) logger.info("Cleaned up %s attachments for realm %s", num_deleted, realm.string_id)
def move_related_objects_to_archive(msg_ids: List[int]) -> None: def move_related_objects_to_archive(msg_ids: List[int]) -> None:

View File

@ -194,10 +194,10 @@ def unregister_remote_push_device(
validate_bouncer_token_request(token, token_kind) validate_bouncer_token_request(token, token_kind)
user_identity = UserPushIdentityCompat(user_id=user_id, user_uuid=user_uuid) user_identity = UserPushIdentityCompat(user_id=user_id, user_uuid=user_uuid)
deleted = RemotePushDeviceToken.objects.filter( (num_deleted, ignored) = RemotePushDeviceToken.objects.filter(
user_identity.filter_q(), token=token, kind=token_kind, server=server user_identity.filter_q(), token=token, kind=token_kind, server=server
).delete() ).delete()
if deleted[0] == 0: if num_deleted == 0:
raise JsonableError(err_("Token does not exist")) raise JsonableError(err_("Token does not exist"))
return json_success(request) return json_success(request)