Skip to content

Commit

Permalink
Clean up style
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Feb 23, 2014
1 parent 3fc6d86 commit 4c3aeb5
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 46 deletions.
9 changes: 8 additions & 1 deletion kaleo/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
class InvitationStatAdmin(admin.ModelAdmin):
raw_id_fields = ["user"]
readonly_fields = ["invites_sent", "invites_accepted"]
list_display = ["user", "invites_sent", "invites_accepted", "invites_allocated", "invites_remaining", "can_send"]
list_display = [
"user",
"invites_sent",
"invites_accepted",
"invites_allocated",
"invites_remaining",
"can_send"
]
list_filter = ["invites_sent", "invites_accepted"]


Expand Down
4 changes: 3 additions & 1 deletion kaleo/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def get_user_lookup_kwargs(kwargs):
return result


def receiver(signal, **kwargs):
def receiver(signal, **kwargs): # noqa
if django.VERSION < (1, 7, 0):
unresolved_references = {}

def _resolve_references(sender, **kwargs):
opts = sender._meta
reference = (opts.app_label, opts.object_name)
Expand All @@ -45,6 +46,7 @@ def _resolve_references(sender, **kwargs):
kwargs["sender"] = sender
signal.connect(func, **kwargs)
class_prepared.connect(_resolve_references, weak=False)

def _decorator(func):
if django.VERSION < (1, 7, 0):
from django.db.models.loading import cache as app_cache
Expand Down
2 changes: 1 addition & 1 deletion kaleo/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

from django.conf import settings
from django.conf import settings # noqa

from appconf import AppConf

Expand Down
4 changes: 2 additions & 2 deletions kaleo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

class InviteForm(forms.Form):
email_address = forms.EmailField()

def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
super(InviteForm, self).__init__(*args, **kwargs)

def clean_email_address(self):
email = self.cleaned_data["email_address"]
if EmailAddress.objects.filter(email=email, verified=True).exists():
Expand Down
6 changes: 3 additions & 3 deletions kaleo/management/commands/add_invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

class Command(BaseCommand):
help = "Adds invites to all users with 0 invites remaining."

def handle(self, *args, **kwargs):
if len(args) == 0:
sys.exit("You must supply the number of invites as an argument.")

try:
num_of_invites = int(args[0])
except ValueError:
sys.exit("The argument for number of invites must be an integer.")

InvitationStat.add_invites(num_of_invites)
2 changes: 1 addition & 1 deletion kaleo/management/commands/infinite_invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Command(BaseCommand):
help = "Sets invites_allocated to -1 to represent infinite invites."

def handle(self, *args, **kwargs):
for user in get_user_model().objects.all():
stat, _ = InvitationStat.objects.get_or_create(user=user)
Expand Down
6 changes: 3 additions & 3 deletions kaleo/management/commands/topoff_invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

class Command(BaseCommand):
help = "Makes sure all users have a certain number of invites."

def handle(self, *args, **kwargs):
if len(args) == 0:
sys.exit("You must supply the number of invites as an argument.")

try:
num_of_invites = int(args[0])
except ValueError:
sys.exit("The argument for number of invites must be an integer.")

InvitationStat.topoff(num_of_invites)
32 changes: 16 additions & 16 deletions kaleo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ class NotEnoughInvitationsError(Exception):


class JoinInvitation(models.Model):

STATUS_SENT = 1
STATUS_ACCEPTED = 2
STATUS_JOINED_INDEPENDENTLY = 3

INVITE_STATUS_CHOICES = [
(STATUS_SENT, "Sent"),
(STATUS_ACCEPTED, "Accepted"),
(STATUS_JOINED_INDEPENDENTLY, "Joined Independently")
]

from_user = models.ForeignKey(AUTH_USER_MODEL, related_name="invites_sent")
to_user = models.ForeignKey(AUTH_USER_MODEL, null=True, related_name="invites_received")
message = models.TextField(null=True)
sent = models.DateTimeField(default=timezone.now)
status = models.IntegerField(choices=INVITE_STATUS_CHOICES)
signup_code = models.OneToOneField(SignupCode)

