Skip to content

Commit

Permalink
Fix for #1813: Refactor error messages for rewards (#1832)
Browse files Browse the repository at this point in the history
* Refactor error messages for rewards

* Moved translation + added test case

* Reformatted file

* Removed empty error string

* Removed unused import

* Changed message type to error for rewards
  • Loading branch information
florian-str authored Dec 5, 2022
1 parent 32866a7 commit 60328bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
8 changes: 8 additions & 0 deletions evap/rewards/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def test_redeem_too_many_points(self):
self.assertContains(response, "have enough reward points.", status_code=400)
self.assertEqual(5, reward_points_of_user(self.student))

def test_redeem_zero_points(self):
response = self.app.get(self.url, user=self.student)
form = response.forms["reward-redemption-form"]
form.set(f"points-{self.event1.pk}", 0)
response = form.submit(status=400)
self.assertContains(response, "cannot redeem 0 points.", status_code=400)
self.assertEqual(5, reward_points_of_user(self.student))

def test_redeem_points_for_expired_event(self):
"""Regression test for #846"""
response = self.app.get(self.url, user=self.student)
Expand Down
12 changes: 4 additions & 8 deletions evap/rewards/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,22 @@ def save_redemptions(request, redemptions: Dict[int, int], previous_redeemed_poi
# check consistent previous redeemed points
# do not validate reward points, to allow receiving points after page load
if previous_redeemed_points != redeemed_points_of_user(request.user):
raise OutdatedRedemptionData(
_(
"It appears that your browser sent multiple redemption requests. You can see all successful redemptions below."
)
)
raise OutdatedRedemptionData()

total_points_available = reward_points_of_user(request.user)
total_points_redeemed = sum(redemptions.values())

if total_points_redeemed <= 0:
raise NoPointsSelected(_("You cannot redeem 0 points."))
raise NoPointsSelected()

if total_points_redeemed > total_points_available:
raise NotEnoughPoints(_("You don't have enough reward points."))
raise NotEnoughPoints()

for event_id in redemptions:
if redemptions[event_id] > 0:
event = get_object_or_404(RewardPointRedemptionEvent, pk=event_id)
if event.redeem_end_date < date.today():
raise RedemptionEventExpired(_("Sorry, the deadline for this event expired already."))
raise RedemptionEventExpired()

RewardPointRedemption.objects.create(user_profile=request.user, value=redemptions[event_id], event=event)

Expand Down
21 changes: 15 additions & 6 deletions evap/rewards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ def redeem_reward_points(request):
try:
save_redemptions(request, redemptions, previous_redeemed_points)
messages.success(request, _("You successfully redeemed your points."))
except (NoPointsSelected, NotEnoughPoints, RedemptionEventExpired) as error:
messages.warning(request, error)
return 400
except OutdatedRedemptionData as error:
messages.error(request, error)
return 409
except (NoPointsSelected, NotEnoughPoints, RedemptionEventExpired, OutdatedRedemptionData) as error:
status_code = 400
if isinstance(error, NoPointsSelected):
error_string = _("You cannot redeem 0 points.")
elif isinstance(error, NotEnoughPoints):
error_string = _("You don't have enough reward points.")
elif isinstance(error, RedemptionEventExpired):
error_string = _("Sorry, the deadline for this event expired already.")
elif isinstance(error, OutdatedRedemptionData):
status_code = 409
error_string = _(
"It appears that your browser sent multiple redemption requests. You can see all successful redemptions below."
)
messages.error(request, error_string)
return status_code
return 200


Expand Down

0 comments on commit 60328bd

Please sign in to comment.