Verified Commit 92b9696a authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add command to minimise registrations data

parent 1bafbc2b
registrations.management.commands package
=========================================
.. automodule:: registrations.management.commands
:members:
:undoc-members:
:show-inheritance:
Submodules
----------
registrations.management.commands.minimiseregistrations module
--------------------------------------------------------------
.. automodule:: registrations.management.commands.minimiseregistrations
:members:
:undoc-members:
:show-inheritance:
registrations.management package
================================
.. automodule:: registrations.management
:members:
:undoc-members:
:show-inheritance:
Subpackages
-----------
.. toctree::
registrations.management.commands
......@@ -6,6 +6,13 @@ registrations package
:undoc-members:
:show-inheritance:
Subpackages
-----------
.. toctree::
registrations.management
Submodules
----------
......
from django.core.management import BaseCommand
from registrations import services
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
'--dry-run',
action='store_true',
dest='dry-run',
default=False,
help='Dry run instead of saving data',
)
def handle(self, *args, **options):
services.execute_data_minimisation(options['dry-run'])
......@@ -415,3 +415,22 @@ def process_payment(payment):
entry.membership = membership
entry.status = Entry.STATUS_COMPLETED
entry.save()
def execute_data_minimisation(dry_run=False):
"""
Delete completed or rejected registrations that were modified
at least 31 days ago
:param dry_run: does not really remove data if True
:return: number of removed registrations
"""
deletion_period = timezone.now().date() - timezone.timedelta(days=31)
objects = (Entry.objects
.filter((Q(status=Entry.STATUS_COMPLETED)
| Q(status=Entry.STATUS_REJECTED))
& Q(updated_at__lt=deletion_period)))
if dry_run:
return objects.count()
return objects.delete()[0]
from unittest import mock
from unittest.mock import MagicMock
from django.test import TestCase
from registrations.management.commands.minimiseregistrations import Command
class ManagementMinimiseTest(TestCase):
def test_add_argument(self):
mockParser = MagicMock()
Command().add_arguments(mockParser)
mockParser.add_argument.assert_called_with(
'--dry-run',
action='store_true',
default=False,
dest='dry-run',
help='Dry run instead of saving data'
)
@mock.patch('registrations.services.execute_data_minimisation')
def test_handle(self, execute_data_minimisation):
Command().handle({}, **{'dry-run': False})
execute_data_minimisation.assert_called_with(False)
Command().handle({}, **{'dry-run': True})
execute_data_minimisation.assert_called_with(True)
......@@ -569,3 +569,39 @@ class ServicesTest(TestCase):
for payment in Payment.objects.filter(pk__in=[p1.pk, p2.pk]):
services.process_payment(payment)
self.assertFalse(create_membership.called)
@freeze_time('2019-01-01')
def test_execute_data_minimisation(self):
with self.subTest('No processed entries'):
self.assertEqual(services.execute_data_minimisation(), 0)
with freeze_time('2018-09-01'):
self.e0.status = Entry.STATUS_COMPLETED
self.e0.save()
with self.subTest('Has processed entries when completed'):
self.assertEqual(services.execute_data_minimisation(), 1)
with freeze_time('2018-09-01'):
self.e0.status = Entry.STATUS_REJECTED
self.e0.save()
with self.subTest('Has processed entries when rejected'):
self.assertEqual(services.execute_data_minimisation(), 1)
with freeze_time('2018-09-01'):
self.e0.status = Entry.STATUS_COMPLETED
self.e0.save()
with self.subTest('Has processed entries when '
'rejected with dry-run'):
self.assertEqual(services.execute_data_minimisation(True), 1)
self.e0.status = Entry.STATUS_COMPLETED
self.e0.save()
with self.subTest('No processed entries when inside 31 days'):
self.assertEqual(services.execute_data_minimisation(), 0)
self.e0.status = Entry.STATUS_REVIEW
self.e0.save()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment