-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the sync_geonode_layers command. Fixes #4115
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
geonode/geoserver/management/commands/sync_geonode_layers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |