-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from makinacorpus/add_leaflet_form_widget_and_…
…admin Add leaflet form widget and admin
- Loading branch information
Showing
22 changed files
with
3,715 additions
and
9 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
include README.rst CHANGES LICENSE | ||
recursive-include leaflet/locale *.mo | ||
recursive-include leaflet/templates *.html | ||
recursive-include leaflet/templates *.html *.js | ||
recursive-include leaflet/static *.js | ||
recursive-include leaflet/static *.css | ||
recursive-include leaflet/static *.png |
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
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
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,41 @@ | ||
from django.contrib.admin import ModelAdmin | ||
from django.contrib.gis.db import models | ||
|
||
from .forms.widgets import LeafletWidget | ||
|
||
|
||
class LeafletGeoAdmin(ModelAdmin): | ||
widget = LeafletWidget | ||
map_template = 'leaflet/admin/widget.html' | ||
modifiable = True | ||
map_width = '100%' | ||
map_height = '400px' | ||
display_raw = False | ||
|
||
def formfield_for_dbfield(self, db_field, **kwargs): | ||
""" | ||
Overloaded from ModelAdmin to set Leaflet widget | ||
in form field init params. | ||
""" | ||
if isinstance(db_field, models.GeometryField) and (db_field.dim < 3 or | ||
self.widget.supports_3d): | ||
kwargs.pop('request', None) # unsupported for form field | ||
# Setting the widget with the newly defined widget. | ||
kwargs['widget'] = self._get_map_widget(db_field) | ||
return db_field.formfield(**kwargs) | ||
else: | ||
return super(LeafletGeoAdmin, self).formfield_for_dbfield(db_field, **kwargs) | ||
|
||
def _get_map_widget(self, db_field): | ||
""" | ||
Overriden LeafletWidget with LeafletGeoAdmin params. | ||
""" | ||
class LeafletMap(self.widget): | ||
template_name = self.map_template | ||
include_media = True | ||
geom_type = db_field.geom_type | ||
modifiable = self.modifiable, | ||
map_width = self.map_width | ||
map_height = self.map_height | ||
display_raw = self.display_raw | ||
return LeafletMap |
Empty file.
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,50 @@ | ||
import django | ||
|
||
if django.VERSION >= (1, 6, 0): | ||
from django.contrib.gis.forms.fields import GeometryField | ||
else: | ||
from django.contrib.gis.forms.fields import GeometryField as BaseField | ||
|
||
class GeometryField(BaseField): # noqa | ||
geom_type = 'GEOMETRY' | ||
|
||
def __init__(self, *args, **kwargs): | ||
kwargs['geom_type'] = self.geom_type | ||
super(GeometryField, self).__init__(*args, **kwargs) | ||
self.widget.attrs['geom_type'] = self.geom_type | ||
|
||
|
||
from .widgets import LeafletWidget | ||
|
||
|
||
class LeafletGeometryField(GeometryField): | ||
widget = LeafletWidget | ||
geom_type = 'GEOMETRY' | ||
|
||
|
||
class GeometryCollectionField(LeafletGeometryField): | ||
geom_type = 'GEOMETRYCOLLECTION' | ||
|
||
|
||
class PointField(LeafletGeometryField): | ||
geom_type = 'POINT' | ||
|
||
|
||
class MultiPointField(LeafletGeometryField): | ||
geom_type = 'MULTIPOINT' | ||
|
||
|
||
class LineStringField(LeafletGeometryField): | ||
geom_type = 'LINESTRING' | ||
|
||
|
||
class MultiLineStringField(LeafletGeometryField): | ||
geom_type = 'MULTILINESTRING' | ||
|
||
|
||
class PolygonField(LeafletGeometryField): | ||
geom_type = 'POLYGON' | ||
|
||
|
||
class MultiPolygonField(LeafletGeometryField): | ||
geom_type = 'MULTIPOLYGON' |
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,56 @@ | ||
from django import forms | ||
try: | ||
from django.contrib.gis.forms.widgets import BaseGeometryWidget | ||
except ImportError: | ||
from django.forms.widgets import Textarea | ||
|
||
class BaseGeometryWidget(Textarea): | ||
geom_type = 'GEOMETRY' | ||
display_raw = False | ||
|
||
from .. import PLUGINS, PLUGIN_FORMS | ||
|
||
|
||
class LeafletWidget(BaseGeometryWidget): | ||
template_name = 'leaflet/widget.html' | ||
map_srid = 4326 | ||
map_width = None | ||
map_height = None | ||
modifiable = True | ||
supports_3d = False | ||
include_media = False | ||
|
||
@property | ||
def media(self): | ||
if not self.include_media: | ||
return forms.Media() | ||
|
||
js = PLUGINS[PLUGIN_FORMS]['js'] | ||
css = PLUGINS[PLUGIN_FORMS]['css'] | ||
return forms.Media(js=js, css={'screen': css}) | ||
|
||
def serialize(self, value): | ||
return value.geojson if value else '' | ||
|
||
def deserialize(self, value): | ||
value = super(LeafletWidget, self).deserialize(value) | ||
if value: | ||
value.srid = self.map_srid | ||
return value | ||
|
||
def render(self, name, value, attrs=None): | ||
assert self.map_srid == 4326, 'Leaflet vectors should be decimal degrees.' | ||
|
||
attrs = attrs or {} | ||
|
||
# In BaseGeometryWidget, geom_type is set using gdal, and fails with generic. | ||
# See https://code.djangoproject.com/ticket/21021 | ||
if self.geom_type == 'GEOMETRY': | ||
attrs['geom_type'] = 'Geometry' | ||
|
||
attrs.update(id_map=attrs.get('id', name) + '_map', | ||
id_map_callback=attrs.get('id', name) + '_map_callback', | ||
modifiable=self.modifiable, | ||
geometry_field_class=attrs.get('geometry_field_class', 'L.GeometryField'), | ||
field_store_class=attrs.get('field_store_class', 'L.FieldStore')) | ||
return super(LeafletWidget, self).render(name, value, attrs) |
Binary file not shown.
Oops, something went wrong.