Skip to content

Commit

Permalink
Add a flush command
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Mar 29, 2024
1 parent b602d9a commit cf118a2
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions timeline_logger/management/commands/prune_timeline_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from datetime import datetime
from textwrap import dedent

from django.core.management.base import BaseCommand

from timeline_logger.models import TimelineLog


class Command(BaseCommand):
help = "Removes timeline logs objects older than the specified date."

def add_arguments(self, parser):
parser.add_argument(
"--noinput",
"--no-input",
action="store_false",
dest="interactive",
help="Tells Django to NOT prompt the user for input of any kind.",
)
parser.add_argument(
"--before",
type=datetime.fromisoformat,
help="Only flush timeline logs older than the specified date.",
)

def handle(self, *args, **options):
interactive = options["interactive"]
before = options["before"]

if not before and interactive:
confirm = input(
dedent(
"""You haven't specified a date to limit the objects to be deleted.
This will delete all timeline logs objects. Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: """
)
)
else:
confirm = "yes"

if confirm == "yes":
if before:
qs = TimelineLog.objects.filter(timestamp__lte=before)
else:
qs = TimelineLog.objects.all()
number, _ = qs.delete()
self.stdout.write(f"Successfully deleted {number} timeline logs.")
else:
self.stdout.write("Flush cancelled.")

0 comments on commit cf118a2

Please sign in to comment.