Giter Site home page Giter Site logo

django-global-permissions's Introduction

Django Global Permissions

Build Status Implementation of permissions not related to models

Quickstart

Install django-global-permissions:

pip install django-global-permissions

Add to the installed apps:

INSTALLED_APPS += (global_permissions,)

If you want co create a permission in the admin interface, then head to the Global Permissions section, then add a permission. Pick a name (which should be a human readable description), a code name (which will be used throughout the your apps), then save it.

Then open the user edit page. Now you can choose the permission you've just created.

Otherwise if you want to create a permission programmatically, just import the GlobalPermission model and create a new permission choosing a name and a codename.

from global_permissions.models import GlobalPermission

GlobalPermission.objects.create(name='My Perm', codename='my_perm')

Putting into action!

Lets say you want to verify if the logged in user can do something (based on a permission). In your view, you can do the following

if user.has_perm('global_permissions.my_perm_codename'):
    pass # do something intersting!
else:
    pass # ops, you're not allowed to do that. Sorry ¯\_(ツ)_/¯

If you cant to verify a permission inside some template, you can do this way:

{% if perms.global_permissions.my_perm_codename %}
    Yay!
{% else %}
    Not so lucky...
{% endif %}

Upgrade

If you're upgrading from version 0.1.x to version 0.2.x, you have to manually update the old contentttype model attribute to the new one. The following script may do the trick:

from django.contrib.contenttypes.models import ContentType

ContentType.objects.filter(name='global_permission', app_label='global_permissions').update(model='globalpermission')

This change is required on django 1.7+ to avoid a prompt asking if you wanna remove staled content types after running a migration.

django-global-permissions's People

Contributors

eduardo-matos avatar mwx23 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.