diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2011-09-19 10:33:51 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2011-09-19 10:33:51 +0800 |
commit | 4bc2c34df5e5eb3e7153286f59aa8bdaf0c23dc0 (patch) | |
tree | 971498466574214e4a3a90a451278e4386e51768 | |
parent | 75d8cf966034e673afe0077ba393d8b2eb3e9b93 (diff) | |
download | patchwork-4bc2c34df5e5eb3e7153286f59aa8bdaf0c23dc0.tar.bz2 patchwork-4bc2c34df5e5eb3e7153286f59aa8bdaf0c23dc0.tar.xz |
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 <jk@ozlabs.org>
-rw-r--r-- | apps/patchwork/models.py | 11 | ||||
-rw-r--r-- | 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) |