mirror of
https://github.com/pretix/pretix.git
synced 2026-05-09 15:54:03 +00:00
Add central cookie consent mechanism (#2330)
Co-authored-by: Richard Schreiber <schreiber@rami.io>
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
# Unless required by applicable law or agreed to in writing, software distributed under the Apache License 2.0 is
|
||||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations under the License.
|
||||
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
@@ -47,10 +46,12 @@ from pretix.helpers.i18n import (
|
||||
)
|
||||
|
||||
from ..base.i18n import get_language_without_region
|
||||
from .cookies import get_cookie_providers
|
||||
from .signals import (
|
||||
footer_link, global_footer_link, global_html_footer, global_html_head,
|
||||
global_html_page_header, html_footer, html_head, html_page_header,
|
||||
)
|
||||
from .views.cart import cart_session, get_or_create_cart_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -140,6 +141,12 @@ def _default_context(request):
|
||||
ctx['event'] = request.event
|
||||
ctx['languages'] = [get_language_info(code) for code in request.event.settings.locales]
|
||||
|
||||
ctx['cookie_providers'] = get_cookie_providers(request.event, request)
|
||||
if get_or_create_cart_id(request, create=False):
|
||||
c = cart_session(request)
|
||||
if "widget_data" in c and c["widget_data"].get("consent"):
|
||||
ctx['cookie_consent_from_widget'] = c["widget_data"].get("consent").split(",")
|
||||
|
||||
if request.resolver_match:
|
||||
ctx['cart_namespace'] = request.resolver_match.kwargs.get('cart_namespace', '')
|
||||
elif hasattr(request, 'organizer'):
|
||||
|
||||
64
src/pretix/presale/cookies.py
Normal file
64
src/pretix/presale/cookies.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#
|
||||
# This file is part of pretix (Community Edition).
|
||||
#
|
||||
# Copyright (C) 2014-2020 Raphael Michel and contributors
|
||||
# Copyright (C) 2020-2021 rami.io GmbH and contributors
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
|
||||
# Public License as published by the Free Software Foundation in version 3 of the License.
|
||||
#
|
||||
# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are
|
||||
# applicable granting you additional permissions and placing additional restrictions on your usage of this software.
|
||||
# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive
|
||||
# this file, see <https://pretix.eu/about/en/license>.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
||||
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
|
||||
# <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
# This file is based on an earlier version of pretix which was released under the Apache License 2.0. The full text of
|
||||
# the Apache License 2.0 can be obtained at <http://www.apache.org/licenses/LICENSE-2.0>.
|
||||
#
|
||||
# This file may have since been changed and any changes are released under the terms of AGPLv3 as described above. A
|
||||
# full history of changes and contributors is available at <https://github.com/pretix/pretix>.
|
||||
#
|
||||
# This file contains Apache-licensed contributions copyrighted by: Tobias Kunze
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software distributed under the Apache License 2.0 is
|
||||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations under the License.
|
||||
from enum import Enum
|
||||
from typing import List
|
||||
|
||||
from pretix.presale.signals import register_cookie_providers
|
||||
|
||||
|
||||
class UsageClass(Enum):
|
||||
FUNCTIONAL = 1
|
||||
ANALYTICS = 2
|
||||
MARKETING = 3
|
||||
SOCIAL = 4
|
||||
|
||||
|
||||
class CookieProvider:
|
||||
def __init__(self, identifier: str, usage_classes: List[UsageClass], provider_name: str, privacy_url: str = None, **kwargs):
|
||||
self.identifier = identifier
|
||||
self.usage_classes = usage_classes
|
||||
self.provider_name = provider_name
|
||||
self.privacy_url = privacy_url
|
||||
|
||||
|
||||
def get_cookie_providers(event, request):
|
||||
c = [
|
||||
]
|
||||
for receiver, response in register_cookie_providers.send(event, request=request):
|
||||
if isinstance(response, list):
|
||||
c += response
|
||||
else:
|
||||
c.append(response)
|
||||
c.sort(key=lambda k: str(k.provider_name))
|
||||
return c
|
||||
@@ -401,3 +401,13 @@ This signal is sent out when the description of an item or variation is rendered
|
||||
additional text to the description. You are passed the ``item`` and ``variation`` and expected to return
|
||||
HTML.
|
||||
"""
|
||||
|
||||
register_cookie_providers = EventPluginSignal()
|
||||
"""
|
||||
Arguments: ``request``
|
||||
|
||||
This signal is sent out to get all cookie providers that could set a cookie on this page, regardless of
|
||||
consent state. Receivers should return a list of ``pretix.presale.cookies.CookieProvider`` objects.
|
||||
|
||||
As with all event-plugin signals, the ``sender`` keyword argument will contain the event.
|
||||
"""
|
||||
|
||||
@@ -169,6 +169,12 @@
|
||||
{% if request.event.settings.contact_mail %}
|
||||
<li><a href="mailto:{{ request.event.settings.contact_mail }}">{% trans "Contact event organizer" %}</a></li>
|
||||
{% endif %}
|
||||
{% if request.event.settings.privacy_url %}
|
||||
<li><a href="{% safelink request.event.settings.privacy_url %}" target="_blank" rel="noopener">{% trans "Privacy policy" %}</a></li>
|
||||
{% endif %}
|
||||
{% if request.event.settings.cookie_consent and cookie_providers %}
|
||||
<li><button type="button" class="btn btn-link" id="cookie-consent-reopen">{% trans "Cookie settings" %}</button></li>
|
||||
{% endif %}
|
||||
{% if request.event.settings.imprint_url %}
|
||||
<li><a href="{% safelink request.event.settings.imprint_url %}" target="_blank" rel="noopener">{% trans "Imprint" %}</a></li>
|
||||
{% endif %}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<script type="text/javascript" src="{% static "pretixpresale/js/widget/floatformat.js" %}"></script>
|
||||
<script type="text/javascript" src="{% static "pretixpresale/js/ui/questions.js" %}"></script>
|
||||
<script type="text/javascript" src="{% static "pretixpresale/js/ui/main.js" %}"></script>
|
||||
<script type="text/javascript" src="{% static "pretixpresale/js/ui/cookieconsent.js" %}"></script>
|
||||
<script type="text/javascript" src="{% static "pretixbase/js/asynctask.js" %}"></script>
|
||||
<script type="text/javascript" src="{% static "pretixpresale/js/ui/cart.js" %}"></script>
|
||||
<script type="text/javascript" src="{% static "lightbox/js/lightbox.js" %}"></script>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
{% load i18n %}
|
||||
{% load rich_text %}
|
||||
{% load safelink %}
|
||||
{% load escapejson %}
|
||||
<div id="ajaxerr">
|
||||
</div>
|
||||
<div id="loadingmodal" hidden aria-live="polite">
|
||||
@@ -15,3 +18,95 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% if request.organizer and request.organizer.settings.cookie_consent %}
|
||||
<script type="text/plain" id="cookie-consent-storage-key">cookie-consent-{{ request.organizer.slug }}</script>
|
||||
{% if cookie_consent_from_widget %}
|
||||
{{ cookie_consent_from_widget|json_script:"cookie-consent-from-widget" }}
|
||||
{% endif %}
|
||||
{% if cookie_providers %}
|
||||
<div id="cookie-consent-modal" aria-live="polite">
|
||||
<div class="modal-card">
|
||||
<div class="modal-card-content">
|
||||
<h3 id="cookie-consent-modal-label"></h3>
|
||||
<div id="cookie-consent-modal-description">
|
||||
{% with request.event|default:request.organizer as sh %}
|
||||
<h3>{{ sh.settings.cookie_consent_dialog_title }}</h3>
|
||||
{{ sh.settings.cookie_consent_dialog_text|rich_text }}
|
||||
{% if sh.settings.cookie_consent_dialog_text_secondary %}
|
||||
<div class="text-muted">
|
||||
{{ sh.settings.cookie_consent_dialog_text_secondary|rich_text }}
|
||||
</div>
|
||||
{% endif %}
|
||||
<details id="cookie-consent-details">
|
||||
<summary>
|
||||
<span class="fa fa-fw chevron"></span>
|
||||
{% trans "Adjust settings in detail" %}
|
||||
</summary>
|
||||
<div class="checkbox">
|
||||
<label>
|
||||
<input type="checkbox" disabled checked="">
|
||||
{% trans "Required cookies" %}<br>
|
||||
<span class="text-muted">
|
||||
{% trans "Functional cookies (e.g. shopping cart, login, payment, language preference) and technical cookies (e.g. security purposes)" %}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
{% for cp in cookie_providers %}
|
||||
<div class="checkbox">
|
||||
<label>
|
||||
<input type="checkbox" name="{{ cp.identifier }}">
|
||||
{{ cp.provider_name }}<br>
|
||||
<span class="text-muted">
|
||||
{% for c in cp.usage_classes %}
|
||||
{% if forloop.counter0 > 0 %}· {% endif %}
|
||||
{% if c.value == 1 %}
|
||||
{% trans "Functionality" context "cookie_usage" %}
|
||||
{% elif c.value == 2 %}
|
||||
{% trans "Analytics" context "cookie_usage" %}
|
||||
{% elif c.value == 3 %}
|
||||
{% trans "Marketing" context "cookie_usage" %}
|
||||
{% elif c.value == 4 %}
|
||||
{% trans "Social features" context "cookie_usage" %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if cp.privacy_url %}
|
||||
·
|
||||
<a href="{% safelink cp.privacy_url %}" target="_blank">
|
||||
{% trans "Privacy policy" %}
|
||||
</a>
|
||||
{% endif %}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</details>
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-md-6">
|
||||
<p>
|
||||
<button type="button" class="btn btn-lg btn-block btn-primary" id="cookie-consent-button-no"
|
||||
data-summary-text="{{ sh.settings.cookie_consent_dialog_button_no }}"
|
||||
data-detail-text="{% trans "Save selection" %}">
|
||||
{{ sh.settings.cookie_consent_dialog_button_no }}
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
<div class="col-xs-12 col-md-6">
|
||||
<p>
|
||||
<button type="button" class="btn btn-lg btn-block btn-primary" id="cookie-consent-button-yes">
|
||||
{{ sh.settings.cookie_consent_dialog_button_yes }}
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{% if sh.settings.privacy_url %}
|
||||
<p class="text-center">
|
||||
<a href="{% safelink sh.settings.privacy_url %}" target="_blank" rel="noopener">{% trans "Privacy policy" %}</a>
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
@@ -87,6 +87,12 @@
|
||||
{% if not request.event and request.organizer.settings.contact_mail %}
|
||||
<li><a href="mailto:{{ request.organizer.settings.contact_mail }}">{% trans "Contact event organizer" %}</a></li>
|
||||
{% endif %}
|
||||
{% if not request.event and request.organizer.settings.privacy_url %}
|
||||
<li><a href="{% safelink request.organizer.settings.privacy_url %}" target="_blank" rel="noopener">{% trans "Privacy policy" %}</a></li>
|
||||
{% endif %}
|
||||
{% if not request.event and request.organizer.settings.cookie_consent and cookie_providers %}
|
||||
<li><button type="button" class="btn btn-link" id="cookie-consent-reopen">{% trans "Cookie settings" %}</button></li>
|
||||
{% endif %}
|
||||
{% if not request.event and request.organizer.settings.imprint_url %}
|
||||
<li><a href="{% safelink request.organizer.settings.imprint_url %}" target="_blank" rel="noopener">{% trans "Imprint" %}</a></li>
|
||||
{% endif %}
|
||||
|
||||
Reference in New Issue
Block a user