Skip to content

Commit

Permalink
Merge pull request #566 from EOxServer/update-models
Browse files Browse the repository at this point in the history
CLI update models via replace parameter
  • Loading branch information
constantinius authored Sep 11, 2023
2 parents c7f709c + 0a7f38e commit 456f119
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 112 deletions.
31 changes: 25 additions & 6 deletions eoxserver/backends/management/commands/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def add_arguments(self, parser):
accessor will be used."""
)

create_parser.add_argument(
'--replace', action='store_true',
default=False,
help=(
'Replace storage definition if already exists.'
)
)

for parser in [list_parser, env_parser]:
parser.add_argument(
'paths', nargs='*', default=None,
Expand Down Expand Up @@ -109,7 +117,7 @@ def handle(self, subcommand, name, *args, **kwargs):
self.handle_env(name, *args, **kwargs)

def handle_create(self, name, url, type_name, parent_name,
storage_auth_name, streaming, **kwargs):
storage_auth_name, streaming, replace, **kwargs):
""" Handle the creation of a new storage.
"""
url = url[0]
Expand Down Expand Up @@ -144,11 +152,22 @@ def handle_create(self, name, url, type_name, parent_name,
)
else:
storage_auth = None

backends.Storage.objects.create(
name=name, url=url, storage_type=type_name, parent=parent,
storage_auth=storage_auth, streaming=streaming,
)
if replace:
backends.Storage.objects.update_or_create(
name=name,
defaults={
'url':url,
'storage_type':type_name,
'parent':parent,
'storage_auth':storage_auth,
'streaming':streaming,
},
)
else:
backends.Storage.objects.create(
storage_type=type_name, parent=parent,
storage_auth=storage_auth, streaming=streaming,
)

self.print_msg(
'Successfully created storage %s (%s)' % (
Expand Down
37 changes: 27 additions & 10 deletions eoxserver/backends/management/commands/storageauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def add_arguments(self, parser):
help='Check access to the storage auth.',
)

create_parser.add_argument(
'--replace', action='store_true',
default=False,
help=(
'Replace storage auth definition if already exists.'
)
)

@transaction.atomic
def handle(self, subcommand, name, *args, **kwargs):
""" Dispatch sub-commands: create, delete, insert, exclude, purge.
Expand All @@ -84,7 +92,7 @@ def handle(self, subcommand, name, *args, **kwargs):
elif subcommand == "delete":
self.handle_delete(name, *args, **kwargs)

def handle_create(self, name, url, type_name, parameters, check, **kwargs):
def handle_create(self, name, url, type_name, parameters, check, replace, **kwargs):
""" Handle the creation of a new storage.
"""
url = url[0]
Expand All @@ -102,15 +110,24 @@ def parse_parameter(key, value=None, *extra):
parse_parameter(*param)
for param in parameters
)

storage_auth = backends.StorageAuth(
name=name,
url=url,
storage_auth_type=type_name,
auth_parameters=json.dumps(parameters),
)
storage_auth.full_clean()
storage_auth.save()
if replace:
backends.StorageAuth.objects.update_or_create(
name=name,
defaults={
'url':url,
'storage_auth_type':type_name,
'auth_parameters':json.dumps(parameters),
},
)
else:
storage_auth = backends.StorageAuth(
name=name,
url=url,
storage_auth_type=type_name,
auth_parameters=json.dumps(parameters),
)
storage_auth.full_clean()
storage_auth.save()

if check:
_ = get_handler_for_model(storage_auth)
Expand Down
76 changes: 54 additions & 22 deletions eoxserver/resources/coverages/management/commands/browsetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def add_arguments(self, parser):
action="store_true",
default=False,
)
create_parser.add_argument(
'--replace', action='store_true',
default=False,
help=(
'''Change browse type if browse type already exists.'''
)
)


