Repository: e-valuation/EvaP

Issue:
Reward point redemption error message
When users try to redeem 0 reward points they get the error message "You don't have enough reward points". The error message should be something like "You must select the number of reward points you want to redeem" instead.



Discussion:

Resolving patch:
diff --git a/evap/rewards/models.py b/evap/rewards/models.py
--- a/evap/rewards/models.py
+++ b/evap/rewards/models.py
@@ -3,6 +3,13 @@
 
 from collections import OrderedDict
 
+class NoPointsSelected(Exception):
+    """An attempt has been made to redeem <= 0 points."""
+    pass
+
+class NotEnoughPoints(Exception):
+    """An attempt has been made to redeem more points than available."""
+    pass
 
 class RewardPointRedemptionEvent(models.Model):
     name = models.CharField(max_length=1024, verbose_name=_("event name"))
diff --git a/evap/rewards/tools.py b/evap/rewards/tools.py
--- a/evap/rewards/tools.py
+++ b/evap/rewards/tools.py
@@ -7,7 +7,7 @@
 from django.contrib.auth.decorators import login_required
 from evap.evaluation.models import Course
 
-from evap.rewards.models import RewardPointGranting, RewardPointRedemption, RewardPointRedemptionEvent, SemesterActivation
+from evap.rewards.models import RewardPointGranting, RewardPointRedemption, RewardPointRedemptionEvent, SemesterActivation, NoPointsSelected, NotEnoughPoints
 
 @login_required
 @transaction.atomic
@@ -15,8 +15,11 @@ def save_redemptions(request, redemptions):
     total_points_available = reward_points_of_user(request.user)
     total_points_redeemed = sum(redemptions.values())
 
-    if total_points_redeemed == 0 or total_points_redeemed > total_points_available:
-        return False
+    if total_points_redeemed <= 0:
+        raise NoPointsSelected(_("You cannot redeem 0 points."))
+
+    if total_points_redeemed > total_points_available:
+        raise NotEnoughPoints(_("You don't have enough reward points."))
 
     for event_id in redemptions:
         if redemptions[event_id] > 0:
@@ -26,8 +29,6 @@ def save_redemptions(request, redemptions):
                 event=RewardPointRedemptionEvent.objects.get(id=event_id)
             )
             redemption.save()
-    return True
-
 
 def can_user_use_reward_points(user):
     return not user.is_external and user.is_participant
diff --git a/evap/rewards/views.py b/evap/rewards/views.py
--- a/evap/rewards/views.py
+++ b/evap/rewards/views.py
@@ -11,7 +11,7 @@
 
 from evap.staff.views import semester_view
 
-from evap.rewards.models import RewardPointGranting, RewardPointRedemption, RewardPointRedemptionEvent, SemesterActivation
+from evap.rewards.models import RewardPointGranting, RewardPointRedemption, RewardPointRedemptionEvent, SemesterActivation, NoPointsSelected, NotEnoughPoints
 from evap.rewards.tools import save_redemptions, reward_points_of_user, can_user_use_reward_points
 from evap.rewards.forms import RewardPointRedemptionEventForm
 from evap.rewards.exporters import ExcelExporter
@@ -25,10 +25,11 @@ def index(request):
                 event_id = int(key.rpartition('-')[2])
                 redemptions[event_id] = int(value)
 
-        if save_redemptions(request, redemptions):
+        try:
+            save_redemptions(request, redemptions)
             messages.success(request, _("You successfully redeemed your points."))
-        else:
-            messages.warning(request, _("You don't have enough reward points."))
+        except (NoPointsSelected, NotEnoughPoints) as error:
+            messages.warning(request, error)
 
     total_points_available = reward_points_of_user(request.user)
     reward_point_grantings = RewardPointGranting.objects.filter(user_profile=request.user)


Test patch: