Skip to content

Commit bde9c9d

Browse files
authored
[Fixes #1381] Improve regenerate_xml
* [Fixes #1381] Improve regenerate_xml * [Fixes #1381] Improve regenerate_xml - improvs
1 parent baa10c0 commit bde9c9d

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

geonode/catalogue/management/commands/regenerate_xml.py

Lines changed: 53 additions & 33 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,56 +78,65 @@ 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

71-
try:
83+
uuid_handler_class = None
84+
if hasattr(settings, "LAYER_UUID_HANDLER") and settings.LAYER_UUID_HANDLER:
85+
from geonode.layers.utils import get_uuid_handler
86+
uuid_handler_class = get_uuid_handler()
7287

73-
layers = Dataset.objects.all()
74-
tot = len(layers)
88+
try:
89+
resources = Dataset.objects.all().order_by("id")
90+
tot = resources.count()
7591
logger.info(f"Total layers in GeoNode: {tot}")
7692
i = 0
7793
cnt_ok = 0
7894
cnt_bad = 0
7995
cnt_skip = 0
8096

8197
instance: ResourceBase
82-
for instance in layers:
98+
for instance in resources:
8399
i += 1
84-
logger.info(f"- {i}/{tot} Processing layer {instance.id} [{instance.typename}] '{instance.title}'")
100+
logger.info(f"- {i}/{tot} Processing resource {instance.id} [{instance.typename}] '{instance.title}'")
101+
102+
include_by_rl = requested_layers and instance.typename in requested_layers
103+
include_by_id = requested_ids and instance.id in requested_ids
104+
accepted = (not requested_layers and not requested_ids) or include_by_id or include_by_rl
85105

86-
if requested_layers and instance.typename not in requested_layers:
87-
logger.info(" - Layer filtered out by args")
106+
if not accepted:
107+
logger.info(" - Resource filtered out by args")
88108
cnt_skip += 1
89109
continue
90110

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

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
116+
good = None
117+
if not dry_run:
118+
try:
119+
# regenerate UUID
120+
if uuid_handler_class:
121+
_uuid = uuid_handler_class(instance).create_uuid()
122+
if _uuid != instance.uuid:
123+
logger.info(f"Replacing UUID: {instance.uuid} --> {_uuid}")
124+
instance.uuid = _uuid
125+
ResourceBase.objects.filter(id=instance.id).update(uuid=_uuid)
126+
127+
# regenerate XML
128+
catalogue_post_save(instance, None)
129+
good = True
130+
except Exception as e:
131+
logger.exception(f"Error processing '{instance.title}': {e}", e)
132+
133+
if dry_run or good:
134+
logger.info(f" - Done {instance.name}")
135+
cnt_ok += 1
136+
else:
137+
logger.warning(f"Metadata couldn't be regenerated for instance '{instance.title}' ")
138+
cnt_bad += 1
139+
120140
except Exception as e:
121141
raise e
122142

0 commit comments

Comments
 (0)