Commit 46515897 authored by Luko van der Maas's avatar Luko van der Maas
Browse files

added api key and signature to google maps static map api requests

parent 3da5d0b0
import hmac
from hashlib import sha1
from base64 import urlsafe_b64decode, urlsafe_b64encode
from collections import OrderedDict
from django.template.defaultfilters import urlencode
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _, get_language
from events import emails
from events.exceptions import RegistrationError
from events.models import Registration, RegistrationInformationField
from thaliawebsite.settings import settings
def is_user_registered(member, event):
......@@ -232,3 +237,22 @@ def registration_fields(member, event):
else:
raise RegistrationError(
_("You are not allowed to update this registration."))
def create_google_maps_url(event):
maps_url = (f"/maps/api/staticmap?"
f"center={ urlencode(event.map_location) }&"
f"zoom=13&size=450x250&"
f"markers={ urlencode(event.map_location) }&"
f"key={ settings.GOOGLE_MAPS_API_KEY }")
decoded_key = urlsafe_b64decode(settings.GOOGLE_MAPS_API_SECRET)
print(type(decoded_key))
signature = hmac.new(decoded_key, maps_url.encode(), sha1)
encoded_signature = urlsafe_b64encode(signature.digest())
maps_url += f"&signature={encoded_signature.decode('utf-8')}"
return "https://maps.googleapis.com" + maps_url
......@@ -200,7 +200,7 @@
<div class="span6">
<div class="location-map">
<img src="https://maps.googleapis.com/maps/api/staticmap?center={{ event.map_location|urlencode }}&zoom=13&size=450x250&markers={{ event.map_location|urlencode }}"/>
<img src="{{ maps_url }}"/>
</div>
</div>
</div>
......
"""Views provided by the events package"""
from hashlib import sha1
import hmac
from base64 import decodebytes, encodebytes, urlsafe_b64decode, \
urlsafe_b64encode
from urllib.parse import quote
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect
......@@ -12,6 +18,7 @@ from events import services
from events.exceptions import RegistrationError
from .forms import FieldsForm
from .models import Event, Registration
from thaliawebsite import settings
class EventIndex(TemplateView):
......@@ -63,6 +70,8 @@ class EventDetail(DetailView):
context['date_now'] = timezone.now()
context['maps_url'] = services.create_google_maps_url(event)
return context
......
......@@ -282,6 +282,10 @@ MAILINGLIST_API_SECRET = ''
# Members Sentry API key
MEMBERS_SENTRY_API_SECRET = ''
# Google maps API key and secrets
GOOGLE_MAPS_API_KEY = ''
GOOGLE_MAPS_API_SECRET = ''
# Photos settings
PHOTO_UPLOAD_SIZE = 1920, 1080
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment