Projet

Général

Profil

0001-management-add-command-to-cleanup-old-export-files-5.patch

Valentin Deniaud, 15 avril 2021 11:20

Télécharger (4,63 ko)

Voir les différences:

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
debian/authentic2-multitenant.cron.d
6 6
10 * * * * authentic-multitenant authentic2-multitenant-manage tenant_command sync-ldap-users --all-tenants
7 7
15 * * * * authentic-multitenant authentic2-multitenant-manage tenant_command clean-unused-accounts --all-tenants
8 8
30 5 * * * authentic-multitenant authentic2-multitenant-manage tenant_command deactivate-orphaned-ldap-users --all-tenants
9
0 0 * * 0 authentic-multitenant authentic2-multitenant-manage tenant_command clean-user-exports --all-tenants
debian/authentic2.cron.d
6 6
10 * * * * authentic2 authentic2-manage sync-ldap-users
7 7
0 5 * * * authentic2 authentic2-manage clean-unused-accounts
8 8
30 5 * * * authentic2 authentic2-manage deactivate-orphaned-ldap-users
9
0 0 * * 0 authentic2 authentic2-manage clean-user-exports
src/authentic2/management/commands/clean-user-exports.py
1
# authentic2 - versatile identity manager
2
# Copyright (C) 2010-2021 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import os
18
from datetime import datetime, timedelta
19
from shutil import rmtree
20

  
21
from django.core.files.storage import default_storage
22
from django.core.management.base import BaseCommand
23

  
24

  
25
class Command(BaseCommand):
26
    help = 'Clean old export files.'
27

  
28
    def handle(self, **options):
29
        path = default_storage.path('user_exports')
30
        for directory in os.listdir(path):
31
            dir_path = os.path.join(path, directory)
32
            modification_timestamp = os.path.getmtime(dir_path)
33
            if datetime.now() - datetime.fromtimestamp(modification_timestamp) > timedelta(days=7):
34
                rmtree(dir_path)
tests/test_commands.py
21 21

  
22 22
import py
23 23
import pytest
24
import webtest
24 25
from django.contrib.auth import get_user_model
25 26
from django.contrib.contenttypes.models import ContentType
26 27
from django.utils.timezone import now
......
389 390
    call_command('clean-unused-accounts')
390 391
    # 4 new alerts and 4 deletions notifications
391 392
    assert len(mailoutbox) == 4 + 8
393

  
394

  
395
def test_clean_user_exports(settings, app, superuser, freezer):
396
    users = [User(username='user%s' % i) for i in range(10)]
397
    User.objects.bulk_create(users)
398

  
399
    resp = login(app, superuser, '/manage/users/')
400
    resp = resp.click('CSV').follow()
401
    file_creation_time = now()
402
    assert resp.click('Download CSV')
403

  
404
    freezer.move_to(file_creation_time + datetime.timedelta(days=5))
405
    call_command('clean-user-exports')
406
    assert resp.click('Download CSV')
407

  
408
    freezer.move_to(file_creation_time + datetime.timedelta(days=8))
409
    call_command('clean-user-exports')
410
    with pytest.raises(webtest.app.AppError):
411
        resp.click('Download CSV')
392
-