repos
/ analytics-django master

analytics-django

mirror archived upstream

Self-hostable website analytics on Django: a straightforward collector API, dashboards, a world map, and PDF reports.

analyticsdjangodockerhandcodedpythonself-hostedsqliteviteweb-analytics

844 B · 25 lines · Python Raw History
 1from django.core.management.base import BaseCommand
 2from django.utils import timezone
 3
 4from properties.models import Event
 5
 6
 7class Command(BaseCommand):
 8    help = "Delete Event rows older than --days (default 730)."
 9
10    def add_arguments(self, parser):
11        parser.add_argument("--days", type=int, default=730)
12        parser.add_argument("--dry-run", action="store_true")
13
14    def handle(self, *args, **options):
15        cutoff = timezone.now() - timezone.timedelta(days=options["days"])
16        qs = Event.objects.filter(created_at__lt=cutoff)
17        count = qs.count()
18
19        if options["dry_run"]:
20            self.stdout.write(f"Would delete {count} events older than {cutoff.isoformat()}")
21            return
22
23        deleted, _ = qs.delete()
24        self.stdout.write(f"Deleted {deleted} events older than {cutoff.isoformat()}")