Skip to main content

Reusable Django application for hierarchical organizations.

Project description

django-orghierarchy

Reusable Django application for hierarchical organizations

Installation

  1. pip install django-orghierarchy

  2. Add django_orghierarchy to INSTALLED_APPS

  3. If you wish to use your own Django model as data source model (the same way you hot-swap your own user model in Django), add DJANGO_ORGHIERARCHY_DATASOURCE_MODEL = 'yourapp.DataSource' to your project settings. Otherwise, the barebones Django model DataSource at django_orghierarchy/models.py is used, it contains the fields a data source model must contain to be used with django-orghierarchy. Similarly, django-orghierarchy links to your user model, so AUTH_USER_MODEL must be defined in Django project settings as always.

  4. Run migrations

python manage.py migrate django_orghierarchy

Usage

The Organization class is the main feature of django-orghierarchy. We use django-mptt to implement an organization hierarchy that can be as deep as you wish. Each organization has a name, a data source (referring to the system the organization data is from), origin_id (referring to organization id in the original data source), founding date and dissolution date, status (normal or affiliated), a place in a forest of organization trees, and possibly a replacement organization, which means a link to any other organization across the trees (making the forest strictly a directed graph, not a bunch of trees). Replacement organization allows linking dissolved organization structures to new ones so that old user rights are automatically transferred across the hierarchy to the replacing organization.

Each organization may have admin_users and regular_users, which are linked to your Django user model. Also, an organization may have sub_organizations and affiliated_organizations. You may have any number of top level organizations. Also, some extra user permissions are defined, i.e. add_affiliated_organization, change_affiliated_organization, delete_affiliated_organization, replace_organization and change_organization_regular_users. These permissions are for adding regular users and affiliated organizations in Django-admin, and creating replacement links, without being allowed to touch the admin users or the existing organization hierarchy. Affiliated organizations usually have more limited rights than normal organizations within the hierarchy; they are meant for external organizations you collaborate with and wish to grant limited rights to.

Your desired user rights and permissions for each user group in each level of the organization depend on your application details, so you should implement your own user rights checks depending on your needs. You may e.g. create a user model permissions mixin that uses information on the user organization, as done in Linkedevents permissions and Linkedevents user model. The user rights model is originally specified here.

You may import an existing organization hierarchy from a REST API corresponding to the 6Aika Paatos decisionmaking API specification, based on the Popolo project, with the included importer, for example:

python manage.py import_organizations "https://api.hel.fi/paatos/v1/organization/"

You may give the organization data source a specific id to correspond to your own data source model ids in your project:

python manage.py import_organizations "https://api.hel.fi/paatos/v1/organization/" -s original_id:imported_id

Otherwise, the data source id in the original API is used for the imported organizations (helsinki in the Helsinki API).

Development

Install requirements.

# Package requirements
pip install -e .
# Development requirements
pip install -r requirements.txt

Tests

Unit tests

Run the tests.

pytest

Run the tests with coverage report.

pytest --cov-report html --cov .

Open htmlcov/index.html for the coverage report.

Running tests against multiple environments

You can run the tests against multiple environments by using hatch. Install hatch globally and run:

hatch test --all

Use the -py and -i options to target specific environments.

# Target only Python 3.10 environments
hatch test --python 3.10
# Target Python 3.10 and Django 5.2
hatch test --python 3.10 -i django=5.2

Integration tests

We need to provide different settings files for the test as the setting variables for swappable model are only evaluated the first time the module is imported. Integration tests are skipped by default.

Run the integration tests.

pytest -m custom_ds --ds=tests.test_app.settings_custom_ds
pytest -m custom_pk_ds --ds=tests.test_app.settings_custom_pk_ds

Code format

This project uses ruff for code formatting and quality checking. Ruff configuration is to be found in pyproject.toml.

Basic ruff commands:

  • To let ruff do its magic: ruff format and ruff check --fix

pre-commit can be used to install and run all the formatting tools as git hooks automatically before a commit.

Git blame ignore refs

Project includes a .git-blame-ignore-revs file for ignoring certain commits from git blame. This can be useful for ignoring e.g. formatting commits, so that it is more clear from git blame where the actual code change came from. Configure your git to use it for this project with the following command:

git config blame.ignoreRevsFile .git-blame-ignore-revs

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

django_orghierarchy-0.7.0.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_orghierarchy-0.7.0-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file django_orghierarchy-0.7.0.tar.gz.

File metadata

  • Download URL: django_orghierarchy-0.7.0.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_orghierarchy-0.7.0.tar.gz
Algorithm Hash digest
SHA256 8cdaa2c2f6493ce95181f59d3cbb7ba6ad78c07919cb0f0290d98e179d92a8e5
MD5 9096d8c3576ff9fd601519052301ecfd
BLAKE2b-256 f0dd0616e5145cdab1dac5083c7b06e42bff602440910960163e4cdcb59aed63

See more details on using hashes here.

File details

Details for the file django_orghierarchy-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_orghierarchy-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27950555c7e1bf67c021b5941f6b3c561bf48bbed9b40c71011ab15db1ad2a9f
MD5 acc955224ed7001192b27c2f9136c79b
BLAKE2b-256 e43a2bad907aeb3f94bdfa7cc0ad74e99922c940f453eadab4a320f5a33db426

See more details on using hashes here.

Supported by

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