forked from CGM_Public/pretix_original
* Fix #1416 -- Add canonical geodata field for events and subevents * Add optional geocoding through OpenCageData * Fix markup everywhere * Add Leaflet map to geo coordinates * Fix tests, add credits * Fix spelling
This commit is contained in:
59
src/pretix/control/views/geo.py
Normal file
59
src/pretix/control/views/geo.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import logging
|
||||
from urllib.parse import quote
|
||||
|
||||
import requests
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.core.cache import cache
|
||||
from django.http import JsonResponse
|
||||
from django.views.generic.base import View
|
||||
|
||||
from pretix.base.settings import GlobalSettingsObject
|
||||
|
||||
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')
|
||||
cd = cache.get('geocode:{}'.format(q))
|
||||
if cd:
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'results': cd
|
||||
}, status=200)
|
||||
|
||||
try:
|
||||
r = requests.get(
|
||||
'https://api.opencagedata.com/geocode/v1/json?q={}&key={}'.format(
|
||||
quote(q), gs.settings.opencagedata_apikey
|
||||
)
|
||||
)
|
||||
r.raise_for_status()
|
||||
except IOError:
|
||||
logger.exception("Geocoding failed")
|
||||
return JsonResponse({
|
||||
'success': False,
|
||||
'results': []
|
||||
}, status=200)
|
||||
else:
|
||||
d = r.json()
|
||||
res = [
|
||||
{
|
||||
'formatted': r['formatted'],
|
||||
'lat': r['geometry']['lat'],
|
||||
'lon': r['geometry']['lng'],
|
||||
} for r in d['results']
|
||||
]
|
||||
cache.set('geocode:{}'.format(q), res, timeout=3600 * 6)
|
||||
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'results': res
|
||||
}, status=200)
|
||||
@@ -134,6 +134,8 @@ class EventWizard(SafeSessionWizardView):
|
||||
initial['currency'] = self.clone_from.currency
|
||||
initial['date_from'] = self.clone_from.date_from
|
||||
initial['date_to'] = self.clone_from.date_to
|
||||
initial['geo_lat'] = self.clone_from.geo_lat
|
||||
initial['geo_lon'] = self.clone_from.geo_lon
|
||||
initial['presale_start'] = self.clone_from.presale_start
|
||||
initial['presale_end'] = self.clone_from.presale_end
|
||||
initial['location'] = self.clone_from.location
|
||||
@@ -245,6 +247,8 @@ class EventWizard(SafeSessionWizardView):
|
||||
presale_start=event.presale_start,
|
||||
presale_end=event.presale_end,
|
||||
location=event.location,
|
||||
geo_lat=event.geo_lat,
|
||||
geo_lon=event.geo_lon,
|
||||
active=True
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user