From ccc339747db7bb1ac7dfb7f22e227f2d786e7f2a Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Thu, 30 Nov 2023 09:32:12 +0330 Subject: [PATCH 1/2] Add reversed constraints to RaffleSerializer --- prizetap/serializers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/prizetap/serializers.py b/prizetap/serializers.py index f7d502b8..9b350928 100644 --- a/prizetap/serializers.py +++ b/prizetap/serializers.py @@ -178,6 +178,7 @@ class Meta: "raffleId", "constraints", "constraint_params", + "reversed_constraints", "created_at", "start_at", "deadline", From 40649ac2864767652d5dc37fac3c825e86db1abf Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Thu, 30 Nov 2023 09:33:26 +0330 Subject: [PATCH 2/2] Refactor get raffle constraints depending on reversed constraints --- prizetap/tests.py | 21 +++++++++++++++++++++ prizetap/views.py | 9 +++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/prizetap/tests.py b/prizetap/tests.py index a410a11e..1790291d 100644 --- a/prizetap/tests.py +++ b/prizetap/tests.py @@ -435,6 +435,27 @@ def test_get_raffle_constraints_when_is_verified(self): self.assertEqual(data["pk"], self.meet_constraint.pk) self.assertEqual(data["is_verified"], True) + @patch( + "authentication.helpers.BrightIDSoulboundAPIInterface.get_verification_status", + lambda a, b, c: (True, None), + ) + def test_get_raffle_constraints_when_constraint_is_reversed(self): + self.client.force_authenticate(user=self.user_profile.user) + self.raffle.reversed_constraints = str(self.meet_constraint.pk) + self.raffle.save() + response = self.client.get(reverse("get-raffle-constraints", kwargs={"raffle_pk": self.raffle.pk})) + data = response.data["constraints"][0] + self.assertEqual(response.status_code, 200) + self.assertEqual(data["pk"], self.meet_constraint.pk) + self.assertEqual(data["is_verified"], False) + + BrightIDSoulboundAPIInterface.get_verification_status = MagicMock(return_value=(False, None)) + response = self.client.get(reverse("get-raffle-constraints", kwargs={"raffle_pk": self.raffle.pk})) + data = response.data["constraints"][0] + self.assertEqual(response.status_code, 200) + self.assertEqual(data["pk"], self.meet_constraint.pk) + self.assertEqual(data["is_verified"], True) + class RaffleEntryTestCase(RaffleTestCase): def setUp(self): diff --git a/prizetap/views.py b/prizetap/views.py index 4fb5a92e..80c5f368 100644 --- a/prizetap/views.py +++ b/prizetap/views.py @@ -145,6 +145,7 @@ def get(self, request, raffle_pk): except Exception: param_values = {} + reversed_constraints = raffle.reversed_constraints.split(",") if raffle.reversed_constraints else [] response_constraints = [] for c in raffle.constraints.all(): @@ -155,8 +156,12 @@ def get(self, request, raffle_pk): except KeyError: pass is_verified = False - if constraint.is_observed(): - is_verified = True + if str(c.pk) in reversed_constraints: + if not constraint.is_observed(): + is_verified = True + else: + if constraint.is_observed(): + is_verified = True response_constraints.append({**ConstraintSerializer(c).data, "is_verified": is_verified}) return Response({"success": True, "constraints": response_constraints}, status=200)