8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 09:26:27 +00:00
re2o/users/management/commands/archive.py

112 lines
3.9 KiB
Python
Raw Normal View History

2019-09-29 15:57:31 +00:00
# ⁻*- 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
2019-09-29 15:57:31 +00:00
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)
2019-09-29 15:57:31 +00:00
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="Fully archive users, i.e. delete their email address, machines and remove them from the LDAP.",
2019-09-29 15:57:31 +00:00
)
parser.add_argument(
"--date",
"-d",
default=datetime.date.today().strftime("%d/%m/%Y"),
2019-09-29 15:57:31 +00:00
type=valid_date,
help="Users whose membership ends sooner than this date will be archived.",
2019-09-29 15:57:31 +00:00
)
parser.add_argument(
"--show",
"-s",
action="store_true",
help="Only show a list of users, without doing anything.",
2019-09-29 15:57:31 +00:00
)
parser.add_argument(
"-y",
action="store_true",
help="Do not ask for confirmation before fully archiving.",
2019-09-29 15:57:31 +00:00
)
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)
)
2019-09-29 15:57:31 +00:00
if show:
self.stdout.write("%s users found : " % to_archive_list.count())
self.stdout.write("\n".join(map(str, to_archive_list.all())))
2019-09-29 15:57:31 +00:00
return
if full_archive and not force:
self.stdout.write(
self.style.WARNING(
"Please confirm fully archiving (it is a critical operation!) [Y/n]"
2019-09-29 15:57:31 +00:00
)
)
if input() != "Y":
2019-09-29 15:57:31 +00:00
self.stdout.write("Leaving without archiving.")
return
if full_archive:
self.stdout.write(
"Fully archiving users with a membership ending prior to %s."
% date.strftime("%d/%m/%Y")
)
2019-09-29 15:57:31 +00:00
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")
)
2019-09-29 15:57:31 +00:00
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())
)