list_parser.add_argument(
Expand Down Expand Up @@ -141,6 +148,7 @@ def handle_create(self, product_type_name, browse_type_name,
red_or_grey_nodata=None, green_nodata=None,
blue_nodata=None, alpha_nodata=None,
show_out_of_bounds_data=False,
replace=False,
*args, **kwargs):
""" Handle the creation of a new browse type.
"""
Expand All @@ -158,28 +166,52 @@ def handle_create(self, product_type_name, browse_type_name,
green_min, green_max = green_range
blue_min, blue_max = blue_range
alpha_min, alpha_max = alpha_range

models.BrowseType.objects.create(
product_type=product_type,
name=browse_type_name,
red_or_grey_expression=red_or_grey_expression,
green_expression=green_expression,
blue_expression=blue_expression,
alpha_expression=alpha_expression,
red_or_grey_range_min=red_min,
red_or_grey_range_max=red_max,
green_range_min=green_min,
green_range_max=green_max,
blue_range_min=blue_min,
blue_range_max=blue_max,
alpha_range_min=alpha_min,
alpha_range_max=alpha_max,
red_or_grey_nodata_value=red_or_grey_nodata,
green_nodata_value=green_nodata,
blue_nodata_value=blue_nodata,
alpha_nodata_value=alpha_nodata,
show_out_of_bounds_data=show_out_of_bounds_data,
)
if replace:
models.BrowseType.objects.update_or_create(
product_type=product_type,
name=browse_type_name,
defaults={
'red_or_grey_expression':red_or_grey_expression,
'green_expression':green_expression,
'blue_expression':blue_expression,
'alpha_expression':alpha_expression,
'red_or_grey_range_min':red_min,
'red_or_grey_range_max':red_max,
'green_range_min':green_min,
'green_range_max':green_max,
'blue_range_min':blue_min,
'blue_range_max':blue_max,
'alpha_range_min':alpha_min,
'alpha_range_max':alpha_max,
'red_or_grey_nodata_value':red_or_grey_nodata,
'green_nodata_value':green_nodata,
'blue_nodata_value':blue_nodata,
'alpha_nodata_value':alpha_nodata,
'show_out_of_bounds_data':show_out_of_bounds_data,
},
)
else:
models.BrowseType.objects.create(
product_type=product_type,
name=browse_type_name,
red_or_grey_expression=red_or_grey_expression,
green_expression=green_expression,
blue_expression=blue_expression,
alpha_expression=alpha_expression,
red_or_grey_range_min=red_min,
red_or_grey_range_max=red_max,
green_range_min=green_min,
green_range_max=green_max,
blue_range_min=blue_min,
blue_range_max=blue_max,
alpha_range_min=alpha_min,
alpha_range_max=alpha_max,
red_or_grey_nodata_value=red_or_grey_nodata,
green_nodata_value=green_nodata,
blue_nodata_value=blue_nodata,
alpha_nodata_value=alpha_nodata,
show_out_of_bounds_data=show_out_of_bounds_data,
)

if not browse_type_name:
print(
Expand Down
27 changes: 21 additions & 6 deletions eoxserver/resources/coverages/management/commands/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def add_arguments(self, parser):
'"platform".'
)
)
create_parser.add_argument(
'--replace', action='store_true',
default=False,
help=(
'''Change collection type references according to parameters
if collection type already exists.'''
)
)
delete_parser.add_argument(
'--all', '-a', action="store_true",
default=False, dest='all_collections',
Expand Down Expand Up @@ -155,7 +163,7 @@ def handle(self, subcommand, identifier, *args, **kwargs):
elif subcommand == "summary":
self.handle_summary(identifier[0], *args, **kwargs)

def handle_create(self, identifier, type_name, grid_name, **kwargs):
def handle_create(self, identifier, type_name, grid_name, replace, **kwargs):
""" Handle the creation of a new collection.
"""
if grid_name:
Expand All @@ -176,11 +184,18 @@ def handle_create(self, identifier, type_name, grid_name, **kwargs):
raise CommandError(
"Collection type %r does not exist." % type_name
)

models.Collection.objects.create(
identifier=identifier,
collection_type=collection_type, grid=grid
)
if replace:
models.Collection.objects.update_or_create(
identifier=identifier,
defaults={
'collection_type':collection_type,
'grid':grid,
}
)
else:
models.Collection.objects.create(
identifier=identifier, collection_type=collection_type, grid=grid
)

print('Successfully created collection %r' % identifier)

Expand Down
88 changes: 63 additions & 25 deletions eoxserver/resources/coverages/management/commands/collectiontype.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def add_arguments(self, parser):
)
)