def to_user_email(self):
return self.signup_code.email

def accept(self, user):
self.to_user = user
self.status = JoinInvitation.STATUS_ACCEPTED
self.save()
self.from_user.invitationstat.increment_accepted()
invite_accepted.send(sender=JoinInvitation, invitation=self)

@classmethod
def process_independent_joins(cls, user, email):
invites = cls.objects.filter(
Expand All @@ -52,12 +52,12 @@ def process_independent_joins(cls, user, email):
invite.status = cls.STATUS_JOINED_INDEPENDENTLY
invite.save()
joined_independently.send(sender=cls, invitation=invite)

@classmethod
def invite(cls, from_user, to_email, message=None):
if not from_user.invitationstat.can_send():
raise NotEnoughInvitationsError()

signup_code = SignupCode.create(
email=to_email,
inviter=from_user,
Expand All @@ -80,28 +80,28 @@ def invite(cls, from_user, to_email, message=None):


class InvitationStat(models.Model):

user = models.OneToOneField(AUTH_USER_MODEL)
invites_sent = models.IntegerField(default=0)
invites_allocated = models.IntegerField(default=settings.KALEO_DEFAULT_INVITE_ALLOCATION)
invites_accepted = models.IntegerField(default=0)

def increment_accepted(self):
self.invites_accepted += 1
self.save()

@classmethod
def add_invites_to_user(cls, user, amount):
stat, _ = InvitationStat.objects.get_or_create(user=user)
if stat.invites_allocated != -1:
stat.invites_allocated += amount
stat.save()

@classmethod
def add_invites(cls, amount):
for user in get_user_model().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"
Expand All @@ -110,18 +110,18 @@ def topoff_user(cls, user, amount):
if remaining != -1 and 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 get_user_model().objects.all():
cls.topoff_user(user, amount)

def invites_remaining(self):
if self.invites_allocated == -1:
return -1
return self.invites_allocated - self.invites_sent

def can_send(self):
if self.invites_allocated == -1:
return True
Expand Down
34 changes: 18 additions & 16 deletions kaleo/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
from django.test import TestCase
from account.models import SignupCode
from kaleo.models import JoinInvitation, InvitationStat

from django.contrib.auth.models import User

from account.models import SignupCode
from kaleo.models import JoinInvitation


class TestsJoinInvitation(TestCase):

def setUp(self):
self.to_user = User.objects.create(username='foo1')
self.from_user = User.objects.create(username='foo2')
self.to_user = User.objects.create(username='foo1')
self.from_user = User.objects.create(username='foo2')
self.signup_code = SignupCode.create(email="me@you.com")
self.signup_code.save()
self.status = JoinInvitation.STATUS_ACCEPTED
self.signup_code.save()
self.status = JoinInvitation.STATUS_ACCEPTED
self.invitation = JoinInvitation.objects.create(
from_user = self.from_user,
status = self.status,
signup_code = self.signup_code,
)
from_user=self.from_user,
status=self.status,
signup_code=self.signup_code,
)

def test_to_user_email(self):
self.assertEqual(self.signup_code.email,"me@you.com")
self.assertEqual(self.signup_code.email, "me@you.com")

def test_accept(self):
self.invitation.accept(self.to_user)
self.assertEqual(self.from_user.invitationstat.invites_accepted,1)
self.assertEqual(self.from_user.invitationstat.invites_accepted, 1)

def test_process_independent_joins(self):

JoinInvitation.process_independent_joins(self.to_user,"me@you.com")
JoinInvitation.process_independent_joins(self.to_user, "me@you.com")
invite = JoinInvitation.objects.get(pk=self.invitation.pk)
self.assertEqual(invite.status,JoinInvitation.STATUS_JOINED_INDEPENDENTLY)
self.assertEqual(invite.status, JoinInvitation.STATUS_JOINED_INDEPENDENTLY)
2 changes: 0 additions & 2 deletions kaleo/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@
"",
(r"^", include("kaleo.urls")),
)


0 comments on commit 4c3aeb5

Please sign in to comment.