automatically create pages for all places (of a kind)

This commit is contained in:
Frédéric Péters 2017-08-21 11:43:42 +02:00
parent a447717fb0
commit b7e87a4e00
3 changed files with 61 additions and 0 deletions

View File

View File

@ -0,0 +1,61 @@
# combo-plugin-gnm - Combo GNM plugin
# Copyright (C) 2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import requests
from django.conf import settings
from django.core.management.base import BaseCommand
from combo.apps.maps.models import MapLayer
from combo.data.models import Page, ConfigJsonCell
class Command(BaseCommand):
def handle(self, *args, **options):
layers = ['mdr']
places_page = Page.objects.get(slug='lieux')
for layer in MapLayer.objects.filter(slug__in=layers):
cell_form_keys = [x['varname'] for x in settings.JSON_CELL_TYPES[layer.slug].get('form')]
parent_page = Page.objects.get_or_create(
parent__id=places_page.id,
slug=layer.slug,
defaults={
'title': layer.label,
'redirect_url': '..',
'parent': places_page,
})
for feature in layer.get_geojson():
cell_parameters = dict([
(x, feature['properties'][x]) for x in feature['properties'] if x in cell_form_keys])
try:
cell = ConfigJsonCell.objects.get(
key=layer.slug,
parameters=cell_parameters,
page__template_name='place')
except ConfigJsonCell.DoesNotExist:
page = Page()
page.title = feature['properties']['nom']
page.parent = Page.objects.get(slug=layer.slug)
page.template_name = 'place'
page.save()
cell = ConfigJsonCell()
cell.order = 0
cell.placeholder = 'content'
cell.key = layer.slug
cell.parameters = cell_parameters
cell.page = page
cell.save()