Improved location widget

This commit is contained in:
Martin Bauer
2013-09-27 11:39:28 +02:00
parent 11135b5592
commit 05b7a2b969
8 changed files with 74 additions and 94 deletions

View File

@@ -1,11 +1,12 @@
from django.forms import widgets
from django.utils.safestring import mark_safe
class LocationWidget(widgets.TextInput):
def __init__(self, attrs=None, based_fields=None, zoom=None, **kwargs):
self.based_fields = based_fields
def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height = 480, **kwargs):
self.based_field = based_field
self.zoom = zoom
self.width = width
self.height = height
super(LocationWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
@@ -20,29 +21,23 @@ class LocationWidget(widgets.TextInput):
else:
value = ''
if '-' not in name:
prefix = ''
else:
prefix = name[:name.rindex('-') + 1]
based_fields = ','.join(
map(lambda f: '#id_' + prefix + f.name, self.based_fields))
based_field = "#id_" + self.based_field.name
attrs = attrs or {}
attrs['data-location-widget'] = name
attrs['data-based-fields'] = based_fields
attrs['data-based-field'] = based_field
attrs['data-zoom'] = self.zoom
attrs['data-map'] = '#map_' + name
text_input = super(LocationWidget, self).render(name, value, attrs)
map_div = u'''
<div style="margin:4px 0 0 0">
<label></label>
<div id="map_%(name)s" style="width: 610px; height: 350px"></div>
</div>
'''
return mark_safe(text_input + map_div % {'name': name})
<div style="margin:4px 0 0 0"> <label></label>
<div id="map_%(name)s" style="width: %(width)dpx; height: %(height)dpx"></div>
</div>
'''
return mark_safe(text_input + map_div % {'name': name, 'width': self.width, 'height': self.height })
class Media:
# Use schemaless URL so it works with both, http and https websites