Parcourir la source

Add a command to delete expired contributions

So that we do not keep data forever…
Jocelyn Delalande il y a 7 ans
Parent
commit
77510ecd7c

+ 32 - 0
wifiwithme/apps/contribmap/management/commands/delete_expired_contribs.py

@@ -0,0 +1,32 @@
+"""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))

+ 23 - 4
wifiwithme/apps/contribmap/tests.py

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