From 8016078e4ff08755bd8c1d89ce6eaaee5078d4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michele=20Orr=C3=B9?= Date: Wed, 4 Jan 2017 22:32:50 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Deleting=20a=20CofProfile=20=E2=87=92=20Del?= =?UTF-8?q?eting=20a=20User.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There's no reason for keeping a User if its respective cofprofile was deleted. No? Unittests attached. --- gestioncof/models.py | 8 +++++++- gestioncof/tests.py | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) 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()) From 542d02ca6dc68163d2c3c54dc2ddb68940b10094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michele=20Orr=C3=B9?= Date: Wed, 4 Jan 2017 22:37:03 +0100 Subject: [PATCH 2/3] Cosmetics: post_save-ing a CofProfile now with decorators. --- gestioncof/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gestioncof/models.py b/gestioncof/models.py index 6ed93c46..01798180 100644 --- a/gestioncof/models.py +++ b/gestioncof/models.py @@ -79,10 +79,10 @@ class CofProfile(models.Model): return six.text_type(self.user.username) +@receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: CofProfile.objects.get_or_create(user=instance) -post_save.connect(create_user_profile, sender=User) @receiver(post_delete, sender=CofProfile) From 8568ecedba767c529ae3eed5461133c73cb9a540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 6 Jan 2017 16:58:27 +0100 Subject: [PATCH 3/3] Update provisioning Django has now access to the test datebase --- provisioning/bootstrap.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/provisioning/bootstrap.sh b/provisioning/bootstrap.sh index 89e8790d..413c445e 100644 --- a/provisioning/bootstrap.sh +++ b/provisioning/bootstrap.sh @@ -21,6 +21,7 @@ echo "mysql-server mysql-server/root_password_again password $DBPASSWD" | debcon apt-get install -y mysql-server mysql -uroot -p$DBPASSWD -e "CREATE DATABASE $DBNAME; GRANT ALL PRIVILEGES ON $DBNAME.* TO '$DBUSER'@'localhost' IDENTIFIED BY '$DBPASSWD'" +mysql -uroot -p$DBPASSWD -e "CREATE DATABASE test_$DBNAME; GRANT ALL PRIVILEGES ON test_$DBNAME.* TO '$DBUSER'@'localhost'" # Installation et configuration d'Apache apt-get install -y apache2