From a9c9c58e8fdced9a05cf30c28c744f1a988ebd49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Sch=C3=A4fer?= <felix@thegcat.net>
Date: Sat, 15 Jun 2019 17:36:10 +0200
Subject: [PATCH] Show public registrations on event front page

Currently limited to Gravatar based on attendee email and attendee name.
---
 pretix_public_registrations/signals.py        | 39 ++++++++++++++++++-
 .../front_page.html                           | 25 ++++++++++++
 setup.py                                      |  2 +-
 3 files changed, 63 insertions(+), 3 deletions(-)
 create mode 100644 pretix_public_registrations/templates/pretix_public_registrations/front_page.html

diff --git a/pretix_public_registrations/signals.py b/pretix_public_registrations/signals.py
index f284225..fcd6042 100644
--- a/pretix_public_registrations/signals.py
+++ b/pretix_public_registrations/signals.py
@@ -1,8 +1,11 @@
 from django import forms
 from django.dispatch import receiver
-from django.utils.translation import ugettext_lazy as _
+from django.template.loader import get_template
+from django.utils.translation import ugettext_lazy as _, get_language
+from django_gravatar.helpers import get_gravatar_url
 from i18nfield.strings import LazyI18nString
-from pretix.presale.signals import question_form_fields
+from pretix.presale.signals import question_form_fields, front_page_bottom, process_response
+from pretix.base.models import OrderPosition
 
 
 @receiver(question_form_fields, dispatch_uid="public_registration_question")
@@ -13,3 +16,35 @@ def add_public_registration_question(sender, **kwargs):
         help_text=sender.settings.get('public_registration_field_help_text', as_type=LazyI18nString),
         widget=forms.CheckboxInput(),
     )}
+
+
+@receiver(signal=front_page_bottom, dispatch_uid="public_registrations_table")
+def add_public_registrations_table(sender, **kwargs):
+    cached = sender.cache.get('public_registrations_table_' + get_language())
+    if cached is None:
+        cached = ""
+        headers = ["", "Name"]
+        order_positions = OrderPosition.all.filter(order__event=sender)
+        public_order_positions = [
+            op for op in order_positions
+            if op.meta_info_data.get('question_form_data', {}).get('public_registration') == "True"
+        ]
+        public_registrations = [
+            {
+                'gr_url': get_gravatar_url(pop.attendee_email, size=24, default="wavatar"),
+                'fields': [pop.attendee_name_cached]
+            } for pop in public_order_positions
+        ]
+        template = get_template('pretix_public_registrations/front_page.html')
+        cached = template.render({
+            'headers': headers,
+            'public_registrations': public_registrations
+        })
+    return cached
+
+
+@receiver(signal=process_response, dispatch_uid="public_registragions_csp_headers")
+def add_public_registrations_csp_headers(sender, **kwargs):
+    response = kwargs['response']
+    response['Content-Security-Policy'] = "img-src https://secure.gravatar.com"
+    return response
diff --git a/pretix_public_registrations/templates/pretix_public_registrations/front_page.html b/pretix_public_registrations/templates/pretix_public_registrations/front_page.html
new file mode 100644
index 0000000..9f6c551
--- /dev/null
+++ b/pretix_public_registrations/templates/pretix_public_registrations/front_page.html
@@ -0,0 +1,25 @@
+{% load i18n %}
+<section class="front-page">
+    <h3>{% trans "Public registrations" %}</h3>
+    <div class="table-responsive product-row">
+    <table class="table table-striped">
+        <thead>
+            <tr>
+                {% for h in headers %}
+                <th scope="col">{% trans h %}</th>
+                {% endfor %}
+            </tr>
+        </thead>
+        <tbody>
+        {% for pr in public_registrations %}
+            <tr>
+                <td><img class="gravatar" src="{{ pr.gr_url }}" /></td>
+                {% for f in pr.fields %}
+                <td>{{ f }}</td>
+                {% endfor %}
+            </tr>
+        {% endfor %}
+        </tbody>
+    </table>
+    </div>
+</section>
diff --git a/setup.py b/setup.py
index 92a1a0b..516ccce 100644
--- a/setup.py
+++ b/setup.py
@@ -33,7 +33,7 @@ setup(
     author_email='felix@kif.rocks',
     license='Apache Software License',
 
-    install_requires=[],
+    install_requires=['django-gravatar2'],
     packages=find_packages(exclude=['tests', 'tests.*']),
     include_package_data=True,
     cmdclass=cmdclass,
-- 
GitLab