create_parser.add_argument(
'--replace', action='store_true',
default=False,
help=(
'''Change collection type references according to parameters
if collection type already exists.'''
)
)

delete_parser.add_argument(
'--force', '-f', action='store_true', default=False,
help='Also remove all collections associated with that type.'
Expand All @@ -88,37 +97,39 @@ def handle(self, subcommand, *args, **kwargs):
self.handle_list(*args, **kwargs)

def handle_create(self, name, allowed_coverage_type_names,
allowed_product_type_names, **kwargs):
allowed_product_type_names, replace, **kwargs):
""" Handle the creation of a new collection type.
"""

collection_type = models.CollectionType.objects.create(name=name)
if replace:
collection_type = models.CollectionType.objects.get_or_create(name=name)[0]
else:
collection_type = models.CollectionType.objects.create(name=name)

for allowed_coverage_type_name in allowed_coverage_type_names:
try:
collection_type.allowed_coverage_types.add(
models.CoverageType.objects.get(
name=allowed_coverage_type_name
)
)
except models.CoverageType.DoesNotExist:
raise CommandError(
'Coverage type %r does not exist.' %
allowed_coverage_type_name
)
if replace:
if not collection_type.allowed_coverage_types.filter(name=allowed_coverage_type_name).exists():
self.add_allowed_coverage_type_name(collection_type, allowed_coverage_type_name)
else:
self.add_allowed_coverage_type_name(collection_type, allowed_coverage_type_name)
if replace:
# remove allowed coverage types not part of definition of collection type
referenced_coverage_types = collection_type.allowed_coverage_types.all()
for ct in referenced_coverage_types:
if ct.name not in allowed_coverage_type_names:
collection_type.allowed_coverage_types.remove(ct)

for allowed_product_type_name in allowed_product_type_names:
try:
collection_type.allowed_product_types.add(
models.ProductType.objects.get(
name=allowed_product_type_name
)
)
except models.ProductType.DoesNotExist:
raise CommandError(
'Product type %r does not exist.' %
allowed_product_type_name
)
if replace:
if not collection_type.allowed_product_types.filter(name=allowed_product_type_name).exists():
self.add_allowed_product_type_name(collection_type, allowed_product_type_name)
else:
self.add_allowed_product_type_name(collection_type, allowed_product_type_name)
if replace:
# remove allowed product types not part of definition of collection type
referenced_product_types = collection_type.allowed_product_types.all()
for pt in referenced_product_types:
if pt.name not in allowed_product_type_names:
collection_type.allowed_product_types.remove(pt)

print('Successfully created collection type %r' % name)

Expand All @@ -139,3 +150,30 @@ def handle_list(self, detail, *args, **kwargs):
# if detail:
# for coverage_type in collection_type.allowed_coverage_types.all():
# print("\t%s" % coverage_type.name)

def add_allowed_coverage_type_name(self, collection_type, allowed_coverage_type_name):
try:
collection_type.allowed_coverage_types.add(
models.CoverageType.objects.get(
name=allowed_coverage_type_name
)
)
except models.CoverageType.DoesNotExist:
raise CommandError(
'Coverage type %r does not exist.' %
allowed_coverage_type_name
)


def add_allowed_product_type_name(self, collection_type, allowed_product_type_name):
try:
collection_type.allowed_product_types.add(
models.ProductType.objects.get(
name=allowed_product_type_name
)
)
except models.CoverageType.DoesNotExist:
raise CommandError(
'Product type %r does not exist.' %
allowed_product_type_name
)
Loading

0 comments on commit 456f119

Please sign in to comment.