Files
zulip/zerver/management/commands/delete_old_unclaimed_attachments.py
Tim Abbott 8e7ce7cc79 python: Sort migrations/management command imports with isort.
This is a preparatory commit for using isort for sorting all of our
imports, merging changes to files where we can easily review the
changes as something we're happy with.

These are also files with relatively little active development, which
means we don't expect much merge conflict risk from these changes.
2020-01-14 13:07:47 -08:00

44 lines
1.7 KiB
Python

from argparse import ArgumentParser
from typing import Any
from django.core.management.base import BaseCommand, CommandError
from zerver.lib.actions import do_delete_old_unclaimed_attachments
from zerver.models import get_old_unclaimed_attachments
class Command(BaseCommand):
help = """Remove unclaimed attachments from storage older than a supplied
numerical value indicating the limit of how old the attachment can be.
One week is taken as the default value."""
def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument('-w', '--weeks',
dest='delta_weeks',
default=1,
help="Limiting value of how old the file can be.")
parser.add_argument('-f', '--for-real',
dest='for_real',
action='store_true',
default=False,
help="Actually remove the files from the storage.")
def handle(self, *args: Any, **options: Any) -> None:
delta_weeks = options['delta_weeks']
print("Deleting unclaimed attached files older than %s" % (delta_weeks,))
print("")
# print the list of files that are going to be removed
old_attachments = get_old_unclaimed_attachments(delta_weeks)
for old_attachment in old_attachments:
print("%s created at %s" % (old_attachment.file_name, old_attachment.create_time))
print("")
if not options["for_real"]:
raise CommandError("This was a dry run. Pass -f to actually delete.")
do_delete_old_unclaimed_attachments(delta_weeks)
print("")
print("Unclaimed Files deleted.")