From 7728d057c56a26ed325768a2429467a228a9d347 Mon Sep 17 00:00:00 2001 From: nutrina Date: Tue, 13 Jun 2023 16:41:03 +0300 Subject: [PATCH] feat(api): score calculations should also be run if requires_calculation is None, as this will be the default value (#280) --- api/registry/tasks.py | 12 ++++++++---- api/registry/test/test_score_passport.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/api/registry/tasks.py b/api/registry/tasks.py index c65daeb77..039bdc591 100644 --- a/api/registry/tasks.py +++ b/api/registry/tasks.py @@ -63,7 +63,7 @@ def score_passport(community_id: int, address: str): passport = load_passport_record(community_id, address) if not passport: log.info( - "Passport no passport found for address='%s', community_id='%s' that has requires_calculation=True", + "Passport no passport found for address='%s', community_id='%s' that has requires_calculation=True or None", address, community_id, ) @@ -126,9 +126,13 @@ def load_passport_record(community_id: int, address: str) -> Passport | None: # A Passport instance should exist, and have the requires_calculation flag set to True if it requires calculation. # We check for this by running an update and checking for the number of updated rows # This update should also avoid race conditions as stated in the documentation: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#update - num_passports_updated = Passport.objects.filter( - address=address.lower(), community_id=community_id, requires_calculation=True - ).update(requires_calculation=False) + # We query for all passports that have requires_calculation not set to False + # because we want to calculate the score for any passport that has requires_calculation set to True or None + num_passports_updated = ( + Passport.objects.filter(address=address.lower(), community_id=community_id) + .exclude(requires_calculation=False) + .update(requires_calculation=False) + ) # If the num_passports_updated == 1, this means we are in the lucky task that has managed to pick this passport up for processing # Other tasks which are potentially racing for the same calculation should get num_passports_updated == 0 diff --git a/api/registry/test/test_score_passport.py b/api/registry/test/test_score_passport.py index 15e0c7b98..d7ed8adf6 100644 --- a/api/registry/test/test_score_passport.py +++ b/api/registry/test/test_score_passport.py @@ -201,7 +201,7 @@ def test_deduplication_of_scoring_tasks(self): score_passport_passport(self.community.pk, self.account.address) expected_call = call( - "Passport no passport found for address='%s', community_id='%s' that has requires_calculation=True", + "Passport no passport found for address='%s', community_id='%s' that has requires_calculation=True or None", self.account.address, self.community.pk, )