~dooferlad/patchwork/niceties

« back to all changes in this revision

Viewing changes to apps/patchwork/models.py

  • Committer: Jeremy Kerr
  • Date: 2011-09-19 02:33:51 UTC
  • Revision ID: git-v1:4bc2c34df5e5eb3e7153286f59aa8bdaf0c23dc0
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>

Show diffs side-by-side

added added

removed removed

Lines of Context:
136
136
    def __unicode__(self):
137
137
        return self.name()
138
138
 
139
 
def _user_created_callback(sender, created, instance, **kwargs):
140
 
    if not created:
141
 
        return
142
 
    profile = UserProfile(user = instance)
 
139
def _user_saved_callback(sender, created, instance, **kwargs):
 
140
    try:
 
141
        profile = instance.get_profile()
 
142
    except UserProfile.DoesNotExist:
 
143
        profile = UserProfile(user = instance)
143
144
    profile.save()
144
145
 
145
 
models.signals.post_save.connect(_user_created_callback, sender = User)
 
146
models.signals.post_save.connect(_user_saved_callback, sender = User)
146
147
 
147
148
class State(models.Model):
148
149
    name = models.CharField(max_length = 100)