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-11-04 16:55:03 +00:00
|
|
|
|
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-11-04 16:55:03 +00:00
|
|
|
|
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(
|
2019-11-04 16:55:03 +00:00
|
|
|
"--full",
|
|
|
|
"-f",
|
|
|
|
action="store_true",
|
2019-11-16 14:11:57 +00:00
|
|
|
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(
|
2019-11-04 16:55:03 +00:00
|
|
|
"--date",
|
|
|
|
"-d",
|
|
|
|
default=datetime.date.today().strftime("%d/%m/%Y"),
|
2019-09-29 15:57:31 +00:00
|
|
|
type=valid_date,
|
2019-11-16 14:11:57 +00:00
|
|
|
help="Users whose membership ends sooner than this date will be archived.",
|
2019-09-29 15:57:31 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-11-04 16:55:03 +00:00
|
|
|
"--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(
|
2019-11-04 16:55:03 +00:00
|
|
|
"-y",
|
|
|
|
action="store_true",
|
2019-11-16 14:11:57 +00:00
|
|
|
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"]
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
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:
|
2019-11-04 16:55:03 +00:00
|
|
|
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(
|
2019-11-16 14:11:57 +00:00
|
|
|
"Please confirm fully archiving (it is a critical operation!) [Y/n]"
|
2019-09-29 15:57:31 +00:00
|
|
|
)
|
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
if input() != "Y":
|
2019-09-29 15:57:31 +00:00
|
|
|
self.stdout.write("Leaving without archiving.")
|
|
|
|
return
|
|
|
|
if full_archive:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.stdout.write(
|
2019-11-16 14:11:57 +00:00
|
|
|
"Fully archiving users with a membership ending prior to %s."
|
2019-11-04 16:55:03 +00:00
|
|
|
% date.strftime("%d/%m/%Y")
|
|
|
|
)
|
2019-09-29 15:57:31 +00:00
|
|
|
User.mass_full_archive(to_archive_list)
|
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.stdout.write(
|
2019-11-16 14:11:57 +00:00
|
|
|
"Archiving users with a membership ending prior to %s."
|
2019-11-04 16:55:03 +00:00
|
|
|
% 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)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS("%s users were archived." % to_archive_list.count())
|
|
|
|
)
|