From 61c7f4de259b8e29948f1a28f7958a90da818fc6 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 28 Aug 2013 23:59:06 +0200 Subject: [PATCH 1/2] Enable admin and add polygon model --- README.rst | 6 ++++-- webmap/__init__.py | 9 +++++++++ webmap/models.py | 12 ++++++++++++ webmap/settings.py | 6 ++++++ webmap/urls.py | 12 +++++++++--- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 19c1dd4..8ccee6b 100644 --- a/README.rst +++ b/README.rst @@ -14,6 +14,8 @@ Install project dependencies : python setup.py develop +For GIS edition in Adminsite, Django 1.6 is required. + On Ubuntu, I use these commands to install Spatialite : :: @@ -32,7 +34,7 @@ And its python module *pysqlite* : cd pysqlite-2.6.3 sed -i "s/define=SQLITE_OMIT_LOAD_EXTENSION//g" setup.cfg python setup.py install - cd .. + cd .. ========= @@ -57,7 +59,7 @@ Load all weather station instances from GeoJSON : python manage.py shell >>> from django.core.serializers import deserialize - >>> + >>> >>> source = open('Pub9volA130819x.geojson') >>> for ws in deserialize('geojson', source): ... ws.save() diff --git a/webmap/__init__.py b/webmap/__init__.py index e69de29..905e8f8 100644 --- a/webmap/__init__.py +++ b/webmap/__init__.py @@ -0,0 +1,9 @@ +from django.contrib import admin + +from leaflet.admin import LeafletGeoAdmin + +from .models import WeatherStation, Region + + +admin.site.register(WeatherStation, LeafletGeoAdmin) +admin.site.register(Region, LeafletGeoAdmin) diff --git a/webmap/models.py b/webmap/models.py index d7d8b2e..ef7c29c 100644 --- a/webmap/models.py +++ b/webmap/models.py @@ -13,3 +13,15 @@ class WeatherStation(gismodels.Model): def __unicode__(self): return self.name + + +class Region(gismodels.Model): + + name = models.CharField(max_length=256) + + geom = gismodels.PolygonField() + + objects = gismodels.GeoManager() + + def __unicode__(self): + return self.name diff --git a/webmap/settings.py b/webmap/settings.py index 29444cd..472dec0 100644 --- a/webmap/settings.py +++ b/webmap/settings.py @@ -7,6 +7,7 @@ SECRET_KEY = os.getenv("SECRET_KEY", 'booh') ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", '*') + STATIC_URL = '/static/' ROOT_URLCONF = 'webmap.urls' @@ -15,6 +16,11 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.gis', + 'django.contrib.admin', 'leaflet', 'djgeojson', 'webmap', diff --git a/webmap/urls.py b/webmap/urls.py index c32a4cf..8fd9ece 100644 --- a/webmap/urls.py +++ b/webmap/urls.py @@ -1,12 +1,18 @@ from django.views.generic import TemplateView -from django.conf.urls import patterns, url +from django.conf.urls import patterns, url, include +from django.contrib import admin from djgeojson.views import GeoJSONLayerView from .models import WeatherStation +admin.autodiscover() + + urlpatterns = patterns('', url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'), - url(r'^data.geojson$', GeoJSONLayerView.as_view(model=WeatherStation), name='data') -) + url(r'^data.geojson$', GeoJSONLayerView.as_view(model=WeatherStation), name='data'), + + url(r'^admin/', include(admin.site.urls)), +) \ No newline at end of file From c7e2f49bdb13d8539eb2401df97d395a62bd9d9a Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Mon, 9 Dec 2013 15:10:58 +0100 Subject: [PATCH 2/2] Add custom form for weather stations --- webmap/form.html | 15 +++++++++++++++ webmap/index.html | 8 ++++++-- webmap/urls.py | 2 ++ webmap/views.py | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 webmap/form.html create mode 100644 webmap/views.py diff --git a/webmap/form.html b/webmap/form.html new file mode 100644 index 0000000..0d66e53 --- /dev/null +++ b/webmap/form.html @@ -0,0 +1,15 @@ +{% load leaflet_tags %} + + + {% leaflet_js plugins="forms" %} + {% leaflet_css plugins="forms" %} + + +

Edit {{ object }}

+
+ {{ form }} + {% csrf_token %} + +
+ + \ No newline at end of file diff --git a/webmap/index.html b/webmap/index.html index 67ff2b2..22eecc9 100644 --- a/webmap/index.html +++ b/webmap/index.html @@ -18,10 +18,14 @@

Django-Leaflet

// Download GeoJSON via Ajax $.getJSON(dataurl, function (data) { // Add GeoJSON layer - L.geoJson(data).addTo(map); + L.geoJson(data) + .addTo(map) + .on('click', function (e) { + window.location = '{% url "form" 0 %}'.replace('0', e.layer.feature.id); + }); }); } - + diff --git a/webmap/urls.py b/webmap/urls.py index 8fd9ece..588fdeb 100644 --- a/webmap/urls.py +++ b/webmap/urls.py @@ -5,6 +5,7 @@ from djgeojson.views import GeoJSONLayerView from .models import WeatherStation +from .views import EditWeatherStation admin.autodiscover() @@ -13,6 +14,7 @@ urlpatterns = patterns('', url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'), url(r'^data.geojson$', GeoJSONLayerView.as_view(model=WeatherStation), name='data'), + url(r'^form/(?P\d+)$', EditWeatherStation.as_view(), name='form'), url(r'^admin/', include(admin.site.urls)), ) \ No newline at end of file diff --git a/webmap/views.py b/webmap/views.py new file mode 100644 index 0000000..2322697 --- /dev/null +++ b/webmap/views.py @@ -0,0 +1,21 @@ +from django import forms +from django.views.generic import UpdateView +from django.core.urlresolvers import reverse + +from .models import WeatherStation +from leaflet.forms.fields import PointField + + +class WeatherStationForm(forms.ModelForm): + geom = PointField() + class Meta: + model = WeatherStation + + +class EditWeatherStation(UpdateView): + model = WeatherStation + form_class = WeatherStationForm + template_name = 'form.html' + + def get_success_url(self): + return reverse('home')