From 355e5d5a5e012d93d9441acc0b78a356b07245d9 Mon Sep 17 00:00:00 2001 From: detraz Date: Sat, 13 Oct 2018 15:32:27 +0200 Subject: [PATCH] Fix suppression machines + factures --- .../migrations/0096_auto_20181013_1417.py | 22 +++++++++++++++++++ machines/models.py | 2 +- .../management/commands/clean_notyetactive.py | 9 +++++--- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 machines/migrations/0096_auto_20181013_1417.py 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()