From 7248bea3feb1239584618d0bee5f5ed688b2b0e7 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 15 Apr 2021 11:18:33 +0200 Subject: [PATCH] management: add command to cleanup old export files (#52626) --- debian/authentic2-multitenant.cron.d | 1 + debian/authentic2.cron.d | 1 + .../management/commands/clean-user-exports.py | 34 +++++++++++++++++++ tests/test_commands.py | 20 +++++++++++ 4 files changed, 56 insertions(+) create mode 100644 src/authentic2/management/commands/clean-user-exports.py diff --git a/debian/authentic2-multitenant.cron.d b/debian/authentic2-multitenant.cron.d index 21374b3d4..afaa4a061 100644 --- a/debian/authentic2-multitenant.cron.d +++ b/debian/authentic2-multitenant.cron.d @@ -6,3 +6,4 @@ MAILTO=root 10 * * * * authentic-multitenant authentic2-multitenant-manage tenant_command sync-ldap-users --all-tenants 15 * * * * authentic-multitenant authentic2-multitenant-manage tenant_command clean-unused-accounts --all-tenants 30 5 * * * authentic-multitenant authentic2-multitenant-manage tenant_command deactivate-orphaned-ldap-users --all-tenants +0 0 * * 0 authentic-multitenant authentic2-multitenant-manage tenant_command clean-user-exports --all-tenants diff --git a/debian/authentic2.cron.d b/debian/authentic2.cron.d index 7ae5dfbc8..d23679ad0 100644 --- a/debian/authentic2.cron.d +++ b/debian/authentic2.cron.d @@ -6,3 +6,4 @@ MAILTO=root 10 * * * * authentic2 authentic2-manage sync-ldap-users 0 5 * * * authentic2 authentic2-manage clean-unused-accounts 30 5 * * * authentic2 authentic2-manage deactivate-orphaned-ldap-users +0 0 * * 0 authentic2 authentic2-manage clean-user-exports diff --git a/src/authentic2/management/commands/clean-user-exports.py b/src/authentic2/management/commands/clean-user-exports.py new file mode 100644 index 000000000..652b7977c --- /dev/null +++ b/src/authentic2/management/commands/clean-user-exports.py @@ -0,0 +1,34 @@ +# authentic2 - versatile identity manager +# Copyright (C) 2010-2021 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import os +from datetime import datetime, timedelta +from shutil import rmtree + +from django.core.files.storage import default_storage +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = 'Clean old export files.' + + def handle(self, **options): + path = default_storage.path('user_exports') + for directory in os.listdir(path): + dir_path = os.path.join(path, directory) + modification_timestamp = os.path.getmtime(dir_path) + if datetime.now() - datetime.fromtimestamp(modification_timestamp) > timedelta(days=7): + rmtree(dir_path) diff --git a/tests/test_commands.py b/tests/test_commands.py index 2f03e62f2..b77a00724 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -21,6 +21,7 @@ from io import BufferedReader, BufferedWriter, TextIOWrapper import py import pytest +import webtest from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType from django.utils.timezone import now @@ -389,3 +390,22 @@ def test_clean_unused_account_max_mails_per_period(settings, db, mailoutbox, fre call_command('clean-unused-accounts') # 4 new alerts and 4 deletions notifications assert len(mailoutbox) == 4 + 8 + + +def test_clean_user_exports(settings, app, superuser, freezer): + users = [User(username='user%s' % i) for i in range(10)] + User.objects.bulk_create(users) + + resp = login(app, superuser, '/manage/users/') + resp = resp.click('CSV').follow() + file_creation_time = now() + assert resp.click('Download CSV') + + freezer.move_to(file_creation_time + datetime.timedelta(days=5)) + call_command('clean-user-exports') + assert resp.click('Download CSV') + + freezer.move_to(file_creation_time + datetime.timedelta(days=8)) + call_command('clean-user-exports') + with pytest.raises(webtest.app.AppError): + resp.click('Download CSV')