From 4bc2c34df5e5eb3e7153286f59aa8bdaf0c23dc0 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Mon, 19 Sep 2011 10:33:51 +0800 Subject: [PATCH] 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: Jeremy Kerr --- apps/patchwork/models.py | 11 ++++---- apps/patchwork/tests/registration.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index 22062c2..bb8d8e7 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -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) diff --git a/apps/patchwork/tests/registration.py b/apps/patchwork/tests/registration.py index 18b781f..08ed66a 100644 --- a/apps/patchwork/tests/registration.py +++ b/apps/patchwork/tests/registration.py @@ -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) -- 2.39.2