Skip to main content

OpenWISP 2 Utilities

Project description

CI build status Test coverage Dependency monitoring pypi downloads support chat code style: black

Python and Django functions, classes and settings re-used across different OpenWISP modules, stored here with the aim of avoiding code duplication and ease maintenance.

Don’t repeat yourself!

Current features

Install stable version from pypi

Install from pypi:

pip install openwisp-utils

# install optional dependencies for REST framework
pip install openwisp-utils[rest]

# install optional dependencies for tests (flake8, black and isort)
pip install openwisp-utils[qa]

# or install everything
pip install openwisp-utils[rest,qa]

Install development version

Install tarball:

pip install

Alternatively you can install via pip using git:

pip install -e git+git://

Using the admin_theme

The admin theme requires Django >= 2.2..

Add openwisp_utils.admin_theme to INSTALLED_APPS in


    'openwisp_utils.admin_theme',    # <----- add this

    # admin

Using DependencyLoader and DependencyFinder

Add the list of all packages extended to EXTENDED_APPS in

For example, if you’ve extended django_x509:

EXTENDED_APPS = ['django_x509']


This is a static finder which looks for static files in the static directory of the apps listed in settings.EXTENDED_APPS.

Add openwisp_utils.staticfiles.DependencyFinder to STATICFILES_FINDERS in

    'openwisp_utils.staticfiles.DependencyFinder',    # <----- add this


This is a template loader which looks for templates in the templates directory of the apps listed in settings.EXTENDED_APPS.

