Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #1813: Refactor error messages for rewards #1832

Merged
merged 6 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 14 additions & 6 deletions evap/rewards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ 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:
error_string = ""
niklasmohrin marked this conversation as resolved.
Show resolved Hide resolved
status_code = 400
if isinstance(error, NoPointsSelected):
error_string = "You cannot redeem 0 points."
richardebeling marked this conversation as resolved.
Show resolved Hide resolved
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.warning(request, _(error_string))
richardebeling marked this conversation as resolved.
Show resolved Hide resolved
janno42 marked this conversation as resolved.
Show resolved Hide resolved
return status_code
return 200


Expand Down