Package: django-orghierarchy
# django-orghierarchyReusable Django application for hierarchical organizations# Installationpip install django-orghierarchyAdddjango_orghierarchytoINSTALLED_APPSIf you wish to use your own Django model as data source model (the same way you hot-swap your own user model in Django), addDJANGO_ORGHIERARCHY_DATASOURCE_MODEL = ‘yourapp.DataSource’to your project settings. Otherwise, the barebones Django modelDataSourceat django_orghierarchy/models.py is used, it contains the fields a data source modelmustcontain to be used with django-orghierarchy. Similarly, django-orghierarchy links to your user model, soAUTH_USER_MODELmust be defined in Django project settings as always.Run migrations`bash python manage.py migrate django_orghierarchy `# UsageTheOrganizationclass is the main feature of django-orghierarchy. We use [django-mptt](https://github.com/django-mptt/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 (normaloraffiliated), a place in a forest of organization trees, and possibly areplacement 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 haveadmin_usersandregular_users, which are linked to your Django user model. Also, an organization may havesub_organizationsandaffiliated_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_organizationandchange_organization_regular_users. These permissions are for addingregular usersandaffiliated organizationsin Django-admin, and creatingreplacementlinks, without being allowed to touch theadmin usersor the existing organization hierarchy.Affiliatedorganizations usually have more limited rights thannormalorganizations 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](https://github.com/City-of-Helsinki/linkedevents/blob/master/events/permissions.py) and [Linkedevents user model](https://github.com/City-of-Helsinki/linkedevents/blob/master/helevents/models.py). The user rights model is originally specified [here](https://github.com/City-of-Helsinki/linkedevents/issues/235).You may import an existing organization hierarchy from a REST API corresponding to the [6Aika Paatos decisionmaking API specification](https://github.com/6aika/api-paatos), based on the [Popolo project](http://www.popoloproject.com/), with the included importer, for example:`bash 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:`bash python manage.py import_organizations"https://api.hel.fi/paatos/v1/organization/"-soriginal_id:imported_id `Otherwise, the data source id in the original API is used for the imported organizations (helsinkiin the Helsinki API).# DevelopmentInstall requirements.`bash # Package requirements pip install-e. # Development requirements pip install-rrequirements.txt `## Tests### Unit testsRun the tests.`bash pytest `Run the tests with coverage report.`bash pytest--cov-reporthtml--cov. `Open htmlcov/index.html for the coverage report.### Running tests against multiple environmentsYou can run the tests against multiple environments by using [tox](https://tox.readthedocs.io/en/latest/).
Installtoxglobally and run:`bash tox `Use the-foption to specify target environments.`bash # Target only Python 3.9 environments tox-fpy39 # Target Python 3.9 and Django 4.2 tox-fpy39 django42 `### Integration testsWe 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.`bash pytest-mcustom_ds--ds=tests.test_app.settings_custom_dspytest-mcustom_pk_ds--ds=tests.test_app.settings_custom_pk_ds`## Code formatThis project uses
[black](https://github.com/ambv/black),
[flake8](https://github.com/pycqa/flake8) and
[isort](https://github.com/timothycrosley/isort)
for code formatting and quality checking. Project follows the basic
black config, without any modifications.Basicblackcommands:To letblackdo its magic:black .To see which filesblackwould change:black –check .[pre-commit](https://pre-commit.com/) can be used to install and
run all the formatting tools as git hooks automatically before a
commit.## Git blame ignore refsProject includes a.git-blame-ignore-revsfile for ignoring certain commits fromgit blame.
This can be useful for ignoring e.g. formatting commits, so that it is more clear fromgit blamewhere the actual code change came from. Configure your git to use it for this project with the
following command:`shell git config blame.ignoreRevsFile.git-blame-ignore-revs`