From e75eb9861010bc7a5eaed01712cbdaaec4acfdd8 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Wed, 15 Jan 2014 16:08:39 -0500 Subject: [PATCH] Gracefully handle failure when creating a UserProfile #270 --- karmaworld/apps/users/models.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index 53106dc..788b528 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -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)) -- 2.25.1