Add openwisp_utils.loaders.DependencyLoader to template loaders in as shown below.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'loaders': [
                # ... other loaders ...
                'openwisp_utils.loaders.DependencyLoader',    # <----- add this
            'context_processors': [
                # ... omitted ...

Supplying custom CSS and JS for the admin theme

Add openwisp_utils.admin_theme.context_processor.admin_theme_settings to template context_processors in as shown below. This will allow to set OPENWISP_ADMIN_THEME_LINKS and OPENWISP_ADMIN_THEME_JS settings to provide CSS and JS files to customise admin theme.

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'loaders': [
                # ... omitted ...
            'context_processors': [
                # ... other context processors ...
                'openwisp_utils.admin_theme.context_processor.admin_theme_settings'    # <----- add this

OpenWISP Dashboard

The admin_theme sub app of this package provides an admin dashboard for OpenWISP which can be manipulated with the functions described in the next sections.

Example 1, monitoring:

Example 2, controller:


Allows including a specific django template in the OpenWISP dashboard.

It is designed to allow the inclusion of the geographic map shipped by OpenWISP Monitoring but can be used to include any custom element in the dashboard.

Note: templates are loaded before charts.


register_dashboard_template(position, config)




(int) The position of the template.


(dict) The configuration of the template.


optional (dict) Extra configuration you want to pass to custom template.

Following properties can be configured for each template config:




(str) Path to pass to the template loader.


(tuple) List of CSS files to load in the HTML page.


(tuple) List of Javascript files to load in the HTML page.

Code example:

      from openwisp_utils.admin_theme import register_dashboard_template

        'template': 'admin/dashboard/device_map.html',
        'css': (
        'js': (
        'optional_variable': 'any_valid_value',

It is recommended to register dashboard templates from the ready method of the AppConfig of the app where the templates are defined.


This function can be used to remove a template from the dashboard.






(str) The name of the template to remove.

Code example:

from openwisp_utils.admin_theme import unregister_dashboard_template


Note: an ImproperlyConfigured exception is raised the specified dashboard template is not registered.


Adds a chart to the OpenWISP dashboard.

At the moment only pie charts are supported.

The code works by defining the type of query which will be executed, and optionally, how the returned values have to be colored and labeled.


register_dashboard_chart(position, config)




(int) Position of the chart.


(dict) Configuration of chart.

Following properties can be configured for each chart config:




It is a required property in form of dict containing following properties:




(str) Chart title shown in the user interface.


(str) App label of the model that will be used to query the database.


(str) Name of the model that will be used to query the database.


(str) The property which will be used to group values.


Alternative to group_by, dict used for more complex queries.


Alternative to group_by, dict used for more complex queries.


An optional dict which can be used to define colors for each distinct value shown in the pie charts.


An optional dict which can be used to define translatable strings for each distinct value shown in the pie charts. Can be used also to provide fallback human readable values for raw values stored in the database which would be otherwise hard to understand for the user.


An optional dict which can be used when using aggregate and annotate in query_params to define the link that will be generated to filter results (pie charts are clickable and clicking on a portion of it will show the filtered results).

Code example:

    from openwisp_utils.admin_theme import register_dashboard_chart

        'query_params': {
            'name': 'Operator Project Distribution',
            'app_label': 'test_project',
            'model': 'operator',
            'group_by': 'project__name',
        'colors': {'Utils': 'red', 'User': 'orange'},

For real world examples, look at the code of OpenWISP Controller and OpenWISP Monitoring.

Note: an ImproperlyConfigured exception is raised if a dashboard element is already registered at same position.

It is recommended to register dashboard charts from the ready method of the AppConfig of the app where the models are defined. Checkout of the test_project for reference.


This function can used to remove a chart from the dashboard.






(str) The name of the chart to remove.

Code example:

from openwisp_utils.admin_theme import unregister_dashboard_chart

unregister_dashboard_chart('Operator Project Distribution')

Note: an ImproperlyConfigured exception is raised the specified dashboard chart is not registered.

Admin filters

The admin_theme sub app provides an improved UI for the changelist filter which occupies less space compared to the original implementation in django: filters are displayed horizontally on the top (instead of vertically on the side) and filter options are hidden in dropdown menus which are expanded once clicked.

Multiple filters can be applied at same time with the help of “apply filter” button. This button is only visible when total number of filters is greater than 4. When filters in use are less or equal to 4 the “apply filter” button is not visible and filters work like in the original django implementation (as soon as a filter option is selected the filter is applied and the page is reloaded).

Model utilities


Model class which provides a UUID4 primary key.


Model class inheriting UUIDModel which provides two additional fields:

  • created

  • modified

Which use respectively AutoCreatedField, AutoLastModifiedField from model_utils.fields (self-updating fields providing the creation date-time and the last modified date-time).


A model field whic provides a random key or token, widely used across openwisp modules.

Admin utilities


Admin mixin which adds two readonly fields created and modified.

This is an admin mixin for models inheriting TimeStampedEditableModel which adds the fields created and modified to the database.


A read-only ModelAdmin base class.

Will include the id field by default, which can be excluded by supplying the exclude attribute, eg:

from openwisp_utils.admin import ReadOnlyAdmin

class PostAuthReadOnlyAdmin(ReadOnlyAdmin):
    exclude = ['id']


A mixin designed for inline items and model forms, ensures the item is created even if the default values are unchanged.

Without this, when creating new objects, inline items won’t be saved unless users change the default values.


An admin class that provides the UUID of the object as a read-only input field (to make it easy and quick to copy/paste).


An admin class that provides an URL as a read-only input field (to make it easy and quick to copy/paste).


A stacked inline admin class that displays a help text for entire inline object. Following is an example:

from openwisp_utils.admin import HelpTextStackedInline

class SubnetDivisionRuleInlineAdmin(
    MultitenantAdminMixin, TimeReadonlyAdminMixin, HelpTextStackedInline
    model = Model
    # It is required to set "help_text" attribute
    help_text = {
        # (required) Help text to display
        'text': _(
            'Please keep in mind that once the subnet division rule is created '
            'and used, changing "Size" and "Number of Subnets" and decreasing '
            '"Number of IPs" will not be possible.'
        # (optional) You can provide a link to documentation for user reference
        'documentation_url': (
        # (optional) Icon to be shown along with help text. By default it uses
        # "/static/admin/img/icon-alert.svg"
        'image_url': '/static/admin/img/icon-alert.svg'


The admin_theme sub app of this package provides an input filter that can be used in changelist page to filter UUIDField or CharField.

Code example:

from django.contrib import admin
from openwisp_utils.admin_theme.filters import InputFilter
from my_app.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_filter = [
        ('my_field', InputFilter),

By default InputFilter use exact lookup to filter items which matches to the value being searched by the user. But this behavior can be changed by modifying InputFilter as following:

from django.contrib import admin
from openwisp_utils.admin_theme.filters import InputFilter
from my_app.models import MyModel

class MyInputFilter(InputFilter):
    lookup = 'icontains'

class MyModelAdmin(admin.ModelAdmin):
    list_filter = [
        ('my_field', MyInputFilter),

To know about other lookups that can be used please check Django Lookup API Reference


A stripped down version of openwisp_utils.admin_theme.filters.InputFilter that provides flexibility to customize filtering. It can be used to filter objects using indirectly related fields.

The derived filter class should define the queryset method as shown in following example:

from django.contrib import admin
from openwisp_utils.admin_theme.filters import SimpleInputFilter
from my_app.models import MyModel

class MyInputFilter(SimpleInputFilter):
    parameter_name = 'shelf'
    title = _('Shelf')

    def queryset(self, request, queryset):
        if self.value() is not None:
            return queryset.filter(name__icontains=self.value())

class MyModelAdmin(admin.ModelAdmin):
    list_filter = [

Code utilities


Generates an random string of 32 characters.


Returns a new dict which is the result of the merge of the two dictionaries, all elements are deep-copied to avoid modifying the original data structures.


from openwisp_utils.utils import deep_merge_dicts

mergd_dict = deep_merge_dicts(dict1, dict2)


If the program is being executed during automated tests the value supplied in the test argument will be returned, otherwise the one supplied in the value argument is returned.

from openwisp_utils.utils import default_or_test

THROTTLE_RATE = getattr(
    default_or_test(value='20/day', test=None),


default colors: ['white_bold', 'green_bold', 'yellow_bold', 'red_bold']

If you want to print a string in Red Bold, you can do it as below.

from openwisp_utils.utils import print_color

print_color('This is the printed in Red Bold', color_name='red_bold')

You may also provide the end arguement similar to built-in print method.


Extends collections.SortedDict and implements logic to sort inserted items based on key value. Sorting is done at insert operation which incurs memory space overhead.


A custom celery task class that sets hard and soft time limits of celery tasks using OPENWISP_CELERY_HARD_TIME_LIMIT and OPENWISP_CELERY_SOFT_TIME_LIMIT settings respectively.


from celery import shared_task

from openwisp_utils.tasks import OpenwispCeleryTask

def your_celery_task():

Note: This task class should be used for regular background tasks but not for complex background tasks which can take a long time to execute (eg: firmware upgrades, network operations with retry mechanisms).

Storage utilities

A static storage backend for compression inheriting from django-compress-staticfiles’s CompressStaticFilesStorage class.

Adds support for excluding file types using OPENWISP_STATICFILES_VERSIONED_EXCLUDE setting.

To use point STATICFILES_STORAGE to in


Admin Theme utilities

This function allows sending email in both plain text and HTML version (using the template and logo that can be customised using OPENWISP_EMAIL_TEMPLATE and OPENWISP_EMAIL_LOGO respectively).

In case the HTML version if not needed it may be disabled by setting OPENWISP_HTML_EMAIL to False.


send_email(subject, body_text, body_html, recipients)




(str) The subject of the email template.


(str) The body of the text message to be emailed.


(str) The body of the html template to be emailed.


(list) The list of recipients to send the mail to.


optional (dict) Extra context which is passed to the template. The dictionary keys call_to_action_text and call_to_action_url can be passed to show a call to action button. Similarly, footer can be passed to add a footer.

Note: Data passed in body should be validated and user supplied data should not be sent directly to the function.

REST API utilities


A model serializer which calls the model instance full_clean().


If you’re creating an OpenWISP module which provides a REST API built with Django REST Framework, chances is that you may need to define some default settings to control its throttling or other aspects.

Here’s how to easily do it:

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from openwisp_utils.api.apps import ApiAppConfig

class MyModuleConfig(ApiAppConfig):
    name = 'my_openwisp_module'
    label = 'my_module'
    verbose_name = _('My OpenWISP Module')

    # assumes API is enabled by default
    API_ENABLED = getattr(settings, 'MY_OPENWISP_MODULE_API_ENABLED', True)
    # set throttling rates for your module here
        'DEFAULT_THROTTLE_RATES': {'my_module': '400/hour'},

Every openwisp module which has an API should use this class to configure its own default settings, which will be merged with the settings of the other modules.

Test utilities


This method can be used to mock a signal call inorder to easily verify that the signal has been called.

Usage example as a context-manager:

from openwisp_utils.tests import catch_signal

with catch_signal(openwisp_signal) as handler:


This class extends the default test runner provided by Django and logs the time spent by each test, making it easier to spot slow tests by highlighting time taken by it in yellow (time shall be highlighted in red if it crosses the second threshold).

By default tests are considered slow if they take more than 0.3 seconds but you can control this with OPENWISP_SLOW_TEST_THRESHOLD.

In order to switch to this test runner you have set the following in your

TEST_RUNNER = 'openwisp_utils.tests.TimeLoggingTestRunner'


This decorator can be used to capture standard output produced by tests, either to silence it or to write assertions.

Example usage:

from openwisp_utils.tests import capture_stdout

def test_something(self):
    function_generating_output() # pseudo code

def test_something_again(self, captured_ouput):
    # pseudo code
    # now you can create assertions on the captured output
    self.assertIn('expected stdout', captured_ouput.getvalue())
    # if there are more than one assertions, clear the captured output first
    # you can create new assertion now
    self.assertIn('another output', captured_ouput.getvalue())


  • If assertions need to be made on the captured output, an additional argument (in the example above is named captured_output) can be passed as an argument to the decorated test method, alternatively it can be omitted.

  • A StingIO instance is used for capturing output by default but if needed it’s possible to pass a custom StringIO instance to the decorator function.


Equivalent to capture_stdout, but for standard error.

Example usage:

from openwisp_utils.tests import capture_stderr

def test_error(self):
    function_generating_error() # pseudo code

def test_error_again(self, captured_error):
    # pseudo code
    # now you can create assertions on captured error
    self.assertIn('expected error', captured_error.getvalue())
    # if there are more than one assertions, clear the captured error first
    # you can create new assertion now
    self.assertIn('another expected error', captured_error.getvalue())


Equivalent to capture_stdout and capture_stderr, but captures both types of output (standard output and standard error).

Example usage:

from openwisp_utils.tests import capture_any_output

def test_something_out(self):
    function_generating_output() # pseudo code

def test_out_again(self, captured_output, captured_error):
    # pseudo code
    # now you can create assertions on captured error
    self.assertIn('expected stdout', captured_output.getvalue())
    self.assertIn('expected stderr', captured_error.getvalue())


This mixin overrides the assertNumQueries assertion from the django test case to run in a subTest so that the query check does not block the whole test if it fails.

Example usage:

from django.test import TestCase
from openwisp_utils.tests import AssertNumQueriesSubTestMixin

class MyTest(AssertNumQueriesSubTestMixin, TestCase):
    def my_test(self):
        with self.assertNumQueries(2):

        # the assertion above will fail but this line will be executed
        print('This will be printed anyway.')

Quality Assurance Checks

This package contains some common QA checks that are used in the automated builds of different OpenWISP modules.


This shell script automatically formats Python and CSS code according to the OpenWISP coding style conventions.

It runs isort and black to format python code (these two dependencies are required and installed automatically when running pip install openwisp-utils[qa]).

The stylelint and jshint programs are used to perform style checks on CSS and JS code respectively, but they are optional: if stylelint and/or jshint are not installed, the check(s) will be skipped.


Shell script to run the following quality assurance checks:

  • checkmigrations

  • checkcommit

  • checkendline

  • checkpendingmigrations

  • checkrst

  • flake8 - Python code linter

  • isort - Sorts python imports alphabetically, and seperated into sections

  • black - Formats python code using a common standard

  • csslinter - Formats and checks CSS code using stylelint common standard

  • jslinter - Checks Javascript code using jshint common standard

If a check requires a flag, it can be passed forward in the same way.

Usage example:

openwisp-qa-check --migration-path <path> --message <commit-message>

Any unneeded checks can be skipped by passing --skip-<check-name>

Usage example:

openwisp-qa-check --skip-isort

For backward compatibility csslinter and jslinter are skipped by default. To run them in checks pass arguements in this way.

Usage example:

# To activate csslinter
openwisp-qa-check --csslinter

# To activate jslinter
openwisp-qa-check --jslinter

You can do multiple checkmigrations by passing the arguments with space-delimited string.

For example, this multiple checkmigrations:

checkmigrations --migrations-to-ignore 3 \
        --migration-path ./openwisp_users/migrations/ || exit 1

checkmigrations --migrations-to-ignore 2 \
        --migration-path ./tests/testapp/migrations/ || exit 1

Can be changed with:

openwisp-qa-check --migrations-to-ignore "3 2" \
        --migration-path "./openwisp_users/migrations/ ./tests/testapp/migrations/"


Ensures the latest migrations created have a human readable name.

We want to avoid having many migrations named like

This way we can reconstruct the evolution of our database schemas faster, with less efforts and hence less costs.

Usage example:

checkmigrations --migration-path ./django_freeradius/migrations/


Ensures the last commit message follows our commit message style guidelines.

We want to keep the commit log readable, consistent and easy to scan in order to make it easy to analyze the history of our modules, which is also a very important activity when performing maintenance.

Usage example:

checkcommit --message "$(git log --format=%B -n 1)"

If, for some reason, you wish to skip this QA check for a specific commit message you can add #noqa to the end of your commit message.

Usage example:

[qa] Improved #20

Simulation of a special unplanned case


Ensures that a blank line is kept at the end of each file.


Ensures there django migrations are up to date and no new migrations need to be created.

It accepts an optional --migration-module flag indicating the django app name that should be passed to ./ makemigrations, eg: ./ makemigrations $MIGRATION_MODULE.


Checks the syntax of all ReStructuredText files to ensure they can be published on pypi or using python-sphinx.



default: openwisp_utils.admin_theme.admin.OpenwispAdminSite

If you need to use a customized admin site class, you can use this setting.


default: OpenWISP Admin

Title value used in the <title> HTML tag of the admin site.


default: OpenWISP

Heading text used in the main <h1> HTML tag (the logo) of the admin site.


default: Network administration

Title shown to users in the index page of the admin site.


default: True

When True, enables the OpenWISP Dashboard. Upon login, the user will be greeted with the dashboard instead of the default Django admin index page.


default: []

Allows to pass a list of strings representing URLs of custom JS files to load.

Example usage:



default: True

Whether the OpenAPI documentation is enabled.

When enabled, you can view the available documentation using the Swagger endpoint at /api/v1/docs/.

You also need to add the following url to your project

urlpatterns += [
    url(r'^api/v1/', include('openwisp_utils.api.urls')),



    'title': 'OpenWISP API',
    'default_version': 'v1',
    'description': 'OpenWISP REST API',

Define OpenAPI general information. NOTE: This setting requires OPENWISP_API_DOCS = True to take effect.

For more information about optional parameters check the drf-yasg documentation.


default: [0.3, 1] (seconds)

It can be used to change the thresholds used by TimeLoggingTestRunner to detect slow tests (0.3s by default) and highlight the slowest ones (1s by default) amongst them.


default: ['leaflet/*/*.png']

Allows to pass a list of Unix shell-style wildcards for files to be excluded by CompressStaticFilesStorage.

By default Leaflet PNGs have been excluded to avoid bugs like openwisp/ansible-openwisp2#232.

Example usage:







If True, an HTML themed version of the email can be sent using the send_email function.






This setting allows to change the django template used for sending emails with the send_email function. It is recommended to extend the default email template as in the example below.

{% extends 'openwisp_utils/email_template.html' %}
{% block styles %}
{{ block.super }}
  .background {
    height: 100%;
    background: linear-gradient(to bottom, #8ccbbe 50%, #3797a4 50%);
    background-repeat: no-repeat;
    background-attachment: fixed;
    padding: 50px;

  .mail-header {
    background-color: #3797a4;
    color: white;
{% endblock styles %}

Similarly, you can customize the HTML of the template by overriding the body block. See email_template.html for reference implementation.





OpenWISP logo

This setting allows to change the logo which is displayed in HTML version of the email.

Note: Provide a URL which points to the logo on your own web server. Ensure that the URL provided is publicly accessible from the internet. Otherwise, the logo may not be displayed in the email. Please also note that SVG images do not get processed by some email clients like Gmail so it is recommended to use PNG images.





30 (in seconds)

Sets the soft time limit for celery tasks using OpenwispCeleryTask.





120 (in seconds)

Sets the hard time limit for celery tasks using OpenwispCeleryTask.

Installing for development

Install the system dependencies:

sudo apt-get install sqlite3 libsqlite3-dev

# For running E2E Selenium tests
sudo apt install chromium

Install your forked repo:

git clone git://<your_fork>/openwisp-utils
cd openwisp-utils/
pip install -e .[qa,rest]

Install test requirements:

pip install -r requirements-test.txt

Install node dependencies used for testing:

npm install -g stylelint jshint

Set up the pre-push hook to run tests and QA checks automatically right before the git push action, so that if anything fails the push operation will be aborted:

openwisp-pre-push-hook --install

Install WebDriver for Chromium for your browser version from and Extract chromedriver to one of directories from your $PATH (example: ~/.local/bin/).

Create database:

cd tests/
./ migrate
./ createsuperuser

Run development server:

cd tests/
./ runserver

You can access the admin interface of the test project at

Run tests with:

./ --parallel


Please refer to the OpenWISP contributing guidelines.


See OpenWISP Support Channels.





Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openwisp-utils-1.0.5.tar.gz (529.1 kB view hashes)

Uploaded Source

Built Distribution

openwisp_utils-1.0.5-py2.py3-none-any.whl (527.7 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page