forked from DGNum/gestioCOF
Merge branch 'michele.orru/gestioCOF-master'
Deleting a CofProfile => Deleting the associated User
This commit is contained in:
commit
672b8727b7
3 changed files with 26 additions and 7 deletions
|
@ -5,11 +5,12 @@ from __future__ import print_function
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.dispatch import receiver
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
import django.utils.six as six
|
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
|
from gestioncof.petits_cours_models import choices_length
|
||||||
|
|
||||||
|
@ -78,10 +79,15 @@ class CofProfile(models.Model):
|
||||||
return six.text_type(self.user.username)
|
return six.text_type(self.user.username)
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_save, sender=User)
|
||||||
def create_user_profile(sender, instance, created, **kwargs):
|
def create_user_profile(sender, instance, created, **kwargs):
|
||||||
if created:
|
if created:
|
||||||
CofProfile.objects.get_or_create(user=instance)
|
CofProfile.objects.get_or_create(user=instance)
|
||||||
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
|
@python_2_unicode_compatible
|
||||||
|
|
|
@ -12,10 +12,22 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from gestioncof.models import CofProfile, User
|
||||||
|
|
||||||
|
|
||||||
class SimpleTest(TestCase):
|
class SimpleTest(TestCase):
|
||||||
def test_basic_addition(self):
|
def test_delete_user(self):
|
||||||
"""
|
u = User(username='foo', first_name='foo', last_name='bar')
|
||||||
Tests that 1 + 1 always equals 2.
|
|
||||||
"""
|
# to each user there's a cofprofile associated
|
||||||
self.assertEqual(1 + 1, 2)
|
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())
|
||||||
|
|
|
@ -21,6 +21,7 @@ echo "mysql-server mysql-server/root_password_again password $DBPASSWD" | debcon
|
||||||
apt-get install -y mysql-server
|
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 $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
|
# Installation et configuration d'Apache
|
||||||
apt-get install -y apache2
|
apt-get install -y apache2
|
||||||
|
|
Loading…
Reference in a new issue