diff --git a/machines/migrations/0096_auto_20181013_1417.py b/machines/migrations/0096_auto_20181013_1417.py new file mode 100644 index 00000000..745fb523 --- /dev/null +++ b/machines/migrations/0096_auto_20181013_1417.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-10-13 12:17 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0095_auto_20180919_2225'), + ] + + operations = [ + migrations.AlterField( + model_name='machine', + name='user', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/machines/models.py b/machines/models.py index 282fc1ab..4caca48a 100644 --- a/machines/models.py +++ b/machines/models.py @@ -55,7 +55,7 @@ from re2o.mixins import AclMixin, RevMixin class Machine(RevMixin, FieldPermissionModelMixin, models.Model): """ Class définissant une machine, object parent user, objets fils interfaces""" - user = models.ForeignKey('users.User', on_delete=models.PROTECT) + user = models.ForeignKey('users.User', on_delete=models.CASCADE) name = models.CharField( max_length=255, help_text=_("Optional"), diff --git a/users/management/commands/clean_notyetactive.py b/users/management/commands/clean_notyetactive.py index 40f80c03..44c5b24e 100644 --- a/users/management/commands/clean_notyetactive.py +++ b/users/management/commands/clean_notyetactive.py @@ -19,6 +19,7 @@ from django.core.management.base import BaseCommand, CommandError from users.models import User +from cotisations.models import Facture from preferences.models import OptionalUser from datetime import timedelta @@ -28,7 +29,9 @@ class Command(BaseCommand): help = "Delete non members users (not yet active)" def handle(self, *args, **options): + """First deleting invalid invoices, and then deleting the users""" days = OptionalUser.get_cached_value('delete_notyetactive') - users = User.objects.filter(state=User.STATE_NOT_YET_ACTIVE).filter(registered__lte=timezone.now() - timedelta(days=days)) - print("Deleting " + str(users.count()) + " users") - users.delete() + users_to_delete = User.objects.filter(state=User.STATE_NOT_YET_ACTIVE).filter(registered__lte=timezone.now() - timedelta(days=days)).exclude(facture__valid=True).distinct() + print("Deleting " + str(users_to_delete.count()) + " users") + Facture.objects.filter(user__in=users_to_delete).delete() + users_to_delete.delete()