port to new django, AI automated
This commit is contained in:
@@ -1,54 +1,67 @@
|
||||
import os
|
||||
|
||||
from django.forms import widgets
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.conf import settings
|
||||
import os
|
||||
|
||||
|
||||
class LocationWidget(widgets.TextInput):
|
||||
def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height=480, **kwargs):
|
||||
"""A widget that displays a map for selecting a location."""
|
||||
|
||||
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)
|
||||
super().__init__(attrs)
|
||||
|
||||
def render(self, name, value, attrs=None, renderer=None):
|
||||
|
||||
if value is not None and len(value) > 0:
|
||||
lat, lng, zoom = value.split(',')
|
||||
|
||||
value = '%s,%s,%d' % (
|
||||
float(lat),
|
||||
float(lng),
|
||||
float(zoom)
|
||||
)
|
||||
try:
|
||||
lat, lng, zoom = value.split(",")
|
||||
value = "%s,%s,%d" % (float(lat), float(lng), int(float(zoom)))
|
||||
except (ValueError, TypeError):
|
||||
value = ""
|
||||
else:
|
||||
value = ''
|
||||
value = ""
|
||||
|
||||
based_field = "#id_" + self.based_field.name
|
||||
if self.based_field is not None:
|
||||
based_field = "#id_" + self.based_field.name
|
||||
else:
|
||||
based_field = ""
|
||||
|
||||
attrs = attrs or {}
|
||||
attrs['readonly'] = "readonly"
|
||||
attrs['data-location-widget'] = name
|
||||
attrs['data-based-field'] = based_field
|
||||
attrs['data-zoom'] = self.zoom
|
||||
attrs['data-map'] = '#map_' + name
|
||||
attrs['data-dialog-id'] = "#map_dialog_" + name
|
||||
text_input = super(LocationWidget, self).render(name, value, attrs)
|
||||
attrs["readonly"] = "readonly"
|
||||
attrs["data-location-widget"] = name
|
||||
attrs["data-based-field"] = based_field
|
||||
attrs["data-zoom"] = self.zoom or 7
|
||||
attrs["data-map"] = "#map_" + name
|
||||
attrs["data-dialog-id"] = "#map_dialog_" + name
|
||||
|
||||
text_input = super().render(name, value, attrs, renderer)
|
||||
|
||||
# Load the HTML template for the map dialog
|
||||
path = os.path.abspath(os.path.dirname(__file__))
|
||||
with open(path + "/media/form.html", 'r') as content_file:
|
||||
html = content_file.read()
|
||||
template_path = os.path.join(path, "media", "form.html")
|
||||
|
||||
return mark_safe(text_input + html % {'name': name, 'width': self.width, 'height': self.height})
|
||||
try:
|
||||
with open(template_path, "r") as content_file:
|
||||
html = content_file.read()
|
||||
html = html % {"name": name, "width": self.width, "height": self.height}
|
||||
except (FileNotFoundError, IOError):
|
||||
html = ""
|
||||
|
||||
return mark_safe(text_input + html)
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('/location_field/media/form.css',)
|
||||
}
|
||||
# Use schemaless URL so it works with both, http and https websites
|
||||
css = {"all": ("location_field/form.css",)}
|
||||
js = (
|
||||
'//maps.google.com/maps/api/js?sensor=false&language=de&key={}'.format(settings.GOOGLE_MAPS_API_KEY),
|
||||
'/static/js/bindWithDelay.js',
|
||||
'/location_field/media/form.js',
|
||||
# jQuery and jQuery UI (consider using local copies or newer CDN)
|
||||
"https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js",
|
||||
"https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js",
|
||||
# Google Maps API - you may need to add an API key
|
||||
"https://maps.google.com/maps/api/js?language=de",
|
||||
"js/bindWithDelay.js",
|
||||
"location_field/form.js",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user