Skip to content

HonzaSaibic/django-fperms-iscore

This branch is 1 commit ahead of skip-pay/django-fperms-iscore:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

94ddec3 · Sep 19, 2024

History

74 Commits
Jan 19, 2023
Apr 17, 2024
Jan 17, 2022
Apr 19, 2024
Sep 19, 2024
Mar 15, 2018
Dec 15, 2021
Sep 19, 2024
Mar 15, 2018
Mar 20, 2018
Nov 3, 2020
Mar 15, 2018
Aug 13, 2019
Mar 20, 2018
Jan 19, 2023
Mar 15, 2018
Apr 27, 2018
Apr 19, 2024

Repository files navigation

django-fperms-iscore

https://travis-ci.org/Formulka/django-fperms-iscore.svg?branch=master

Perms for iscore library

Documentation

The full documentation is at https://django-perms-iscore.readthedocs.io.

Quickstart

Install django-fperms-iscore:

pip install skip-django-fperms-iscore

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'fperms_iscore.apps.FPermsConfig',
    ...
)

It includes all the basic permissions from http://github.com/formulka/django-fperms and adds a new type:

  • core: for iscore specific resources

Usage

A superuser has for all intents and purposes permission to do everything. For regular users you can assign permissions directly or via a user group.

Creating a new permission:

You can create a new permission directly via its model or via a specially formated string:

from fperms_iscore import enums
from fperms_iscore.models import IsCorePerm

IsCorePerm.objects.create(
    type=enums.PERM_TYPE_CORE,
    codename='create',
    core='issue_tracker.IssueIsCore',
)
IsCorePerm.objects.create_from_str('core.issue_tracker.IssueIsCore.create')

Assigning a permission:

You can assign existing permission via the custom perms manager available for both User (including custom ones) and Group models. You can add single permission or multiple both directly via its instance or using the formated string:

from django.auth.models import User, Group

from fperms_iscore.models import IsCorePerm

perm = IsCorePerm.objects.create_from_str('core.issue_tracker.IssueIsCore.create')

user = User.objects.get(pk=1)
user.perms.add_perm(perm)

group = Group.objects.get(pk=1)
group.perms.add_perm('core.issue_tracker.IssueIsCore.create')

By default if said permission does not exist, it will raise an exception. You can override this behavior by setting PERM_AUTO_CREATE variable in your project settings to True, assigning a permission will then create it as well if it does not exist.

Retrieving permission instance:

You can get a permission instance directly from the model or via the string representation.

perm = IsCorePerm.objects.get(
    type=enums.PERM_TYPE_CORE,
    codename='create',
    core='issue_tracker.IssueIsCore',
)
perm = IsCorePerm.objects.get_from_str('core.issue_tracker.IssueIsCore.create')

Checking permission:

You can check whether the user or group has a required permission via has_perm method of the perms manager again using both the permission instance or the string representation.

...
perm = IsCorePerm.objects.create(
    type=enums.PERM_TYPE_CORE,
    codename='create',
    core='issue_tracker.IssueIsCore',
)

assert user.perms.has_perm(perm)
assert user.perms.has_perm('core.issue_tracker.IssueIsCore.create')

New perm type

core

  • permission for iscore specific resources
  • type is defined as fperms_iscore.enums.PERM_TYPE_CORE
  • codename is usually one of the CRUD operations (create, read, update, delete)
  • it requires type, codename and core fields
  • string representation is 'core.<app_label>.<core_name>.<codename>'
...
# equivalent results:
IsCorePerm.objects.create(
    type=enums.PERM_TYPE_CORE,
    codename='create',
    core='issue_tracker.IssueIsCore',
)
IsCorePerm.objects.create_from_str('core.issue_tracker.IssueIsCore.create')

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Credits

Tools used in rendering this package:

About

iscore extension for django-perms

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 90.4%
  • Makefile 9.6%