From e1575c09d47db0b5b2b26f28cd6df82530519e98 Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Sun, 29 Sep 2019 17:57:10 +0200 Subject: [PATCH 1/2] Fix mass_delete of machine so that AccessPoint deletion is possible. --- machines/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/machines/models.py b/machines/models.py index 68c7c58b..fdc7821e 100644 --- a/machines/models.py +++ b/machines/models.py @@ -258,11 +258,13 @@ class Machine(RevMixin, FieldPermissionModelMixin, models.Model): @classmethod def mass_delete(cls, machine_queryset): """Mass delete for machine queryset""" + from topologie.models import AccessPoint Domain.objects.filter(cname__interface_parent__machine__in=machine_queryset)._raw_delete(machine_queryset.db) Domain.objects.filter(interface_parent__machine__in=machine_queryset)._raw_delete(machine_queryset.db) Ipv6List.objects.filter(interface__machine__in=machine_queryset)._raw_delete(machine_queryset.db) Interface.objects.filter(machine__in=machine_queryset).filter(port_lists__isnull=False).delete() Interface.objects.filter(machine__in=machine_queryset)._raw_delete(machine_queryset.db) + AccessPoint.objects.filter(machine_ptr__in=machine_queryset)._raw_delete(machine_queryset.db) machine_queryset._raw_delete(machine_queryset.db) @cached_property From 693c2b567606b41f7b0f496367f5e21ef8fa70e8 Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Sun, 29 Sep 2019 17:57:31 +0200 Subject: [PATCH 2/2] Fix #218 --- users/management/commands/archive.py | 99 ++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 users/management/commands/archive.py diff --git a/users/management/commands/archive.py b/users/management/commands/archive.py new file mode 100644 index 00000000..c2ff7801 --- /dev/null +++ b/users/management/commands/archive.py @@ -0,0 +1,99 @@ +# ⁻*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Hugo Levy--Falk +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import argparse +import datetime + +from django.core.management.base import BaseCommand, CommandError +from django.utils.timezone import make_aware + +from re2o.utils import all_has_access +from users.models import User + +def valid_date(s): + try: + return make_aware(datetime.datetime.strptime(s, "%d/%m/%Y")) + except ValueError: + msg = "Not a valid date: '{0}'.".format(s) + raise argparse.ArgumentTypeError(msg) + +class Command(BaseCommand): + help = "Allow unactive users archiving by unassigning their IP addresses." + + def add_arguments(self, parser): + parser.add_argument( + '--full', + '-f', + action='store_true', + help="Full archive users, i.e. delete their email address, machines and remove them from the LDAP." + ) + parser.add_argument( + '--date', + '-d', + default=datetime.date.today().strftime('%d/%m/%Y'), + type=valid_date, + help="Users which membership ends sooner than this date will be archived.", + ) + parser.add_argument( + '--show', + '-s', + action='store_true', + help="Only show a list of users, without doing anything." + ) + parser.add_argument( + '-y', + action='store_true', + help='Do not ask for confirmation befor full archiving.' + ) + + def handle(self, *args, **kwargs): + full_archive = kwargs["full"] + date = kwargs["date"] + force = kwargs["y"] + show = kwargs["show"] + + to_archive_list = User.objects.exclude(id__in=all_has_access()).exclude(id__in=all_has_access(search_time=date)).exclude(state=User.STATE_NOT_YET_ACTIVE).exclude(state=User.STATE_FULL_ARCHIVE) + + if show: + self.stdout.write( + "%s users found : " % to_archive_list.count() + ) + self.stdout.write('\n'.join(map(str, to_archive_list.all()))) + return + + if full_archive and not force: + self.stdout.write( + self.style.WARNING( + "Please confirm full archiving (it is a critical operation !) [Y/n]" + ) + ) + if input() != 'Y': + self.stdout.write("Leaving without archiving.") + return + if full_archive: + self.stdout.write("Full archiving users with a membership ending prior to %s" % date.strftime("%d/%m/%Y")) + User.mass_full_archive(to_archive_list) + else: + self.stdout.write("Archiving users with a membership ending prior to %s" % date.strftime("%d/%m/%Y")) + to_archive_list = to_archive_list.exclude(state=User.STATE_ARCHIVE) + User.mass_archive(to_archive_list) + self.stdout.write(self.style.SUCCESS("%s users were archived." % to_archive_list.count())) +