Skip to main content

OpenWISP 2 Utilities

Project description

https://travis-ci.org/openwisp/openwisp-utils.svg?branch=master https://coveralls.io/repos/github/openwisp/openwisp-utils/badge.svg Requirements Status https://badge.fury.io/py/openwisp-utils.svg

Python and Django utilities shared between different OpenWISP modules



Current utilities

  • Customized admin theme for OpenWISP modules

  • Multitenant admin interface and testing mixins

  • TimeStamped models and mixins which add self-updating created and modified fields.

  • DependencyLoader: template loader which looks in the templates dir of all django-apps listed in EXTENDED_APPS

  • DependencyFinder: finds static files of django-apps listed in EXTENDED_APPS

Project goals

  • Minimize code duplication among OpenWISP modules

Install stable version from pypi

Install from pypi:

pip install openwisp-utils

Install development version

Install tarball:

pip install https://github.com/openwisp/openwisp-utils/tarball/master

Alternatively you can install via pip using git:

pip install -e git+git://github.com/openwisp/openwisp-utils#egg=openwisp-utils

If you want to contribute, install your cloned fork:

git clone git@github.com:<your_fork>/openwisp-utils.git
cd openwisp-utils
python setup.py develop

Using the utilities in OpenWISP modules

INSTALLED_APPS in settings.py should look like the following if you want to use all the utilities

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # customized admin theme
    'openwisp_utils.admin_theme',
    # all-auth
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'django_extensions',
    # openwisp2 modules
    'openwisp_users',
    # admin
    'django.contrib.admin',
]

Adding admin theme

For using the customized admin theme,

  • Make sure you’ve added openwisp_utils.admin_theme to INSTALLED_APPS in settings.py

  • Add the following into your urls.py file which contains admin urls.

from django.conf.urls import include, url

from openwisp_utils.admin_theme.admin import admin, openwisp_admin

openwisp_admin()

urlpatterns = [
    # other url patterns
    url(r'^admin/', include(admin.site.urls)),
]

Using Multitenant and other admin mixins

These are mixins which make a ModelAdmin class multitenant: users will see only the objects related to the organizations they are associated with.

  • MultitenantAdminMixin: inheriting this class would make a ModelAdmin class multitenant. set multitenant_shared_relations to the list of parameters you wish to have only organization specific options.

  • MultitenantObjectFilter: Admin filter that shows only organizations the current user is associated with in its available choices.

  • MultitenantObjectFilter: Admin filter that shows only objects of organizations the current user is associated with.

  • TimeReadonlyAdminMixin: 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.

from django.contrib import admin

from openwisp_utils.admin import (MultitenantAdminMixin,
                                  MultitenantObjectFilter,
                                  MultitenantOrgFilter,
                                  TimeReadonlyAdminMixin)

from .models import Book, Shelf


class BaseAdmin(MultitenantAdminMixin, TimeReadonlyAdminMixin, admin.ModelAdmin):
    pass


class ShelfAdmin(BaseAdmin):
    list_display = ['name', 'organization']
    list_filter = [('organization', MultitenantOrgFilter)]
    fields = ['name', 'organization', 'created', 'modified']


class BookAdmin(BaseAdmin):
    list_display = ['name', 'author', 'organization', 'shelf']
    list_filter = [('organization', MultitenantOrgFilter),
                   ('shelf', MultitenantObjectFilter)]
    fields = ['name', 'author', 'organization', 'shelf', 'created', 'modified']
    multitenant_shared_relations = ['shelf']

Using DependencyLoader and DependencyFinder

Add the list of all packages extended to EXTENDED_APPS in settings.py. If you’ve extended django_netjsonconfig and django_x509:

EXTENDED_APPS = ['django_netjsonconfig', 'django_x509']
  • DependencyFinder: Add openwisp_utils.staticfiles.DependencyFinder to STATICFILES_FINDERS in settings.py.

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'openwisp_utils.staticfiles.DependencyFinder',
]
  • DependencyLoader: Add openwisp_utils.staticfiles.DependencyFinder to TEMPLATES_LOADERS in settings.py or as shown below.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'openwisp_utils.loaders.DependencyLoader',
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Installing for development

Install sqlite:

sudo apt-get install sqlite3 libsqlite3-dev

Install your forked repo:

git clone git://github.com/<your_fork>/openwisp-utils
cd openwisp-utils/
python setup.py develop

Install test requirements:

pip install -r requirements-test.txt

Create database:

cd tests/
./manage.py migrate
./manage.py createsuperuser

Set EMAIL_PORT in settings.py to a port number (eg: 1025):

EMAIL_PORT = '1025'

Launch development server and SMTP deubgging server:

./manage.py runserver
# open another session and run
python -m smtpd -n -c DebuggingServer localhost:1025

You can access the admin interface of the test project at http://127.0.0.1:8000/admin/.

Run tests with:

./runtests.py

Contributing

  1. Announce your intentions in the OpenWISP Mailing List and open relavant issues using the issue tracker

  2. Fork this repo and install the project following the instructions

  3. Follow PEP8, Style Guide for Python Code

  4. Write code and corresponding tests

  5. Ensure that all tests pass and the test coverage does not decrease

  6. Document your changes

  7. Send a pull request

Changelog

See CHANGES.

License

See LICENSE.

Support

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-0.1.0a0.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

openwisp_utils-0.1.0a0-py2.py3-none-any.whl (56.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file openwisp-utils-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for openwisp-utils-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 41f3f6f2f2a45546b809f418211681c8845f342eed800630bf3aec04c17760cc
MD5 2b17dba55ee5328890dae66dc076394e
BLAKE2b-256 02fe5b571c19d5c7b1251b696c59e23f79076fdc4027cb617adb706ee8002a3c

See more details on using hashes here.

File details

Details for the file openwisp_utils-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for openwisp_utils-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9838390169a8d26f89a17a8d1f899f20ea3999dc8bf6e89d4a30cab4b35e7873
MD5 b35669f48470451398431448fe3d9a38
BLAKE2b-256 03f0422ddf5b0ae09c6fd7e07cf05079234b42bf040544112be4a2c876f5446a

See more details on using hashes here.

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