2013-09-22 11:11:48 +02:00
|
|
|
from django.forms import widgets
|
|
|
|
from django.utils.safestring import mark_safe
|
2019-01-05 11:27:15 +01:00
|
|
|
from django.conf import settings
|
2013-09-28 11:41:05 +02:00
|
|
|
import os
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
class LocationWidget(widgets.TextInput):
|
2019-01-05 11:27:15 +01:00
|
|
|
def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height=480, **kwargs):
|
2013-09-27 11:39:28 +02:00
|
|
|
self.based_field = based_field
|
2013-09-22 11:11:48 +02:00
|
|
|
self.zoom = zoom
|
2013-09-27 11:39:28 +02:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2013-09-22 11:11:48 +02:00
|
|
|
super(LocationWidget, self).__init__(attrs)
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
def render(self, name, value, attrs=None, renderer=None):
|
|
|
|
|
|
|
|
if value is not None and len(value) > 0:
|
2013-09-22 11:11:48 +02:00
|
|
|
lat, lng, zoom = value.split(',')
|
2019-01-05 11:27:15 +01:00
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
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
|
2019-01-05 11:27:15 +01:00
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
attrs = attrs or {}
|
2013-09-28 11:41:05 +02:00
|
|
|
attrs['readonly'] = "readonly"
|
2013-09-22 11:11:48 +02:00
|
|
|
attrs['data-location-widget'] = name
|
2013-09-27 11:39:28 +02:00
|
|
|
attrs['data-based-field'] = based_field
|
2013-09-22 11:11:48 +02:00
|
|
|
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
|
2013-09-22 11:11:48 +02:00
|
|
|
text_input = super(LocationWidget, self).render(name, value, attrs)
|
|
|
|
|
2013-09-28 11:41:05 +02:00
|
|
|
path = os.path.abspath(os.path.dirname(__file__))
|
2019-01-05 11:27:15 +01:00
|
|
|
with open(path + "/media/form.html", 'r') as content_file:
|
2013-09-28 11:41:05 +02:00
|
|
|
html = content_file.read()
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
return mark_safe(text_input + html % {'name': name, 'width': self.width, 'height': self.height})
|
2013-09-22 11:11:48 +02:00
|
|
|
|
|
|
|
class Media:
|
2019-01-05 11:27:15 +01:00
|
|
|
css = {
|
|
|
|
'all': ('/location_field/media/form.css',)
|
2013-09-28 11:41:05 +02:00
|
|
|
}
|
2013-09-22 11:11:48 +02:00
|
|
|
# Use schemaless URL so it works with both, http and https websites
|
|
|
|
js = (
|
2019-01-05 12:48:20 +01:00
|
|
|
'//maps.google.com/maps/api/js?sensor=false&language=de&key={}'.format(settings.GOOGLE_MAPS_API_KEY),
|
2013-09-22 11:11:48 +02:00
|
|
|
'/static/js/bindWithDelay.js',
|
|
|
|
'/location_field/media/form.js',
|
|
|
|
)
|