Skip to content

Commit

Permalink
Move receiver for user creation signal to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Feb 15, 2013
1 parent 96eb2ba commit a77e505
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
19 changes: 1 addition & 18 deletions account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
from django.core.urlresolvers import reverse
from django.db import models, transaction
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils import timezone, translation
from django.utils.translation import gettext_lazy as _
from django.conf import settings as django_settings

from django.contrib.auth.models import AnonymousUser, User
from django.contrib.auth.models import AnonymousUser
from django.contrib.sites.models import Site

import pytz
Expand Down Expand Up @@ -92,21 +90,6 @@ def localtime(self, value):
return value.astimezone(pytz.timezone(timezone))


@receiver(post_save, sender=User)
def user_post_save(sender, **kwargs):
"""
After User.save is called we check to see if it was a created user. If so,
we check if the User object wants account creation. If all passes we
create an Account object.
We only run on user creation to avoid having to check for existence on
each call to User.save.
"""
user, created = kwargs["instance"], kwargs["created"]
disabled = getattr(user, "_disable_account_creation", not settings.ACCOUNT_CREATE_ON_SAVE)
if created and not disabled:
Account.create(user=user)


class AnonymousAccount(object):

Expand Down
20 changes: 20 additions & 0 deletions account/receivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.auth.models import User

from .models import Account

@receiver(post_save, sender=User)
def user_post_save(sender, **kwargs):
"""
After User.save is called we check to see if it was a created user. If so,
we check if the User object wants account creation. If all passes we
create an Account object.
We only run on user creation to avoid having to check for existence on
each call to User.save.
"""
user, created = kwargs["instance"], kwargs["created"]
disabled = getattr(user, "_disable_account_creation", not settings.ACCOUNT_CREATE_ON_SAVE)
if created and not disabled:
Account.create(user=user)

0 comments on commit a77e505

Please sign in to comment.