Browse files

Add ability to prevent negative totals

  • Loading branch information...
1 parent fcf1f1b commit e51bd38ea121c89febc43f1a1f92e6a6e13e390f @paltman paltman committed Oct 10, 2011
Showing with 28 additions and 0 deletions.
  1. +10 −0 agon/models.py
  2. +17 −0 agon/tests.py
  3. +1 −0 runtests.py
View
10 agon/models.py
@@ -1,6 +1,7 @@
import datetime
import itertools
+from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models, transaction, IntegrityError
@@ -11,6 +12,9 @@
from agon import signals
+ALLOW_NEGATIVE_TOTALS = getattr(settings, "AGON_ALLOW_NEGATIVE_TOTALS", True)
+
+
class PointValue(models.Model):
"""
Stores a key and its point value. Simple.
@@ -168,6 +172,12 @@ def award_points(target, key, reason="", source=None):
" a PointValue or an integer amount of points to award."
)
+ if not ALLOW_NEGATIVE_TOTALS:
+ total = points_awarded(target)
+ if total + points < 0:
+ reason = reason + "(floored from %s to 0)" % points
+ points = -total
+
apv = AwardedPointValue(points=points, value=point_value, reason=reason)
if isinstance(target, User):
apv.target_user = target
View
17 agon/tests.py
@@ -144,6 +144,23 @@ def test_unicode_generic_one_off_point_award(self):
)
+# class NegativePointsTestCase(BasePointsTestCase, TestCase):
+
+# def test_negative_totals_floored(self):
+# group = Group.objects.create(name="Dwarfs")
+# award_points(group, 500)
+# self.assertEqual(points_awarded(group), 500)
+# award_points(group, -700)
+# self.assertEqual(points_awarded(group), 0)
+
+# def test_negative_totals_unfloored(self):
+# group = Group.objects.create(name="Dwarfs")
+# award_points(group, 500)
+# self.assertEqual(points_awarded(group), 500)
+# award_points(group, -700)
+# self.assertEqual(points_awarded(group), -200)
+
+
class PointsTransactionTestCase(BasePointsTestCase, TransactionTestCase):
@skipIf(settings.DATABASE_ENGINE == "sqlite3")
View
1 runtests.py
@@ -14,6 +14,7 @@
"django.contrib.auth",
"agon",
],
+ AGON_ALLOW_NEGATIVE_TOTALS = False
)
def runtests(*test_args):

0 comments on commit e51bd38

Please sign in to comment.