Skip to content

Commit

Permalink
PB-848: Don't return expired items
Browse files Browse the repository at this point in the history
Remove expired items from list/get items endpoints.
Don't return asset details of expired items.
Asset files may still be downloaded from s3 until the item is deleted.
  • Loading branch information
benschs committed Aug 20, 2024
1 parent 820f29a commit cf3f3e4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
7 changes: 0 additions & 7 deletions app/stac_api/management/commands/remove_expired_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
from stac_api.utils import CustomBaseCommand


def boolean_input(question, default=None):
result = input(f"{question}")
if not result and default is not None:
return default
return len(result) > 0 and result[0].lower() == "y"


class Handler(CommandHandler):

def delete(self, instance, object_type):
Expand Down
8 changes: 6 additions & 2 deletions app/stac_api/validators_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

from django.db.models import Q
from django.http import Http404
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers
Expand Down Expand Up @@ -28,7 +30,7 @@ def validate_collection(kwargs):


def validate_item(kwargs):
'''Validate that the item given in request kwargs exists
'''Validate that the item given in request kwargs exists and is not expired
Args:
kwargs: dict
Expand All @@ -38,7 +40,9 @@ def validate_item(kwargs):
Http404: when the item doesn't exists
'''
if not Item.objects.filter(
name=kwargs['item_name'], collection__name=kwargs['collection_name']
Q(properties_expires=None) | Q(properties_expires__gte=timezone.now()),
name=kwargs['item_name'],
collection__name=kwargs['collection_name']
).exists():
logger.error(
"The item %s is not part of the collection %s",
Expand Down
7 changes: 7 additions & 0 deletions app/stac_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.db import transaction
from django.db.models import Min
from django.db.models import Prefetch
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from rest_framework import generics
Expand Down Expand Up @@ -364,6 +366,8 @@ class ItemsList(generics.GenericAPIView):
def get_queryset(self):
# filter based on the url
queryset = Item.objects.filter(
# filter expired items
Q(properties_expires__gte=timezone.now()) | Q(properties_expires=None),
collection__name=self.kwargs['collection_name']
).prefetch_related(Prefetch('assets', queryset=Asset.objects.order_by('name')), 'links')
bbox = self.request.query_params.get('bbox', None)
Expand Down Expand Up @@ -428,6 +432,8 @@ class ItemDetail(
def get_queryset(self):
# filter based on the url
queryset = Item.objects.filter(
# filter expired items
Q(properties_expires__gte=timezone.now()) | Q(properties_expires=None),
collection__name=self.kwargs['collection_name']
).prefetch_related(Prefetch('assets', queryset=Asset.objects.order_by('name')), 'links')

Expand Down Expand Up @@ -536,6 +542,7 @@ class AssetDetail(
def get_queryset(self):
# filter based on the url
return Asset.objects.filter(
Q(item__properties_expires=None) | Q(item__properties_expires__gte=timezone.now()),
item__collection__name=self.kwargs['collection_name'],
item__name=self.kwargs['item_name']
)
Expand Down
29 changes: 29 additions & 0 deletions app/tests/tests_10/test_assets_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
from datetime import datetime
from datetime import timedelta
from json import dumps
from json import loads
from pprint import pformat

from django.contrib.auth import get_user_model
from django.test import Client
from django.urls import reverse
from django.utils import timezone

from stac_api.models import Asset
from stac_api.utils import get_asset_path
Expand Down Expand Up @@ -88,6 +90,19 @@ def test_assets_endpoint_item_does_not_exist(self):
)
self.assertStatusCode(404, response)

def test_assets_endpoint_item_expired(self):
collection_name = self.collection.name
item_expired = self.factory.create_item_sample(
self.collection,
name='item-expired',
db_create=True,
properties_expires=timezone.now() - timedelta(hours=1)
).model
response = self.client.get(
f"/{STAC_BASE_V}/collections/{collection_name}/items/{item_expired.name}/assets"
)
self.assertStatusCode(404, response)

def test_single_asset_endpoint(self):
collection_name = self.collection.name
item_name = self.item.name
Expand All @@ -105,6 +120,20 @@ def test_single_asset_endpoint(self):
# hash computation of the ETag
self.assertEtagHeader(None, response)

def test_single_assets_endpoint_item_expired(self):
collection_name = self.collection.name
item = self.factory.create_item_sample(
self.collection,
name='item-expired',
db_create=True,
properties_expires=timezone.now() - timedelta(hours=1)
).model
asset = self.factory.create_asset_sample(item=item, db_create=True).model
response = self.client.get(
f"/{STAC_BASE_V}/collections/{collection_name}/items/{item.name}/assets/{asset.name}"
)
self.assertStatusCode(404, response)


class AssetsUnimplementedEndpointTestCase(StacBaseTestCase):

Expand Down
23 changes: 23 additions & 0 deletions app/tests/tests_10/test_items_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.auth import get_user_model
from django.test import Client
from django.urls import reverse
from django.utils import timezone

from stac_api.models import Item
from stac_api.utils import fromisoformat
Expand Down Expand Up @@ -44,6 +45,13 @@ def test_items_endpoint(self):
# To make sure that item sorting is working, make sure that the items where not
# created in ascending order, same for assets
item_3 = self.factory.create_item_sample(self.collection, name='item-0', db_create=True)
# created item that is expired should not show up in the get result
self.factory.create_item_sample(
self.collection,
name='item-expired',
db_create=True,
properties_expires=timezone.now() - timedelta(hours=1)
)
assets = self.factory.create_asset_samples(
3, item_3.model, name=['asset-1.tiff', 'asset-0.tiff', 'asset-2.tiff'], db_create=True
)
Expand Down Expand Up @@ -146,6 +154,21 @@ def test_single_item_endpoint(self):
ignore=['id', 'links']
)

def test_single_item_endpoint_expired(self):
collection_name = self.collection.name
# created item that is expired should not be found
item = self.factory.create_item_sample(
self.collection,
name='item-expired',
db_create=True,
properties_expires=timezone.now() - timedelta(hours=1)
)

response = self.client.get(
f"/{STAC_BASE_V}/collections/{collection_name}/items/{item['name']}"
)
self.assertStatusCode(404, response)

def test_items_endpoint_non_existing_collection(self):
response = self.client.get(f"/{STAC_BASE_V}/collections/non-existing-collection/items")
self.assertStatusCode(404, response)
Expand Down

0 comments on commit cf3f3e4

Please sign in to comment.