diff gpp/bio/signals.py @ 44:08cd19c1ee50

Added a signal handler for the user model to create a UserProfile for new users.
author Brian Neal <bgneal@gmail.com>
date Wed, 17 Jun 2009 00:47:33 +0000
parents
children b4305e18d3af
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/bio/signals.py	Wed Jun 17 00:47:33 2009 +0000
@@ -0,0 +1,22 @@
+"""
+Signal handler(s) for the bio application.
+"""
+from django.db.models.signals import post_save
+from django.contrib.auth.models import User
+from bio.models import UserProfile
+
+def on_user_save(sender, **kwargs):
+    """
+    This signal handler ensures that every User has a corresonding
+    UserProfile. It is called after User instance is saved. It creates
+    a UserProfile for the User if the created argument is True.
+    """
+    created = kwargs['created']
+    if created:
+        user = kwargs['instance']
+        profile = UserProfile()
+        profile.user = user
+        profile.save()
+
+
+post_save.connect(on_user_save, sender=User)