From d147b2d824fce5cf7c03a618ac6b318d2a3080c1 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 18 Mar 2019 20:02:26 +0100
Subject: [PATCH 3/4] matomo: dedicated ihm for manual configuration (#19743)

---
 hobo/matomo/__init__.py                       |   0
 hobo/matomo/forms.py                          |  29 +++++
 .../matomo/templates/hobo/matomo_disable.html |  20 ++++
 .../templates/hobo/matomo_enable_auto.html    |  20 ++++
 .../templates/hobo/matomo_enable_manual.html  |  20 ++++
 hobo/matomo/templates/hobo/matomo_home.html   |  84 ++++++++++++++
 hobo/matomo/urls.py                           |  26 +++++
 hobo/matomo/views.py                          | 105 ++++++++++++++++++
 hobo/settings.py                              |   1 +
 hobo/templates/hobo/home.html                 |   1 +
 hobo/urls.py                                  |   3 +
 11 files changed, 309 insertions(+)
 create mode 100644 hobo/matomo/__init__.py
 create mode 100644 hobo/matomo/forms.py
 create mode 100644 hobo/matomo/templates/hobo/matomo_disable.html
 create mode 100644 hobo/matomo/templates/hobo/matomo_enable_auto.html
 create mode 100644 hobo/matomo/templates/hobo/matomo_enable_manual.html
 create mode 100644 hobo/matomo/templates/hobo/matomo_home.html
 create mode 100644 hobo/matomo/urls.py
 create mode 100644 hobo/matomo/views.py

diff --git a/hobo/matomo/__init__.py b/hobo/matomo/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/hobo/matomo/forms.py b/hobo/matomo/forms.py
new file mode 100644
index 0000000..dfb467b
--- /dev/null
+++ b/hobo/matomo/forms.py
@@ -0,0 +1,29 @@
+# hobo - portal to configure and deploy applications
+# Copyright (C) 2015-2019  Entr'ouvert
+#
+# 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+
+class SettingsForm(forms.Form):
+    tracking_js = forms.CharField(
+            label=_('Tracking Javascript'),
+            help_text=_('See <a href="https://developer.matomo.org/guides/tracking-javascript-guide">'
+                        'Matomo partners site</a> for getting your Javascript\'s tracking code.'),
+            widget=forms.Textarea(attrs={'size': 1024}))
+
+class EnableForm(forms.Form):
+    pass
diff --git a/hobo/matomo/templates/hobo/matomo_disable.html b/hobo/matomo/templates/hobo/matomo_disable.html
new file mode 100644
index 0000000..c37c23d
--- /dev/null
+++ b/hobo/matomo/templates/hobo/matomo_disable.html
@@ -0,0 +1,20 @@
+{% extends "hobo/matomo_home.html" %}
+{% load i18n %}
+
+{% block appbar %}
+  <h2>Matomo</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post">
+{% csrf_token %}
+<p>
+{% trans "Are you sure you want to disable Matomo support?" %}
+{{ form.as_p }}
+<div class="buttons">
+<button class="submit-button">{% trans "Disable" %}</button>
+<a class="cancel" href="{% url 'matomo-home' %}">{% trans "Cancel" %}</a>
+</div>
+</form>
+
+{% endblock %}
diff --git a/hobo/matomo/templates/hobo/matomo_enable_auto.html b/hobo/matomo/templates/hobo/matomo_enable_auto.html
new file mode 100644
index 0000000..4cca969
--- /dev/null
+++ b/hobo/matomo/templates/hobo/matomo_enable_auto.html
@@ -0,0 +1,20 @@
+{% extends "hobo/matomo_home.html" %}
+{% load i18n %}
+
+{% block appbar %}
+  <h2>Matomo</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post">
+{% csrf_token %}
+<p>
+{% trans "Are you sure you want to enable Matomo automatic support?" %}
+{{ form.as_p }}
+<div class="buttons">
+<button class="submit-button">{% trans "Enable" %}</button>
+<a class="cancel" href="{% url 'matomo-home' %}">{% trans "Cancel" %}</a>
+</div>
+</form>
+
+{% endblock %}
diff --git a/hobo/matomo/templates/hobo/matomo_enable_manual.html b/hobo/matomo/templates/hobo/matomo_enable_manual.html
new file mode 100644
index 0000000..b6d62f1
--- /dev/null
+++ b/hobo/matomo/templates/hobo/matomo_enable_manual.html
@@ -0,0 +1,20 @@
+{% extends "hobo/matomo_home.html" %}
+{% load i18n %}
+
+{% block appbar %}
+  <h2>Matomo</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post">
+{% csrf_token %}
+<p>
+{% trans "Are you sure you want to enable Matomo manual support?" %}
+{{ form.as_p }}
+<div class="buttons">
+<button class="submit-button">{% trans "Enable" %}</button>
+<a class="cancel" href="{% url 'matomo-home' %}">{% trans "Cancel" %}</a>
+</div>
+</form>
+
+{% endblock %}
diff --git a/hobo/matomo/templates/hobo/matomo_home.html b/hobo/matomo/templates/hobo/matomo_home.html
new file mode 100644
index 0000000..e1c87e7
--- /dev/null
+++ b/hobo/matomo/templates/hobo/matomo_home.html
@@ -0,0 +1,84 @@
+{% extends "hobo/base.html" %}
+{% load i18n %}
+
+{% block breadcrumb %}
+{{ block.super }}
+<a href="{% url 'matomo-home' %}">Matomo</a>
+{% endblock %}
+
+{% block appbar %}
+  <h2>{% trans 'Matomo' %}</h2>
+  {% if enabled %}
+  <span class="actions">
+  <a rel="popup" href="{% url 'matomo-disable' %}">{% trans 'Disable' %}</a>
+  </span>
+  {% endif %}
+{% endblock %}
+
+{% block content %}
+
+<div class="infonotice">
+  {% blocktrans %}
+  <p>
+  Matomo is the tracker solution recommended by the National Commission for Data Protection and Liberties (<a href=https://www.cnil.fr/fr/solutions-pour-les-cookies-de-mesure-daudience>CNIL-France</a>).
+  </p>
+  <p>
+  Les outils de mesures d’audience sont déployés afin d’obtenir des informations sur la navigation des visiteurs. Ils permettent notamment de comprendre comment les utilisateurs arrivent sur un site et de reconstituer leur parcours. Ces outils utilisent des technologies permettant de tracer les utilisateurs sur le site et d’associer un « referrer » ou une campagne à un identifiant unique.
+  </p>
+  L'outil Matomo requiert qu'un léger paramétrage pour être exempté de consentement en conformité avec la règlementation :
+  <ul>
+    <li>La non prorogation des cookies</li>
+    <li>Anonymiser l’adresse IP</li>
+    <li>Offrir une solution permettant de s’opposer au dépôt de cookie à des fins d’analytics</li>
+  </ul>
+  {% endblocktrans %}
+</div>
+
+{% if not enabled %}
+<p>
+ {% trans "Support is currently disabled." %}
+</p>
+<p>
+  <a class="button" rel="popup" href="{% url 'matomo-enable-manual' %}">{% trans 'Manual' %}</a>
+  <a class="button" rel="popup" href="{% url 'matomo-enable-auto' %}">{% trans 'Automatic' %}</a>
+</p>
+{% else %}
+
+{% if cnil_ack_level == 'success' %}
+<div class="successnotice">
+  suivi des recommandations CNIL
+</div>
+{% elif cnil_ack_level == 'warning' %}
+<div class="warningnotice">
+  suivi des recommandations CNIL
+</div>
+{% elif cnil_ack_level == 'error' %}
+<div class="errornotice">
+  suivi des recommandations CNIL
+</div>
+{% endif %}
+
+{% if mode == 'manual' %}
+<p>
+ {% trans "Manual mode." %}
+</p>
+{% endif %}
+
+{% if mode == 'auto' %}
+<p>
+ {% trans "Automatic mode." %}
+</p>
+{% endif %}
+
+<form method="post">
+{% csrf_token %}
+{{ form.as_p }}
+
+<div class="buttons">
+<button class="submit-button">{% trans "Save" %}</button>
+</div>
+</form>
+
+{% endif %}
+
+{% endblock %}
diff --git a/hobo/matomo/urls.py b/hobo/matomo/urls.py
new file mode 100644
index 0000000..e070c8d
--- /dev/null
+++ b/hobo/matomo/urls.py
@@ -0,0 +1,26 @@
+# hobo - portal to configure and deploy applications
+# Copyright (C) 2015-2019  Entr'ouvert
+#
+# 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+from django.conf.urls import url
+
+from . import views
+
+urlpatterns = [
+    url(r'^$', views.home, name='matomo-home'),
+    url(r'^enable-manual$', views.enable_manual, name='matomo-enable-manual'),
+    url(r'^enable-auto$', views.enable_auto, name='matomo-enable-auto'),
+    url(r'^disable$', views.disable, name='matomo-disable'),
+]
diff --git a/hobo/matomo/views.py b/hobo/matomo/views.py
new file mode 100644
index 0000000..f9552b9
--- /dev/null
+++ b/hobo/matomo/views.py
@@ -0,0 +1,105 @@
+# hobo - portal to configure and deploy applications
+# Copyright (C) 2015-2019  Entr'ouvert
+#
+# 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+from django.core.urlresolvers import reverse_lazy
+from django.views.generic import RedirectView, FormView
+
+from .forms import SettingsForm, EnableForm
+from .utils import get_variable, get_matomo_settings, \
+     get_tenant_name_and_public_urls, auto_configure_matomo, \
+     compute_cnil_acknowledgment_level
+
+
+class HomeView(FormView):
+    template_name = 'hobo/matomo_home.html'
+    form_class = SettingsForm
+    success_url = reverse_lazy('matomo-home')
+
+    def get_initial(self):
+        initial = super(HomeView, self).get_initial()
+        initial['tracking_js'] = get_variable('cnil_compliant_visits_tracking_js').value
+        return initial
+
+    def form_valid(self, form):
+        if get_variable('matomo_mode').json == 'manual':
+            variable = get_variable('cnil_compliant_visits_tracking_js')
+            variable.value = form.cleaned_data['tracking_js']
+            variable.save()
+        return super(HomeView, self).form_valid(form)
+
+    def get_context_data(self, **kwargs):
+        context = super(HomeView, self).get_context_data(**kwargs)
+        context['enabled'] = bool(get_variable('matomo_enable').json)
+        context['mode'] = get_variable('matomo_mode').json
+        tracking_js = get_variable('cnil_compliant_visits_tracking_js').value
+        context['cnil_ack_level'] = compute_cnil_acknowledgment_level(tracking_js)
+        return context
+
+home = HomeView.as_view()
+
+
+class EnableManualView(FormView):
+    form_class = EnableForm
+    template_name = 'hobo/matomo_enable_manual.html'
+    success_url = reverse_lazy('matomo-home')
+
+    def form_valid(self, form):
+        variable = get_variable('matomo_enable')
+        variable.value = 'true'
+        variable.save()
+        variable = get_variable('matomo_mode')
+        variable.value = 'manual'
+        variable.save()
+        return super(EnableManualView, self).form_valid(form)
+
+enable_manual = EnableManualView.as_view()
+
+
+class EnableAutoView(FormView):
+    form_class = EnableForm
+    template_name = 'hobo/matomo_enable_auto.html'
+    success_url = reverse_lazy('matomo-home')
+
+    def form_valid(self, form):
+        variable = get_variable('matomo_enable')
+        variable.value = 'true'
+        variable.save()
+        variable = get_variable('matomo_mode')
+        variable.value = 'auto'
+        variable.save()
+        return super(EnableAutoView, self).form_valid(form)
+
+enable_auto = EnableAutoView.as_view()
+
+
+class DisableView(FormView):
+    form_class = EnableForm
+    template_name = 'hobo/matomo_disable.html'
+    success_url = reverse_lazy('matomo-home')
+
+    def form_valid(self, form):
+        variable = get_variable('cnil_compliant_visits_tracking_js')
+        variable.value = ''
+        variable.save()
+        variable = get_variable('matomo_enable')
+        variable.value = 'false'
+        variable.save()
+        variable = get_variable('matomo_mode')
+        variable.value = ''
+        variable.save()
+        return super(DisableView, self).form_valid(form)
+
+disable = DisableView.as_view()
diff --git a/hobo/settings.py b/hobo/settings.py
index 0086f68..0ca16d1 100644
--- a/hobo/settings.py
+++ b/hobo/settings.py
@@ -41,6 +41,7 @@ INSTALLED_APPS = (
     'gadjo',
     'hobo.environment',
     'hobo.franceconnect',
+    'hobo.matomo',
     'hobo.profile',
     'hobo.theme',
     'hobo.emails',
diff --git a/hobo/templates/hobo/home.html b/hobo/templates/hobo/home.html
index 3e957f8..2ccce73 100644
--- a/hobo/templates/hobo/home.html
+++ b/hobo/templates/hobo/home.html
@@ -10,6 +10,7 @@
     <li><a href="{% url 'theme-home' %}">{% trans 'Theme' %}</a></li>
     <li><a href="{% url 'emails-home' %}">{% trans 'Emails' %}</a></li>
     <li><a href="{% url 'franceconnect-home' %}">FranceConnect</a></li>
+    <li><a href="{% url 'matomo-home' %}">Matomo</a></li>
     <li><a href="{% url 'environment-home' %}">{% trans 'Services' %}</a></li>
     <li><a href="{% url 'environment-variables' %}">{% trans 'Variables' %}</a></li>
   </ul>
diff --git a/hobo/urls.py b/hobo/urls.py
index d0f0d52..da16e21 100644
--- a/hobo/urls.py
+++ b/hobo/urls.py
@@ -8,6 +8,7 @@ from .views import admin_required, login, login_local, logout, home, health_json
 from .urls_utils import decorated_includes
 from .environment.urls import urlpatterns as environment_urls
 from .franceconnect.urls import urlpatterns as franceconnect_urls
+from .matomo.urls import urlpatterns as matomo_urls
 from .profile.urls import urlpatterns as profile_urls
 from .theme.urls import urlpatterns as theme_urls
 from .emails.urls import urlpatterns as emails_urls
@@ -20,6 +21,8 @@ urlpatterns = [
                                              include(profile_urls))),
     url(r'^franceconnect/',
         decorated_includes(admin_required, include(franceconnect_urls))),
+    url(r'^matomo/',
+        decorated_includes(admin_required, include(matomo_urls))),
     url(r'^theme/', decorated_includes(admin_required,
                                              include(theme_urls))),
     url(r'^emails/', decorated_includes(admin_required, include(emails_urls))),
-- 
2.20.1

