Commit 4bc2c34d authored by Jeremy Kerr's avatar Jeremy Kerr
Browse files

models: Update Person & UserProfile object whenever User is saved



Currently, UserProfiles are only saved on creation of the User object,
before the first_name and last_name attributes are set. This means that
we fallback to using the User.username value as the new Person.name.

This change modifies User's post-save signal to always update the Person
object, both on creation and updates. This means we get the proper name
appearing on Person instances.
Signed-off-by: default avatarJeremy Kerr <jk@ozlabs.org>
parent 75d8cf96
......@@ -136,13 +136,14 @@ class UserProfile(models.Model):
def __unicode__(self):
return self.name()
def _user_created_callback(sender, created, instance, **kwargs):
if not created:
return
profile = UserProfile(user = instance)
def _user_saved_callback(sender, created, instance, **kwargs):
try:
profile = instance.get_profile()
except UserProfile.DoesNotExist:
profile = UserProfile(user = instance)
profile.save()
models.signals.post_save.connect(_user_created_callback, sender = User)
models.signals.post_save.connect(_user_saved_callback, sender = User)
class State(models.Model):
name = models.CharField(max_length = 100)
......
......@@ -147,4 +147,44 @@ class RegistrationConfirmationTest(TestCase):
self.assertTrue(conf.user.is_active)
self.assertFalse(conf.active)
def testRegistrationNewPersonSetup(self):
""" Check that the person object created after registration has the
correct details """
# register
self.assertEqual(EmailConfirmation.objects.count(), 0)
response = self.client.post('/register/', self.default_data)
self.assertEquals(response.status_code, 200)
# confirm
conf = EmailConfirmation.objects.filter()[0]
response = self.client.get(_confirmation_url(conf))
self.assertEquals(response.status_code, 200)
person = Person.objects.get(email = self.user.email)
self.assertEquals(person.name,
self.user.firstname + ' ' + self.user.lastname)
def testRegistrationExistingPersonSetup(self):
""" Check that the person object created after registration has the
correct details """
fullname = self.user.firstname + ' ' + self.user.lastname
person = Person(name = fullname, email = self.user.email)
person.save()
# register
self.assertEqual(EmailConfirmation.objects.count(), 0)
response = self.client.post('/register/', self.default_data)
self.assertEquals(response.status_code, 200)
# confirm
conf = EmailConfirmation.objects.filter()[0]
response = self.client.get(_confirmation_url(conf))
self.assertEquals(response.status_code, 200)
person = Person.objects.get(email = self.user.email)
self.assertEquals(person.name, fullname)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment