Skip to content

Commit

Permalink
feat(collection): add import subcommand for collection command
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinius committed Feb 21, 2025
1 parent 1e6d5ec commit 48636b7
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions eoxserver/resources/coverages/management/commands/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ def add_arguments(self, parser):
exclude_parser = self.add_subparser(parser, 'exclude')
purge_parser = self.add_subparser(parser, 'purge')
summary_parser = self.add_subparser(parser, 'summary')
import_parser = self.add_subparser(parser, 'import')
parsers = [
create_parser, insert_parser, exclude_parser,
purge_parser, summary_parser
purge_parser, summary_parser, import_parser
]

# identifier is a common argument (except for delete it is optional,
Expand Down Expand Up @@ -91,8 +92,8 @@ def add_arguments(self, parser):
)

delete_parser.add_argument(
'identifier', default=None, nargs='?',
help='The identifier of the collection to delete.'
'identifier', default=None, nargs='?',
help='The identifier of the collection to delete.'
)
# common arguments for insertion/exclusion
insert_parser.add_argument(
Expand Down Expand Up @@ -131,13 +132,13 @@ def add_arguments(self, parser):
)
summary_parser.add_argument(
'--no-products', action='store_false', default=True,
dest='coverage_summary',
dest='product_summary',
help=("Don't collect summary product metadata.")
)

summary_parser.add_argument(
'--coverages', action='store_true', default=True,
dest='product_summary',
dest='coverage_summary',
help=('Collect summary coverage metadata. Default.')
)
summary_parser.add_argument(
Expand All @@ -146,6 +147,32 @@ def add_arguments(self, parser):
help=("Don't collect summary coverage metadata.")
)

import_parser.add_argument(
'--from', action='append', default=[], dest="from_collections",
help=("Declare to import from that collection")
)
import_parser.add_argument(
'--products', action='store_true', default=True,
dest='product_import',
help=('Import products. Default.')
)
import_parser.add_argument(
'--no-products', action='store_false', default=True,
dest='product_import',
help=("Don't import products.")
)

import_parser.add_argument(
'--coverages', action='store_true', default=True,
dest='coverage_import',
help=('Import coverages. Default.')
)
import_parser.add_argument(
'--no-coverages', action='store_false', default=True,
dest='coverage_import',
help=("Don't import coverages.")
)

@transaction.atomic
def handle(self, subcommand, identifier, *args, **kwargs):
""" Dispatch sub-commands: create, delete, insert, exclude, purge.
Expand All @@ -162,6 +189,8 @@ def handle(self, subcommand, identifier, *args, **kwargs):
self.handle_purge(identifier[0], *args, **kwargs)
elif subcommand == "summary":
self.handle_summary(identifier[0], *args, **kwargs)
elif subcommand == "import":
self.handle_import(identifier[0], *args, **kwargs)

def handle_create(self, identifier, type_name, grid_name, replace, **kwargs):
""" Handle the creation of a new collection.
Expand All @@ -188,8 +217,8 @@ def handle_create(self, identifier, type_name, grid_name, replace, **kwargs):
models.Collection.objects.update_or_create(
identifier=identifier,
defaults={
'collection_type':collection_type,
'grid':grid,
'collection_type': collection_type,
'grid': grid,
}
)
else:
Expand Down Expand Up @@ -308,6 +337,23 @@ def handle_summary(self, identifier, product_summary, coverage_summary,
)
print('Successfully collected metadata for collection %r' % identifier)

def handle_import(self, identifier, from_collections, product_import,
coverage_import, **kwargs):
collection = self.get_collection(identifier)

collections = [
self.get_collection(from_collection)
for from_collection in from_collections
]

if product_import:
for product in models.Product.objects.filter(collections__in=collections):
models.collection_insert_eo_object(collection, product)

if coverage_import:
for coverage in models.Coverage.objects.filter(collections__in=collections):
models.collection_insert_eo_object(collection, coverage)

def get_collection(self, identifier):
""" Helper method to get a collection by identifier or raise a
CommandError.
Expand Down

0 comments on commit 48636b7

Please sign in to comment.