Skip to content

Commit 2f18d1c

Browse files
committed
[Fixes #1381] Improve regenerate_xml
1 parent afca096 commit 2f18d1c

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

geonode/catalogue/management/commands/regenerate_xml.py

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import logging
2222

23+
from django.conf import settings
2324
from django.core.management.base import BaseCommand
2425

2526
from geonode.base.management import command_utils
2627
from geonode.base.models import ResourceBase
28+
from geonode.catalogue.models import catalogue_post_save
2729
from geonode.layers.models import Dataset
2830

2931

@@ -39,7 +41,15 @@ def add_arguments(self, parser):
3941
'--layer',
4042
dest="layers",
4143
action='append',
42-
help="Only process specified layers ")
44+
help="Only process layers with specified name")
45+
46+
parser.add_argument(
47+
'-i',
48+
'--id',
49+
dest="ids",
50+
type=int,
51+
action='append',
52+
help="Only process resources with specified id")
4353

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

5767
def handle(self, **options):
5868
requested_layers = options.get('layers')
69+
requested_ids = options.get('ids')
5970
dry_run = options.get('dry-run')
6071

6172
if options.get("setup_logger"):
@@ -67,10 +78,10 @@ def handle(self, **options):
6778

6879
logger.debug(f"DRY-RUN is {dry_run}")
6980
logger.debug(f"LAYERS is {requested_layers}")
81+
logger.debug(f"IDS is {requested_ids}")
7082

7183
try:
72-
73-
layers = Dataset.objects.all()
84+
layers = Dataset.objects.all().order_by("id")
7485
tot = len(layers)
7586
logger.info(f"Total layers in GeoNode: {tot}")
7687
i = 0
@@ -83,7 +94,11 @@ def handle(self, **options):
8394
i += 1
8495
logger.info(f"- {i}/{tot} Processing layer {instance.id} [{instance.typename}] '{instance.title}'")
8596

86-
if requested_layers and instance.typename not in requested_layers:
97+
include_by_rl = requested_layers and instance.typename in requested_layers
98+
include_by_id = requested_ids and instance.id in requested_ids
99+
accepted = (not requested_layers and not requested_ids) or include_by_id or include_by_rl
100+
101+
if not accepted:
87102
logger.info(" - Layer filtered out by args")
88103
cnt_skip += 1
89104
continue
@@ -93,30 +108,31 @@ def handle(self, **options):
93108
cnt_skip += 1
94109
continue
95110

96-
try:
97-
good = None
98-
if not dry_run:
99-
try:
100-
try:
101-
# the save() method triggers the metadata regeneration
102-
instance.save()
103-
good = True
104-
except Exception as e:
105-
logger.error(f"Error saving instance '{instance.title}': {e}")
106-
raise e
107-
108-
except Exception as e:
109-
logger.exception(f"Error processing '{instance.title}': {e}", e)
110-
111-
if dry_run or good:
112-
logger.info(f" - Done {instance.name}")
113-
cnt_ok += 1
114-
else:
115-
logger.warning(f"Metadata couldn't be regenerated for instance '{instance.title}' ")
116-
cnt_bad += 1
117-
118-
except Exception as e:
119-
raise e
111+
good = None
112+
if not dry_run:
113+
try:
114+
# regenerate UUID
115+
if hasattr(settings, "LAYER_UUID_HANDLER") and settings.LAYER_UUID_HANDLER:
116+
from geonode.layers.utils import get_uuid_handler
117+
_uuid = get_uuid_handler()(instance).create_uuid()
118+
if _uuid != instance.uuid:
119+
logger.info(f"Replacing UUID: {instance.uuid} --> {_uuid}")
120+
instance.uuid = _uuid
121+
ResourceBase.objects.filter(id=instance.id).update(uuid=_uuid)
122+
123+
# regenerate XML
124+
catalogue_post_save(instance, None)
125+
good = True
126+
except Exception as e:
127+
logger.exception(f"Error processing '{instance.title}': {e}", e)
128+
129+
if dry_run or good:
130+
logger.info(f" - Done {instance.name}")
131+
cnt_ok += 1
132+
else:
133+
logger.warning(f"Metadata couldn't be regenerated for instance '{instance.title}' ")
134+
cnt_bad += 1
135+
120136
except Exception as e:
121137
raise e
122138

0 commit comments

Comments
 (0)