combo-plugin-gnm/combo_plugin_gnm/management/commands/gnm_create_places.py

72 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
# 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', 'piscine', 'mairie', 'bibliotheque', 'decheterie', 'donnerie']
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, created = Page.objects.get_or_create(
parent__id=places_page.id,
slug=layer.slug,
defaults={
'title': layer.label,
'redirect_url': '..',
'parent': places_page,
})
parent_page.title = layer.label
parent_page.save()
for feature in layer.get_geojson(request=None):
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()
if layer.slug == 'decheterie':
page.title = u'Déchèterie %s' % feature['properties']['nom']
elif layer.slug == 'donnerie':
page.title = u'Donnerie %s' % feature['properties']['nom']
elif layer.slug == 'taxi':
page.title = u'Station de taxi %s' % feature['properties']['adresse']
else:
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()