Fix #30 -- Event microdata information (#546)

* Addition of all basics required for microdata

Microdata for event added to the head of the base event html template.

* Fixed flake8 errors & included settings checks

Fixed the flake8 too many blank line errors and changed the date and time section to refer to settings before including the time and/or the date_to.

* Typo in last commit

Fixed typo in last commit

* Fixed bracket mistake and flake8 error

Fixed tab between { and % in template tag. Removed extra lines. Removed unnecessary str() call.
Corrected binary & to [and] operator.

* Added testing for microdata

Testing added to the presale.test_event tests to cover the 4 branches and basic microdata functionality.
This commit is contained in:
Joepriesto
2017-07-16 16:42:46 +01:00
committed by Raphael Michel
parent 9582f8380f
commit 27b73227ed
3 changed files with 71 additions and 0 deletions

View File

@@ -365,6 +365,26 @@ class Event(EventMixin, LoggedModel):
providers[pp.identifier] = pp
return providers
@property
def event_microdata(self):
import json
eventdict = {"@context": "http://schema.org", "@type": "Event"}
eventdict["location"] = {"@type": "Place",
"address": str(self.location)}
if self.settings.show_times:
eventdict["startDate"] = self.date_from.isoformat()
if self.settings.show_date_to and self.date_to is not None:
eventdict["endDate"] = self.date_to.isoformat()
else:
eventdict["startDate"] = self.date_from.date().isoformat()
if self.settings.show_date_to and self.date_to is not None:
eventdict["endDate"] = self.date_to.date().isoformat()
eventdict["name"] = str(self.name)
return json.dumps(eventdict)
def get_invoice_renderers(self) -> dict:
"""
Returns a dictionary of initialized invoice renderers mapped by their identifiers.

View File

@@ -30,6 +30,13 @@
<script type="text/javascript" src="{% static "pretixpresale/js/ui/typocheck.js" %}"></script>
<script type="text/javascript" src="{% static "lightbox/js/lightbox.min.js" %}"></script>
{% endcompress %}
{% if event %}
{% autoescape off %}
<script type="application/ld+json">
{{ event.event_microdata }}
</script>
{% endautoescape %}
{% endif %}
<meta name="referrer" content="origin">
{{ html_head|safe }}
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

@@ -1,6 +1,7 @@
import datetime
import re
from decimal import Decimal
from json import loads
from django.conf import settings
from django.core import mail
@@ -939,6 +940,49 @@ class EventIcalDownloadTest(EventTestMixin, SoupTest):
self.assertIn('LOCATION:Heeeeeere', ical, 'incorrect location')
class EventMicrodataTest(EventTestMixin, SoupTest):
def setUp(self):
super().setUp()
self.event.settings.show_date_to = True
self.event.settings.show_times = True
self.event.location = 'DUMMY ARENA'
self.event.date_from = datetime.datetime(2013, 12, 26, 21, 57, 58, tzinfo=datetime.timezone.utc)
self.event.date_to = self.event.date_from + datetime.timedelta(days=2)
self.event.settings.timezone = 'UTC'
self.event.save()
def _get_json(self):
doc = self.get_doc('/%s/%s/' % (self.orga.slug, self.event.slug))
microdata = loads(doc.find(type="application/ld+json").string)
return microdata
def test_name(self):
md = self._get_json()
self.assertEqual(self.event.name, md['name'], msg='Name not present')
def test_location(self):
md = self._get_json()
self.assertEqual(self.event.location, md['location']['address'], msg='Location not present')
def test_date_to(self):
md = self._get_json()
self.assertEqual(self.event.date_to.isoformat(), md['endDate'], msg='Date To not present')
self.event.settings.show_date_to = False
md = self._get_json()
self.assertNotIn(self.event.date_to.isoformat(), md,
msg='Date To present when show date to setting is false')
def test_no_times(self):
self.event.settings.show_times = False
md = self._get_json()
self.assertNotEqual(self.event.date_from.isoformat(), md['startDate'], msg='Date including time present')
self.assertEqual(self.event.date_from.date().isoformat(), md['startDate'], msg='Date not present at all')
def test_date_from(self):
md = self._get_json()
self.assertEqual(self.event.date_from.isoformat(), md['startDate'], msg='Date From not present')
class EventSlugBlacklistValidatorTest(EventTestMixin, SoupTest):
def test_slug_validation(self):
event = Event(