Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 53 additions & 33 deletions geonode/catalogue/management/commands/regenerate_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

import logging

from django.conf import settings
from django.core.management.base import BaseCommand

from geonode.base.management import command_utils
from geonode.base.models import ResourceBase
from geonode.catalogue.models import catalogue_post_save
from geonode.layers.models import Dataset


Expand All @@ -39,7 +41,15 @@ def add_arguments(self, parser):
'--layer',
dest="layers",
action='append',
help="Only process specified layers ")
help="Only process layers with specified name")

parser.add_argument(
'-i',
'--id',
dest="ids",
type=int,
action='append',
help="Only process resources with specified id")

parser.add_argument(
"--skip-logger-setup",
Expand All @@ -56,6 +66,7 @@ def add_arguments(self, parser):

def handle(self, **options):
requested_layers = options.get('layers')
requested_ids = options.get('ids')
dry_run = options.get('dry-run')

if options.get("setup_logger"):
Expand All @@ -67,56 +78,65 @@ def handle(self, **options):

logger.debug(f"DRY-RUN is {dry_run}")
logger.debug(f"LAYERS is {requested_layers}")
logger.debug(f"IDS is {requested_ids}")

try:
uuid_handler_class = None
if hasattr(settings, "LAYER_UUID_HANDLER") and settings.LAYER_UUID_HANDLER:
from geonode.layers.utils import get_uuid_handler
uuid_handler_class = get_uuid_handler()

layers = Dataset.objects.all()
tot = len(layers)
try:
resources = Dataset.objects.all().order_by("id")
tot = resources.count()
logger.info(f"Total layers in GeoNode: {tot}")
i = 0
cnt_ok = 0
cnt_bad = 0
cnt_skip = 0

instance: ResourceBase
for instance in layers:
for instance in resources:
i += 1
logger.info(f"- {i}/{tot} Processing layer {instance.id} [{instance.typename}] '{instance.title}'")
logger.info(f"- {i}/{tot} Processing resource {instance.id} [{instance.typename}] '{instance.title}'")

include_by_rl = requested_layers and instance.typename in requested_layers
include_by_id = requested_ids and instance.id in requested_ids
accepted = (not requested_layers and not requested_ids) or include_by_id or include_by_rl

if requested_layers and instance.typename not in requested_layers:
logger.info(" - Layer filtered out by args")
if not accepted:
logger.info(" - Resource filtered out by args")
cnt_skip += 1
continue

if instance.metadata_uploaded and instance.metadata_uploaded_preserve:
logger.info(" - Layer filtered out since it uses custom XML")
logger.info(" - Resource filtered out since it uses custom XML")
cnt_skip += 1
continue

try:
good = None
if not dry_run:
try:
try:
# the save() method triggers the metadata regeneration
instance.save()
good = True
except Exception as e:
logger.error(f"Error saving instance '{instance.title}': {e}")
raise e

except Exception as e:
logger.exception(f"Error processing '{instance.title}': {e}", e)

if dry_run or good:
logger.info(f" - Done {instance.name}")
cnt_ok += 1
else:
logger.warning(f"Metadata couldn't be regenerated for instance '{instance.title}' ")
cnt_bad += 1

except Exception as e:
raise e
good = None
if not dry_run:
try:
# regenerate UUID
if uuid_handler_class:
_uuid = uuid_handler_class(instance).create_uuid()
if _uuid != instance.uuid:
logger.info(f"Replacing UUID: {instance.uuid} --> {_uuid}")
instance.uuid = _uuid
ResourceBase.objects.filter(id=instance.id).update(uuid=_uuid)

# regenerate XML
catalogue_post_save(instance, None)
good = True
except Exception as e:
logger.exception(f"Error processing '{instance.title}': {e}", e)

if dry_run or good:
logger.info(f" - Done {instance.name}")
cnt_ok += 1
else:
logger.warning(f"Metadata couldn't be regenerated for instance '{instance.title}' ")
cnt_bad += 1

except Exception as e:
raise e

Expand Down