2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2019-05-03 23:20:39 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2016-03-24 20:24:01 +01:00
|
|
|
|
|
|
|
from zerver.lib.actions import do_delete_old_unclaimed_attachments
|
2017-11-02 10:19:24 +01:00
|
|
|
from zerver.models import get_old_unclaimed_attachments
|
2016-03-24 20:24:01 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2016-03-24 20:24:01 +01:00
|
|
|
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."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-w',
|
|
|
|
'--weeks',
|
|
|
|
dest='delta_weeks',
|
|
|
|
default=5,
|
|
|
|
type=int,
|
|
|
|
help="Limiting value of how old the file can be.",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-f',
|
|
|
|
'--for-real',
|
|
|
|
action='store_true',
|
|
|
|
help="Actually remove the files from the storage.",
|
|
|
|
)
|
2016-03-24 20:24:01 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2016-03-24 20:24:01 +01:00
|
|
|
delta_weeks = options['delta_weeks']
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Deleting unclaimed attached files older than {delta_weeks} weeks")
|
2016-03-24 20:24:01 +01:00
|
|
|
|
|
|
|
# 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:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"* {old_attachment.file_name} created at {old_attachment.create_time}")
|
2016-03-24 20:24:01 +01:00
|
|
|
|
|
|
|
print("")
|
|
|
|
if not options["for_real"]:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError("This was a dry run. Pass -f to actually delete.")
|
2016-03-24 20:24:01 +01:00
|
|
|
|
|
|
|
do_delete_old_unclaimed_attachments(delta_weeks)
|
|
|
|
print("")
|
2020-10-23 02:43:28 +02:00
|
|
|
print("Unclaimed files deleted.")
|