management: add command to cleanup old export files (#52626)

This commit is contained in:
Valentin Deniaud 2021-04-15 11:18:33 +02:00
parent 49cee43b23
commit 7248bea3fe
4 changed files with 56 additions and 0 deletions

View File

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

View File

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

View File

@ -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 <http://www.gnu.org/licenses/>.
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)

View File

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