@@ -18,6 +18,9 @@ class LeafletWidget(BaseGeometryWidget):
18
18
modifiable = True
19
19
supports_3d = False
20
20
include_media = False
21
+ geometry_field_class = 'L.GeometryField'
22
+ field_store_class = 'L.FieldStore'
23
+ target_map = None
21
24
22
25
@property
23
26
def media (self ):
@@ -34,22 +37,24 @@ def serialize(self, value):
34
37
return value .geojson if value else ''
35
38
36
39
def render (self , name , value , attrs = None ):
37
- assert self . map_srid == 4326 , 'Leaflet vectors should be decimal degrees.'
40
+ context = self . build_attrs ( attrs )
38
41
39
- # Retrieve params from Field init (if any)
40
- self .geom_type = self .attrs .get ('geom_type' , self .geom_type )
42
+ id_map = context .get ('id' , name ).replace ('-' , '_' )
43
+ context .setdefault ('id_map' , id_map + '_map' ) # JS-safe
44
+ context .setdefault ('id_map_callback' , id_map + '_map_callback' )
41
45
42
- attrs = attrs or {}
46
+ override_at_class = ['geom_type' , 'modifiable' , 'map_srid' , 'target_map' ,
47
+ 'geometry_field_class' , 'field_store_class' ]
48
+ for key in override_at_class :
49
+ context .setdefault (key , getattr (self , key ))
50
+
51
+ assert context ['map_srid' ] == 4326 , 'Leaflet vectors should be decimal degrees.'
43
52
44
53
# In BaseGeometryWidget, geom_type is set using gdal, and fails with generic.
54
+ # https://github.com/django/django/blob/1.6.5/django/contrib/gis/forms/widgets.py#L73
45
55
# See https://code.djangoproject.com/ticket/21021
46
- if self .geom_type == 'GEOMETRY' :
47
- attrs ['geom_type' ] = 'Geometry'
48
-
49
- map_id = attrs .get ('id' , name ).replace ('-' , '_' ) # JS-safe
50
- attrs .update (id_map = map_id + '_map' ,
51
- id_map_callback = map_id + '_map_callback' ,
52
- modifiable = self .modifiable ,
53
- geometry_field_class = attrs .get ('geometry_field_class' , 'L.GeometryField' ),
54
- field_store_class = attrs .get ('field_store_class' , 'L.FieldStore' ))
55
- return super (LeafletWidget , self ).render (name , value , attrs )
56
+ if context ['geom_type' ] == 'GEOMETRY' :
57
+ context ['geom_type' ] = 'Geometry'
58
+ self .attrs ['geom_type' ] = context ['geom_type' ]
59
+
60
+ return super (LeafletWidget , self ).render (name , value , context )
0 commit comments