8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 07:02:26 +00:00

Merge branch '218-archive-command' into 'dev'

Resolve "Add a command to archive users"

See merge request federez/re2o!448
This commit is contained in:
chirac 2019-10-01 13:36:05 +02:00
commit 6e0a99cd50
2 changed files with 101 additions and 0 deletions

View file

@ -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

View file

@ -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()))