-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
timeline_logger/management/commands/prune_timeline_logs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |