|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +######################################################################### |
| 3 | +# |
| 4 | +# Copyright (C) 2023 OSGeo |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | +######################################################################### |
| 20 | + |
| 21 | +import logging |
| 22 | + |
| 23 | +from django.core.management.base import BaseCommand |
| 24 | + |
| 25 | +from geonode.base.management import command_utils |
| 26 | +from geonode.base.models import ResourceBase |
| 27 | +from geonode.layers.models import Dataset |
| 28 | + |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +class Command(BaseCommand): |
| 34 | + help = "Re-create XML metadata documents" |
| 35 | + |
| 36 | + def add_arguments(self, parser): |
| 37 | + parser.add_argument( |
| 38 | + '-l', |
| 39 | + '--layer', |
| 40 | + dest="layers", |
| 41 | + action='append', |
| 42 | + help="Only process specified layers ") |
| 43 | + |
| 44 | + parser.add_argument( |
| 45 | + "--skip-logger-setup", |
| 46 | + action="store_false", |
| 47 | + dest="setup_logger", |
| 48 | + help='Skips setup of the "geonode.br" logger, "br" handler and "br" format if not present in settings', |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + '-d', |
| 52 | + '--dry-run', |
| 53 | + dest="dry-run", |
| 54 | + action='store_true', |
| 55 | + help="Do not actually perform any change") |
| 56 | + |
| 57 | + def handle(self, **options): |
| 58 | + requested_layers = options.get('layers') |
| 59 | + dry_run = options.get('dry-run') |
| 60 | + |
| 61 | + if options.get("setup_logger"): |
| 62 | + logger = command_utils.setup_logger() |
| 63 | + |
| 64 | + logger.info(f"==== Running command {__name__}") |
| 65 | + logger.info(f"{self.help}") |
| 66 | + logger.info("") |
| 67 | + |
| 68 | + logger.debug(f"DRY-RUN is {dry_run}") |
| 69 | + logger.debug(f"LAYERS is {requested_layers}") |
| 70 | + |
| 71 | + try: |
| 72 | + |
| 73 | + layers = Dataset.objects.all() |
| 74 | + tot = len(layers) |
| 75 | + logger.info(f"Total layers in GeoNode: {tot}") |
| 76 | + i = 0 |
| 77 | + cnt_ok = 0 |
| 78 | + cnt_bad = 0 |
| 79 | + cnt_skip = 0 |
| 80 | + |
| 81 | + instance: ResourceBase |
| 82 | + for instance in layers: |
| 83 | + i += 1 |
| 84 | + logger.info(f"- {i}/{tot} Processing layer {instance.id} [{instance.typename}] '{instance.title}'") |
| 85 | + |
| 86 | + if requested_layers and instance.typename not in requested_layers: |
| 87 | + logger.info(" - Layer filtered out by args") |
| 88 | + cnt_skip += 1 |
| 89 | + continue |
| 90 | + |
| 91 | + if instance.metadata_uploaded and instance.metadata_uploaded_preserve: |
| 92 | + logger.info(" - Layer filtered out since it uses custom XML") |
| 93 | + cnt_skip += 1 |
| 94 | + continue |
| 95 | + |
| 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 |
| 120 | + except Exception as e: |
| 121 | + raise e |
| 122 | + |
| 123 | + logger.info("Work completed" + (" [DRYRUN]" if dry_run else "")) |
| 124 | + logger.info(f"- Metadata regenerated : {cnt_ok}") |
| 125 | + logger.info(f"- Metadata in error : {cnt_bad}") |
| 126 | + logger.info(f"- Resources skipped : {cnt_skip}") |
0 commit comments