From: Charles Connell Date: Wed, 15 Jan 2014 21:08:39 +0000 (-0500) Subject: Gracefully handle failure when creating a UserProfile #270 X-Git-Tag: release-20150131~257 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=e75eb9861010bc7a5eaed01712cbdaaec4acfdd8;p=oweals%2Fkarmaworld.git Gracefully handle failure when creating a UserProfile #270 --- 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))