Skip to content

Commit

Permalink
Add the sync_geonode_layers command. Fixes #4115
Browse files Browse the repository at this point in the history
  • Loading branch information
capooti committed Dec 12, 2018
1 parent 339fdaf commit 9a8f0c3
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/tutorials/admin/admin_mgmt_commands/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,52 @@ Additional options::
-h, --help show this help message and exit


sync_geonode_layers
===================

Update GeoNode layers: this command will only update layers from the GeoNode
database and will update their permissions (including GeoFence), statistics and thumbnails.
It does not use the GeoServer GetCapabilities document as updatelayers do,
therefore it can be a valid alternative to it wherever GetCapabilities is
broken for any reason.

Usage::

python manage.py sync_geonode_layers [options]

Additional options::

usage: manage.py sync_geonode_layers [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback]
[--no-color] [-i] [-f FILTER]
[-u USERNAME]

Update the GeoNode layers: permissions (including GeoFence database),
statistics, thumbnails

optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on CommandError exceptions
--no-color Don't colorize the command output.
-i, --ignore-errors Stop after any errors are encountered.
-f FILTER, --filter FILTER
Only update data the layers that match the given
filter
-u USERNAME, --username USERNAME
Only update data owned by the specified username

createvectorlayer
=================

Expand Down
96 changes: 96 additions & 0 deletions geonode/geoserver/management/commands/sync_geonode_layers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright (C) 2018 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################

import json
import sys

from django.core.management.base import BaseCommand

from geonode.layers.models import Layer
from geonode.security.views import _perms_info_json
from geonode.geoserver.helpers import set_attributes_from_geoserver


def sync_geonode_layers(ignore_errors, filter, username):
layers = Layer.objects.all().order_by('name')
if filter:
layers = layers.filter(name__icontains=filter)
if username:
layers = layers.filter(owner__username=username)
layers_count = layers.count()
count = 0
layer_errors = []
for layer in layers:
try:
count += 1
print 'Syncing layer %s/%s: %s' % (count, layers_count, layer.name)
# sync permissions in GeoFence
perm_spec = json.loads(_perms_info_json(layer))
layer.set_permissions(perm_spec)
# recalculate the layer statistics
set_attributes_from_geoserver(layer, overwrite=True)
layer.save()
except Exception:
layer_errors.append(layer.alternate)
exception_type, error, traceback = sys.exc_info()
print exception_type, error, traceback
if ignore_errors:
pass
else:
print 'Stopping process because --ignore-errors was not set and an error was found.'
return
print 'There are %s layers which could not be updated because of errors' % len(layer_errors)
for layer_error in layer_errors:
print layer_error


class Command(BaseCommand):
help = 'Update the GeoNode layers: permissions (including GeoFence database), statistics, thumbnails'

def add_arguments(self, parser):
parser.add_argument(
'-i',
'--ignore-errors',
action='store_true',
dest='ignore_errors',
default=False,
help='Stop after any errors are encountered.'
)
parser.add_argument(
'-f',
'--filter',
dest="filter",
default=None,
help="Only update data the layers that match the given filter"),
parser.add_argument(
'-u',
'--username',
dest="username",
default=None,
help="Only update data owned by the specified username")

def handle(self, **options):
ignore_errors = options.get('ignore_errors')
filter = options.get('filter')
if not options.get('username'):
username = None
else:
username = options.get('username')
sync_geonode_layers(ignore_errors, filter, username)

0 comments on commit 9a8f0c3

Please sign in to comment.