Verified Commit cc07adb0 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Move google maps creation to template tag and add parameters

parent 0fac9c0a
......@@ -9,6 +9,14 @@ utils.templatetags package
Submodules
----------
utils.templatetags.google\_map\_url module
------------------------------------------
.. automodule:: utils.templatetags.google_map_url
:members:
:undoc-members:
:show-inheritance:
utils.templatetags.thumbnail module
-----------------------------------
......
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 django.conf import settings
def is_user_registered(member, event):
......@@ -237,21 +232,3 @@ 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)
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
{% extends "base.html" %}
{% load i18n static bleach_tags thumbnail %}
{% load i18n static bleach_tags thumbnail google_map_url %}
{% block title %}{{ event.title }} — {% trans "Calendar"|capfirst %} — {{ block.super }}{% endblock %}
{% block opengraph_title %}{{ event.title }} — {% trans "Calendar"|capfirst %} — {{ block.super }}{% endblock %}
......@@ -200,7 +200,7 @@
<div class="span6">
<div class="location-map">
<img src="{{ maps_url }}"/>
<img src="{% google_map_url event.location %}" alt="{{ event.location }}" />
</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
......@@ -18,7 +13,6 @@ 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):
......@@ -70,8 +64,6 @@ class EventDetail(DetailView):
context['date_now'] = timezone.now()
context['maps_url'] = services.create_google_maps_url(event)
return context
......
{% extends "base.html" %}
{% load i18n bleach_tags %}
{% load i18n bleach_tags google_map_url %}
{% block title %}{{ partner.name }} — {% trans "Partners" %} — {% trans "Career" %} — {{ block.super }}{% endblock %}
{% block opengraph_title %}{{ partner.name }} — {% trans "Partners" %} — {% trans "Career" %} — {{ block.super }}{% endblock %}
......@@ -69,7 +69,9 @@
{% endif %}
</div>
<div class="span7 offset1">
<img src="https://maps.google.com/maps/api/staticmap?center={{ partner.address|urlize }}%20{{ partner.zip_code|urlize }}%20{{ partner.city|urlize }},the+netherlands&zoom=10&size=620x200&maptype=roadmap&markers=color:0xE62272%7Clabel:%7C{{ partner.address|urlize }}%20{{ partner.zip_code|urlize }}%20{{ partner.city|urlize }},the+netherlands&sensor=false" />
{% with partner.address|add:" "|add:partner.zip_code|add:" "|add:partner.city|add:",The Netherlands"|urlize as location %}
<img src="{% google_map_url location zoom=10 size='650x200' %}" />
{% endwith %}
</div>
</div>
......
"""Provides various utilities that are useful across the project"""
import hmac
from _sha1 import sha1
from base64 import urlsafe_b64decode, urlsafe_b64encode
from django.conf import settings
from django.utils import timezone
from django.template.defaultfilters import urlencode
def datetime_to_lectureyear(date):
......@@ -26,3 +32,21 @@ def datetime_to_lectureyear(date):
if date < sept_1.date():
return date.year - 1
return date.year
def create_google_maps_url(location, zoom, size):
maps_url = (f"/maps/api/staticmap?"
f"center={ urlencode(location) }&"
f"zoom={ zoom }&size={ size }&"
f"markers={ urlencode(location) }&"
f"key={ settings.GOOGLE_MAPS_API_KEY }")
decoded_key = urlsafe_b64decode(settings.GOOGLE_MAPS_API_SECRET)
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
from django import template
from utils.snippets import create_google_maps_url
register = template.Library()
@register.simple_tag()
def google_map_url(location, zoom=13, size='450x250'):
return create_google_maps_url(location, zoom, size)
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