Skip to content

Commit

Permalink
[change] Metric collection: added consent logic to admin UI #372
Browse files Browse the repository at this point in the history
* Added info message to admin UI (only once and only for super users)
* Added a consent form to openwisp-info page
* Changed app name from "openwisp_utils.measurements" to "openwisp_utils.metric_collection"
* Refactored code to reduce verbosity

Closes #372

---------

Co-authored-by: Federico Capoano <f.capoano@openwisp.io>
  • Loading branch information
pandafy and nemesifier committed Apr 23, 2024
1 parent 95c8e9a commit 1d3b00c
Show file tree
Hide file tree
Showing 26 changed files with 689 additions and 256 deletions.
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ Collection of Usage Metrics
---------------------------

The ``openwisp-utils`` module includes an optional
sub-app ``openwisp_utils.measurements``.
sub-app ``openwisp_utils.metric_collection``.

This sub-app allows us to collect of the following information
from OpenWISP instances:
Expand Down Expand Up @@ -1706,8 +1706,9 @@ means to make data-driven decisions while respecting our users' rights and trust
We have taken great care to ensure no
sensitive or personal data is being tracked.

The collection of this data can be disabled at any time by removing
the ``openwisp_utils.measurements`` app from ``INSTALLED_APPS``.
You can opt-out from sharing this data any time from the "System Info" page.
Alternatively, you can also remove the ``openwisp_utils.metric_collection`` app
from ``INSTALLED_APPS``.

Quality Assurance Checks
------------------------
Expand Down
7 changes: 5 additions & 2 deletions openwisp_utils/admin_theme/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from django.shortcuts import render
from django.urls import path
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy
from django.utils.translation import gettext_lazy as _

from ..metric_collection.helper import MetricCollectionAdminSiteHelper
from . import settings as app_settings
from .dashboard import get_dashboard_context
from .system_info import (
Expand All @@ -25,16 +25,18 @@ class OpenwispAdminSite(admin.AdminSite):
# h1 text
site_header = getattr(settings, 'OPENWISP_ADMIN_SITE_HEADER', 'OpenWISP')
# text at the top of the admin index page
index_title = gettext_lazy(
index_title = _(
getattr(settings, 'OPENWISP_ADMIN_INDEX_TITLE', 'Network Administration')
)
enable_nav_sidebar = False
metric_collection = MetricCollectionAdminSiteHelper

def index(self, request, extra_context=None):
if app_settings.ADMIN_DASHBOARD_ENABLED:
context = get_dashboard_context(request)
else:
context = {'dashboard_enabled': False}
self.metric_collection.show_consent_info(request)
return super().index(request, extra_context=context)

def openwisp_info(self, request, *args, **kwargs):
Expand All @@ -45,6 +47,7 @@ def openwisp_info(self, request, *args, **kwargs):
'title': _('System Information'),
'site_title': self.site_title,
}
self.metric_collection.manage_form(request, context)
return render(request, 'admin/openwisp_info.html', context)

def get_urls(self):
Expand Down
10 changes: 10 additions & 0 deletions openwisp_utils/admin_theme/static/admin/css/openwisp-info.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#id_user_consented {
float: left;
margin-right: 8px;
vertical-align: top;
margin-bottom: 0px;
}
#id_metric_collection_consent_form > span.helptext{
display: block;
margin-top: 8px;
}
9 changes: 7 additions & 2 deletions openwisp_utils/admin_theme/static/admin/css/openwisp.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ a.section:link,
a.section:visited,
.module th a,
#main .help a,
#main .helptext a,
fieldset.collapsed a.collapse-toggle,
fieldset a.collapse-toggle {
color: #df5d43;
Expand All @@ -137,7 +138,9 @@ a.section:link:focus,
a.section:visited:focus,
.module th a:focus,
#main .help a:hover,
#main .help a:focus {
#main .help a:focus,
#main .helptext a:focus,
#main .helptext a:focus {
text-decoration: underline;
}
#main-content .module caption a {
Expand Down Expand Up @@ -182,7 +185,9 @@ a:focus {
display: inline-block;
}
#main .form-row .help,
#container #main-content .help {
#main .form-row .helptext,
#container #main-content .help,
#container #main-content .helptext {
font-size: 14px !important;
line-height: 20px;
color: rgba(0, 0, 0, 0.45);
Expand Down
33 changes: 32 additions & 1 deletion openwisp_utils/admin_theme/templates/admin/openwisp_info.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load i18n static %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/openwisp-info.css' %}" />
{% endblock extrastyle %}

{% block content %}
{% if openwisp_version %}
Expand All @@ -15,4 +20,30 @@ <h2>{% trans "OS Information" %}</h2>
<p><strong>{% trans "OS version" %}:</strong> {{ system_info.os_version }}</p>
<p><strong>{% trans "Kernel version" %}:</strong> {{ system_info.kernel_version }}</p>
<p><strong>{% trans "Hardware platform" %}:</strong> {{ system_info.hardware_platform }}</p>

{% block metric_collect_consent %}
{% if metric_collection_installed %}
<h2>Metric collection</h2>
<form method="POST" id="id_metric_collection_consent_form">
{% csrf_token %}
{{ metric_consent_form }}
<p><input type="submit" style="display:none;"></p>
</form>
{% endif %}
{% endblock metric_collect_consent %}
{% endblock content %}

{% block extrahead %}
{{ block.super }}
{% if 'jquery' not in block.super and not media.js %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.init.js' %}"></script>
{% endif %}
{% endblock %}

{% block footer %}
{{ block.super }}
{% if metric_collection_installed %}
<script src="{% static 'admin/js/metric-collection-consent.js' %}"></script>
{% endif %}
{% endblock footer %}
File renamed without changes.
42 changes: 0 additions & 42 deletions openwisp_utils/measurements/migrations/0001_initial.py

This file was deleted.

55 changes: 0 additions & 55 deletions openwisp_utils/measurements/models.py

This file was deleted.

76 changes: 0 additions & 76 deletions openwisp_utils/measurements/tasks.py

This file was deleted.

33 changes: 0 additions & 33 deletions openwisp_utils/measurements/utils.py

This file was deleted.

42 changes: 42 additions & 0 deletions openwisp_utils/metric_collection/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django import forms
from django.forms.utils import ErrorList

from .models import Consent


class ConsentForm(forms.ModelForm):
# This required to override the default label_suffix.
# Otherwise, it will show a trailing colon (:) which we
# don't want here due to formatting of the form.
def __init__(
self,
data=None,
files=None,
auto_id="id_%s",
prefix=None,
initial=None,
error_class=ErrorList,
label_suffix='',
empty_permitted=False,
instance=None,
use_required_attribute=None,
renderer=None,
):
super().__init__(
data,
files,
auto_id,
prefix,
initial,
error_class,
label_suffix,
empty_permitted,
instance,
use_required_attribute,
renderer,
)

class Meta:
model = Consent
widgets = {'user_consented': forms.CheckboxInput(attrs={'class': 'bold'})}
fields = ['user_consented']
Loading

0 comments on commit 1d3b00c

Please sign in to comment.