blechreiz-website/location_field/widgets.py

60 lines
1.9 KiB
Python
Raw Normal View History

from django.forms import widgets
from django.utils.safestring import mark_safe
2013-09-28 11:41:05 +02:00
import os
class LocationWidget(widgets.TextInput):
2013-09-27 11:39:28 +02:00
def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height = 480, **kwargs):
self.based_field = based_field
self.zoom = zoom
2013-09-27 11:39:28 +02:00
self.width = width
self.height = height
super(LocationWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
2013-10-15 21:51:05 +02:00
if value is not None and len(value)>0:
lat, lng, zoom = value.split(',')
value = '%s,%s,%d' % (
float(lat),
float(lng),
float(zoom)
)
else:
value = ''
2013-09-27 11:39:28 +02:00
based_field = "#id_" + self.based_field.name
attrs = attrs or {}
2013-09-28 11:41:05 +02:00
attrs['readonly'] = "readonly"
attrs['data-location-widget'] = name
2013-09-27 11:39:28 +02:00
attrs['data-based-field'] = based_field
attrs['data-zoom'] = self.zoom
attrs['data-map'] = '#map_' + name
2013-09-28 11:41:05 +02:00
attrs['data-dialog-id'] = "#map_dialog_" + name
text_input = super(LocationWidget, self).render(name, value, attrs)
2013-09-27 11:39:28 +02:00
2013-09-28 11:41:05 +02:00
path = os.path.abspath(os.path.dirname(__file__))
with open( path + "/media/form.html" , 'r') as content_file:
html = content_file.read()
return mark_safe(text_input + html % {'name': name, 'width': self.width, 'height': self.height })
class Media:
2013-09-28 11:41:05 +02:00
css = {
'all' : ('/location_field/media/form.css', )
}
# Use schemaless URL so it works with both, http and https websites
js = (
2014-02-15 18:07:00 +01:00
'//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', # jquery
2013-09-28 11:41:05 +02:00
'//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js',
'//maps.google.com/maps/api/js?sensor=false&language=de',
'/static/js/bindWithDelay.js',
'/location_field/media/form.js',
)
2013-09-28 11:41:05 +02:00