Skip to content

Commit

Permalink
Merge pull request #247 from UnitapApp/fix/prizetap
Browse files Browse the repository at this point in the history
Include is_reversed in constraint serializer
  • Loading branch information
ShayanShiravani authored Jan 4, 2024
2 parents f60d4a1 + e3fb978 commit 3689b9b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
4 changes: 4 additions & 0 deletions prizetap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def winners(self):
def winner_entries(self):
return self.entries.filter(is_winner=True)

@property
def reversed_constraints_list(self):
return self.reversed_constraints.split(",") if self.reversed_constraints else []

def __str__(self):
return f"{self.name}"

Expand Down
6 changes: 1 addition & 5 deletions prizetap/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@ class Meta:
]

def get_constraints(self, raffle: Raffle):
reversed_constraints = (
raffle.reversed_constraints.split(",")
if raffle.reversed_constraints
else []
)
reversed_constraints = raffle.reversed_constraints_list
return [
{
**ConstraintSerializer(c).data,
Expand Down
20 changes: 14 additions & 6 deletions prizetap/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ def check_user_constraints(self):
param_values = json.loads(self.raffle.constraint_params)
except Exception:
param_values = {}
reversed_constraints = self.raffle.reversed_constraints.split(",") if self.raffle.reversed_constraints else []
for c in self.raffle.constraints.all():
constraint: ConstraintVerification = get_constraint(c.name)(self.user_profile)
constraint: ConstraintVerification = get_constraint(c.name)(
self.user_profile
)
constraint.response = c.response
try:
constraint.param_values = param_values[c.name]
except KeyError:
pass
if str(c.pk) in reversed_constraints:
if str(c.pk) in self.raffle.reversed_constraints_list:
if constraint.is_observed():
raise PermissionDenied(constraint.response)
else:
if not constraint.is_observed():
raise PermissionDenied(constraint.response)

def check_user_has_wallet(self):
if not self.user_profile.wallets.filter(wallet_type=self.raffle.chain.chain_type).exists():
raise PermissionDenied(f"You have not connected an {self.raffle.chain.chain_type} wallet to your account")
if not self.user_profile.wallets.filter(
wallet_type=self.raffle.chain.chain_type
).exists():
raise PermissionDenied(
f"You have not connected an {self.raffle.chain.chain_type} "
"wallet to your account"
)

def is_valid(self, data):
self.can_enroll_in_raffle()
Expand All @@ -56,7 +62,9 @@ def __init__(self, *args, **kwargs):

def is_owner_of_raffle_entry(self):
if not self.raffle_entry.user_profile == self.user_profile:
raise PermissionDenied("You don't have permission to update this raffle entry")
raise PermissionDenied(
"You don't have permission to update this raffle entry"
)

def is_tx_empty(self):
if self.raffle_entry.tx_hash:
Expand Down
12 changes: 6 additions & 6 deletions prizetap/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,7 @@ def get(self, request, raffle_pk):
except Exception:
param_values = {}

reversed_constraints = (
raffle.reversed_constraints.split(",")
if raffle.reversed_constraints
else []
)
reversed_constraints = raffle.reversed_constraints_list
response_constraints = []

for c in raffle.constraints.all():
Expand All @@ -179,7 +175,11 @@ def get(self, request, raffle_pk):
if constraint.is_observed():
is_verified = True
response_constraints.append(
{**ConstraintSerializer(c).data, "is_verified": is_verified}
{
**ConstraintSerializer(c).data,
"is_verified": is_verified,
"is_reversed": True if str(c.pk) in reversed_constraints else False,
}
)

return Response(
Expand Down

0 comments on commit 3689b9b

Please sign in to comment.