Skip to content

Commit

Permalink
PB-848: small cleanup after review
Browse files Browse the repository at this point in the history
  • Loading branch information
benschs committed Aug 20, 2024
1 parent cf3f3e4 commit a3a6e64
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions app/stac_api/management/commands/remove_expired_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.core.management.base import CommandParser
from django.utils import timezone

from stac_api.models import Asset
from stac_api.models import Item
from stac_api.utils import CommandHandler
from stac_api.utils import CustomBaseCommand
Expand All @@ -19,22 +18,21 @@ def delete(self, instance, object_type):
instance.delete()

def run(self):
# print(self.options)
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 i in items:
assets = Asset.objects.filter(item_id=i.id).all()
for item in items:
assets = item.assets.all()
assets_length = len(assets)
self.delete(assets, 'assets')
self.delete(i, 'item')
self.delete(item, 'item')
if not self.options['dry_run']:
self.print_success(
f"deleted item {i.name} and {assets_length}" + " assets belonging to it.",
extra={"item": i.name}
f"deleted item {item.name} and {assets_length}" + " assets belonging to it.",
extra={"item": item.name}
)

if self.options['dry_run']:
Expand Down

0 comments on commit a3a6e64

Please sign in to comment.