]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/models.py
models: use User.post_save signal to create UserProfile objects
[patchwork] / apps / patchwork / models.py
index 5c0ca9550bf08f6cf058bbd538f66b754894ac3b..6ad4e1ad3ff993c22a84e10ddb23ec1fe1b50047 100644 (file)
@@ -21,12 +21,10 @@ from django.db import models
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.contrib.sites.models import Site
-from django.conf import settings
 from patchwork.parser import hash_patch
 
 import re
 import datetime, time
-import string
 import random
 
 try:
@@ -44,8 +42,8 @@ except ImportError:
 
 class Person(models.Model):
     email = models.CharField(max_length=255, unique = True)
-    name = models.CharField(max_length=255, null = True)
-    user = models.ForeignKey(User, null = True)
+    name = models.CharField(max_length=255, null = True, blank = True)
+    user = models.ForeignKey(User, null = True, blank = True)
 
     def __unicode__(self):
         if self.name:
@@ -71,7 +69,7 @@ class Project(models.Model):
 
 class UserProfile(models.Model):
     user = models.ForeignKey(User, unique = True)
-    primary_project = models.ForeignKey(Project, null = True)
+    primary_project = models.ForeignKey(Project, null = True, blank = True)
     maintainer_projects = models.ManyToManyField(Project,
             related_name = 'maintainer_project')
     send_email = models.BooleanField(default = False,
@@ -132,6 +130,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)
+    profile.save()
+
+models.signals.post_save.connect(_user_created_callback, sender = User)
+
 class State(models.Model):
     name = models.CharField(max_length = 100)
     ordering = models.IntegerField(unique = True)
@@ -188,10 +194,10 @@ class Patch(models.Model):
     state = models.ForeignKey(State)
     archived = models.BooleanField(default = False)
     headers = models.TextField(blank = True)
-    content = models.TextField(null = True)
-    pull_url = models.CharField(max_length=255, null = True)
+    content = models.TextField(null = True, blank = True)
+    pull_url = models.CharField(max_length=255, null = True, blank = True)
     commit_ref = models.CharField(max_length=255, null = True, blank = True)
-    hash = HashField(null = True, db_index = True)
+    hash = HashField(null = True, blank = True)
 
     def __unicode__(self):
         return self.name
@@ -322,7 +328,7 @@ class Bundle(models.Model):
         return self.patches.all().count()
 
     def ordered_patches(self):
-        return self.patches.order_by('bundlepatch__order');
+        return self.patches.order_by('bundlepatch__order')
 
     def append_patch(self, patch):
         # todo: use the aggregate queries in django 1.1