Commit 77510ecd authored by jocelyn's avatar jocelyn
Browse files

Add a command to delete expired contributions

So that we do not keep data forever…
parent 8c4f4f1f
"""Delete expired contributions
Based on the expiration date that exists on any contrib. Produce no output by
default (cron stuff)
"""
from django.conf import settings
from django.core.management.base import BaseCommand
from ...models import Contrib
class Command(BaseCommand):
help = __doc__
def add_arguments(self, parser):
parser.add_argument(
'--dry-run', default=False, action='store_true',
help="Do not actually delete contributions, just print which ones should be")
def handle(self, dry_run, *args, **options):
if dry_run:
self.stderr.write('DRY RUN MODE: we do not actually delete contributions.\n')
for contrib in Contrib.objects.expired():
if not dry_run:
contrib.delete()
else:
self._log_deleted_contrib(contrib)
def _log_deleted_contrib(self, contrib):
self.stderr.write("Would delete expired contribution {}\n".format(contrib))
......@@ -411,7 +411,7 @@ class TestManagementCommands(TestCase):
longitude=0.5,
)
contrib.expiration_date = datetime.datetime(
2010, 10, 10, tzinfo=pytz.utc)
2010, 10, 10, 1, 0, tzinfo=pytz.utc)
contrib.save()
@override_settings(DATA_EXPIRATION_REMINDERS=[10])
......@@ -419,16 +419,35 @@ class TestManagementCommands(TestCase):
# 11 days before (should not send)
with freeze_time('29-09-2010', tz_offset=0):
call_command('send_expiration_reminders')
self.assertEqual(len(mail.outbox), 0)
self.assertEqual(Contrib.objects.count(), 1)
# 9 days before (should not send)
with freeze_time('01-10-2010', tz_offset=0):
call_command('send_expiration_reminders')
self.assertEqual(len(mail.outbox), 0)
self.assertEqual(Contrib.objects.count(), 1)
# 10 days before (should send)
with freeze_time('30-09-2010', tz_offset=0):
call_command('send_expiration_reminders', '--dry-run')
self.assertEqual(len(mail.outbox), 0)
self.assertEqual(Contrib.objects.count(), 1)
call_command('send_expiration_reminders')
self.assertEqual(len(mail.outbox), 1)
def test_delete_expired_contribs(self):
# 1 days before expiration
with freeze_time('09-09-2010', tz_offset=0):
call_command('delete_expired_contribs')
self.assertEqual(Contrib.objects.count(), 1)
# expiration day
with freeze_time('10-10-2010 23:59', tz_offset=0):
call_command('delete_expired_contribs', '--dry-run')
self.assertEqual(Contrib.objects.count(), 1)
call_command('delete_expired_contribs')
self.assertEqual(Contrib.objects.count(), 0)
self.setUp()
# 1 day after expiration
with freeze_time('11-10-2010', tz_offset=0):
call_command('delete_expired_contribs')
self.assertEqual(Contrib.objects.count(), 0)
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