diff --git a/gestioncof/models.py b/gestioncof/models.py index 19590aff..6ed93c46 100644 --- a/gestioncof/models.py +++ b/gestioncof/models.py @@ -5,11 +5,12 @@ from __future__ import print_function from __future__ import unicode_literals from django.db import models +from django.dispatch import receiver from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import python_2_unicode_compatible import django.utils.six as six -from django.db.models.signals import post_save +from django.db.models.signals import post_save, post_delete from gestioncof.petits_cours_models import choices_length @@ -84,6 +85,11 @@ def create_user_profile(sender, instance, created, **kwargs): post_save.connect(create_user_profile, sender=User) +@receiver(post_delete, sender=CofProfile) +def post_delete_user(sender, instance, *args, **kwargs): + instance.user.delete() + + @python_2_unicode_compatible class Club(models.Model): name = models.CharField("Nom", max_length=200, unique=True) diff --git a/gestioncof/tests.py b/gestioncof/tests.py index a83ebffc..66043daf 100644 --- a/gestioncof/tests.py +++ b/gestioncof/tests.py @@ -12,10 +12,22 @@ from __future__ import unicode_literals from django.test import TestCase +from gestioncof.models import CofProfile, User + class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) + def test_delete_user(self): + u = User(username='foo', first_name='foo', last_name='bar') + + # to each user there's a cofprofile associated + u.save() + self.assertTrue(CofProfile.objects.filter(user__username='foo').exists()) + + # there's no point in having a cofprofile without a user associated. + u.delete() + self.assertFalse(CofProfile.objects.filter(user__username='foo').exists()) + + # there's no point in having a user without a cofprofile associated. + u.save() + CofProfile.objects.get(user__username='foo').delete() + self.assertFalse(User.objects.filter(username='foo').exists())