Skip to content

Commit

Permalink
[Hardening] Check for duplicates links on 'set_all_layers_metadata' m…
Browse files Browse the repository at this point in the history
…anagement command
  • Loading branch information
root committed Sep 11, 2019
1 parent 7284c65 commit 31e968b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions geonode/base/management/commands/set_all_layers_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#########################################################################

from django.core.management.base import BaseCommand
from geonode.base.models import Link
from geonode.layers.models import Layer
from geonode.catalogue.models import catalogue_post_save

Expand All @@ -30,6 +31,10 @@
elif check_ogc_backend(qgis_server.BACKEND_PACKAGE):
from geonode.qgis_server.gis_tools import set_attributes

_names = ['Zipped', 'Shapefile', 'GML 2.0', 'GML 3.1.1', 'CSV', 'GeoJSON', 'Excel', 'Legend',
'GeoTIFF', 'GZIP', 'Original Dataset', 'ESRI Shapefile', 'View in Google Earth',
'KML', 'KMZ']


class Command(BaseCommand):
help = 'Resets Metadata Attributes and Schema to All Layers'
Expand All @@ -43,6 +48,14 @@ def add_arguments(self, parser):
default=False,
help='Stop after any errors are encountered.'
)
parser.add_argument(
'-d',
'--remove-duplicates',
action='store_true',
dest='remove_duplicates',
default=True,
help='Remove duplicates first.'
)
parser.add_argument(
'-f',
'--filter',
Expand All @@ -58,6 +71,7 @@ def add_arguments(self, parser):

def handle(self, *args, **options):
ignore_errors = options.get('ignore_errors')
remove_duplicates = options.get('remove_duplicates')
filter = options.get('filter')
if not options.get('username'):
username = None
Expand All @@ -79,6 +93,14 @@ def handle(self, *args, **options):
# refresh metadata links
set_resource_default_links(layer, layer, prune=False)

if remove_duplicates:
# remove duplicates
for _n in _names:
_links = Link.objects.filter(resource__id=layer.id, name__icontains=_n)
while _links.count() > 1:
_links.last().delete()
print '.',

# refresh catalogue metadata records
catalogue_post_save(instance=layer, sender=layer.__class__)
except BaseException as e:
Expand All @@ -88,3 +110,4 @@ def handle(self, *args, **options):
print "[ERROR] Layer [%s] couldn't be updated" % (layer.name)
else:
raise e

0 comments on commit 31e968b

Please sign in to comment.