Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit geometries in admin and in custom form #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 :

::
Expand All @@ -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 ..


=========
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions webmap/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions webmap/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% load leaflet_tags %}
<html>
<head>
{% leaflet_js plugins="forms" %}
{% leaflet_css plugins="forms" %}
</head>
<body>
<h1>Edit {{ object }}</h1>
<form method="POST">
{{ form }}
{% csrf_token %}
<input type="submit"/>
</form>
</body>
</html>
8 changes: 6 additions & 2 deletions webmap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ <h1>Django-Leaflet</h1>
// 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);
});
});
}
</script>

</body>
</html>
12 changes: 12 additions & 0 deletions webmap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions webmap/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SECRET_KEY = os.getenv("SECRET_KEY", 'booh')
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", '*')


STATIC_URL = '/static/'

ROOT_URLCONF = 'webmap.urls'
Expand All @@ -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',
Expand Down
14 changes: 11 additions & 3 deletions webmap/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
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
from .views import EditWeatherStation


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'^form/(?P<pk>\d+)$', EditWeatherStation.as_view(), name='form'),

url(r'^admin/', include(admin.site.urls)),
)
21 changes: 21 additions & 0 deletions webmap/views.py
Original file line number Diff line number Diff line change
@@ -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')