-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #457 from geoadmin/develop
New Release v1.26.0 - #minor
- Loading branch information
Showing
20 changed files
with
1,306 additions
and
69 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,66 @@ | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.core.management.base import CommandParser | ||
from django.utils import timezone | ||
|
||
from stac_api.models import Item | ||
from stac_api.utils import CommandHandler | ||
from stac_api.utils import CustomBaseCommand | ||
|
||
|
||
class Handler(CommandHandler): | ||
|
||
def delete(self, instance, object_type): | ||
if self.options['dry_run']: | ||
self.print_success(f'skipping deletion of {object_type} {instance}') | ||
else: | ||
instance.delete() | ||
|
||
def run(self): | ||
self.print_success('running command to remove expired items') | ||
min_age_hours = self.options['min_age_hours'] | ||
self.print_warning(f"deleting all items expired longer than {min_age_hours} hours") | ||
items = Item.objects.filter( | ||
properties_expires__lte=timezone.now() - timedelta(hours=min_age_hours) | ||
).all() | ||
for item in items: | ||
assets = item.assets.all() | ||
assets_length = len(assets) | ||
self.delete(assets, 'assets') | ||
self.delete(item, 'item') | ||
if not self.options['dry_run']: | ||
self.print_success( | ||
f"deleted item {item.name} and {assets_length}" + " assets belonging to it.", | ||
extra={"item": item.name} | ||
) | ||
|
||
if self.options['dry_run']: | ||
self.print_success(f'[dry run] would have removed {len(items)} expired items') | ||
else: | ||
self.print_success(f'successfully removed {len(items)} expired items') | ||
|
||
|
||
class Command(CustomBaseCommand): | ||
help = """Remove items and their assets that have expired more than | ||
DELETE_EXPIRED_ITEMS_OLDER_THAN_HOURS hours ago. | ||
This command is thought to be scheduled as cron job. | ||
""" | ||
|
||
def add_arguments(self, parser: CommandParser) -> None: | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'--dry-run', | ||
action='store_true', | ||
help='Simulate deleting items, without actually deleting them' | ||
) | ||
default_min_age = settings.DELETE_EXPIRED_ITEMS_OLDER_THAN_HOURS | ||
parser.add_argument( | ||
'--min-age-hours', | ||
type=int, | ||
default=default_min_age, | ||
help=f"Minimum hours the item must have been expired for (default {default_min_age})" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
Handler(self, options).run() |
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
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
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
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
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
Oops, something went wrong.