mirror of
https://github.com/pretix/pretix.git
synced 2026-04-28 00:02:37 +00:00
Add MapQuest as additional geocoding provider
This commit is contained in:
@@ -1926,6 +1926,10 @@ Your {event} team"""))
|
||||
'default': None,
|
||||
'type': str
|
||||
},
|
||||
'mapquest_apikey': {
|
||||
'default': None,
|
||||
'type': str
|
||||
},
|
||||
'leaflet_tiles': {
|
||||
'default': None,
|
||||
'type': str
|
||||
|
||||
@@ -41,6 +41,10 @@ class GlobalSettingsForm(SettingsForm):
|
||||
required=False,
|
||||
label=_("OpenCage API key for geocoding"),
|
||||
)),
|
||||
('mapquest_apikey', SecretKeySettingsField(
|
||||
required=False,
|
||||
label=_("MapQuest API key for geocoding"),
|
||||
)),
|
||||
('leaflet_tiles', forms.CharField(
|
||||
required=False,
|
||||
label=_("Leaflet tiles URL pattern"),
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<form action="" method="post" class="form-horizontal" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
{% bootstrap_form_errors form %}
|
||||
{% bootstrap_form form layout='horizontal' %}
|
||||
{% bootstrap_form form layout='control' %}
|
||||
<div class="form-group submit-group">
|
||||
<button type="submit" class="btn btn-primary btn-save">
|
||||
{% trans "Save" %}
|
||||
|
||||
@@ -14,14 +14,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class GeoCodeView(LoginRequiredMixin, View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
gs = GlobalSettingsObject()
|
||||
if not gs.settings.opencagedata_apikey:
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'results': []
|
||||
}, status=200)
|
||||
|
||||
q = request.GET.get('q')
|
||||
q = self.request.GET.get('q')
|
||||
cd = cache.get('geocode:{}'.format(q))
|
||||
if cd:
|
||||
return JsonResponse({
|
||||
@@ -29,6 +22,26 @@ class GeoCodeView(LoginRequiredMixin, View):
|
||||
'results': cd
|
||||
}, status=200)
|
||||
|
||||
gs = GlobalSettingsObject()
|
||||
if gs.settings.opencagedata_apikey:
|
||||
res = self._use_opencage(q)
|
||||
if gs.settings.mapquest_apikey:
|
||||
res = self._use_mapquest(q)
|
||||
else:
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'results': []
|
||||
}, status=200)
|
||||
|
||||
cache.set('geocode:{}'.format(q), res, timeout=3600 * 6)
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'results': res
|
||||
}, status=200)
|
||||
|
||||
def _use_opencage(self, q):
|
||||
gs = GlobalSettingsObject()
|
||||
|
||||
try:
|
||||
r = requests.get(
|
||||
'https://api.opencagedata.com/geocode/v1/json?q={}&key={}'.format(
|
||||
@@ -51,9 +64,31 @@ class GeoCodeView(LoginRequiredMixin, View):
|
||||
'lon': r['geometry']['lng'],
|
||||
} for r in d['results']
|
||||
]
|
||||
cache.set('geocode:{}'.format(q), res, timeout=3600 * 6)
|
||||
return res
|
||||
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'results': res
|
||||
}, status=200)
|
||||
def _use_mapquest(self, q):
|
||||
gs = GlobalSettingsObject()
|
||||
|
||||
try:
|
||||
r = requests.get(
|
||||
'http://www.mapquestapi.com/geocoding/v1/address?location={}&key={}'.format(
|
||||
quote(q), gs.settings.mapquest_apikey
|
||||
)
|
||||
)
|
||||
r.raise_for_status()
|
||||
except IOError:
|
||||
logger.exception("Geocoding failed")
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'results': []
|
||||
}, status=200)
|
||||
else:
|
||||
d = r.json()
|
||||
res = [
|
||||
{
|
||||
'formatted': q,
|
||||
'lat': r['locations'][0]['latLng']['lat'],
|
||||
'lon': r['locations'][0]['latLng']['lng'],
|
||||
} for r in d['results']
|
||||
]
|
||||
return res
|
||||
|
||||
@@ -128,6 +128,7 @@ Leaflet
|
||||
loszulegen
|
||||
Ltd
|
||||
max
|
||||
MapQuest
|
||||
Merchandise
|
||||
Meta
|
||||
Metadaten
|
||||
|
||||
@@ -128,6 +128,7 @@ Leaflet
|
||||
loszulegen
|
||||
Ltd
|
||||
max
|
||||
MapQuest
|
||||
Merchandise
|
||||
Meta
|
||||
Metadaten
|
||||
|
||||
@@ -48,6 +48,7 @@ Kosovo
|
||||
Lead
|
||||
Leaflet
|
||||
LLC
|
||||
MapQuest
|
||||
MOTO
|
||||
Multibanco
|
||||
multiline
|
||||
|
||||
Reference in New Issue
Block a user