Gracefully handle failure when creating a UserProfile #270
authorCharles Connell <charles@connells.org>
Wed, 15 Jan 2014 21:08:39 +0000 (16:08 -0500)
committerCharles Connell <charles@connells.org>
Wed, 15 Jan 2014 21:08:39 +0000 (16:08 -0500)
karmaworld/apps/users/models.py

index 53106dc31556bcfd7f710c2ceeae47c932a561c1..788b52845724d2c95ed74659e198a899f70040ac 100644 (file)
@@ -2,14 +2,17 @@
 # -*- coding:utf8 -*-
 # Copyright (C) 2013  FinalsClub Foundation
 import random
+import logging
 from allauth.account.signals import user_logged_in
 from django.contrib.auth.models import User
 from django.core.exceptions import ObjectDoesNotExist
 from django.db.models.signals import post_save, pre_save
 from django.dispatch import receiver
-from django.db import models
+from django.db import models, DatabaseError
+from django.middleware.transaction import transaction
 from karmaworld.apps.courses.models import School
 
+logger = logging.getLogger(__name__)
 
 class UserProfile(models.Model):
     user      = models.OneToOneField(User)
@@ -50,5 +53,9 @@ def assign_username(sender, instance, **kwargs):
 @receiver(post_save, sender=User, weak=True)
 def create_user_profile(sender, instance, created, **kwargs):
     if created:
-        UserProfile.objects.create(user=instance)
+        with transaction.commit_on_success():
+            try:
+                UserProfile.objects.create(user=instance)
+            except DatabaseError:
+                logger.warn("Could not create UserProfile for user {u}. This is okay if running syncdb.".format(u=instance))