Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pull invitation allocation logic into model methods

  • Loading branch information...
commit 36ab08d65aef8253e9c327dc988b3172f4dd0f2a 1 parent f08cb42
@paltman paltman authored
View
8 kaleo/management/commands/add_invites.py
@@ -2,8 +2,6 @@
from django.core.management.base import BaseCommand
-from django.contrib.auth.models import User
-
from kaleo.models import InvitationStat
@@ -19,8 +17,4 @@ def handle(self, *args, **kwargs):
except ValueError:
sys.exit("The argument for number of invites must be an integer.")
- for user in User.objects.all():
- stat, _ = InvitationStat.objects.get_or_create(user=user)
- if stat.invites_remaining() == 0:
- stat.invites_allocated += num_of_invites
- stat.save()
+ InvitationStat.add_invites(num_of_invites)
View
9 kaleo/management/commands/topoff_invites.py
@@ -2,8 +2,6 @@
from django.core.management.base import BaseCommand
-from django.contrib.auth.models import User
-
from kaleo.models import InvitationStat
@@ -19,9 +17,4 @@ def handle(self, *args, **kwargs):
except ValueError:
sys.exit("The argument for number of invites must be an integer.")
- for user in User.objects.all():
- stat, _ = InvitationStat.objects.get_or_create(user=user)
- remaining = stat.invites_remaining()
- if remaining < num_of_invites:
- stat.invites_allocated += (num_of_invites - remaining)
- stat.save()
+ InvitationStat.topoff(num_of_invites)
View
26 kaleo/models.py
@@ -72,6 +72,32 @@ class InvitationStat(models.Model):
invites_allocated = models.IntegerField(default=DEFAULT_INVITE_ALLOCATION)
invites_accepted = models.IntegerField(default=0)
+ @classmethod
+ def add_invites_to_user(cls, user, amount):
+ stat, _ = InvitationStat.objects.get_or_create(user=user)
+ stat.invites_allocated = -1
+ stat.save()
+
+ @classmethod
+ def add_invites(cls, amount):
+ for user in User.objects.all():
+ cls.add_invites_to_user(user, amount)
+
+ @classmethod
+ def topoff_user(cls, user, amount):
+ "Makes sure user has a certain number of invites"
+ stat, _ = cls.objects.get_or_create(user=user)
+ remaining = stat.invites_remaining()
+ if remaining < amount:
+ stat.invites_allocated += (amount - remaining)
+ stat.save()
+
+ @classmethod
+ def topoff(cls, amount):
+ "Makes sure all users have a certain number of invites"
+ for user in User.objects.all():
+ cls.topoff_user(user, amount)
+
def invites_remaining(self):
if self.invites_allocated == -1:
return -1
Please sign in to comment.
Something went wrong with that request. Please try again.