Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Permission to Django Admin

Last Month i posted question on stackoverflow and on Django-Users group on G+ and on django website too. But i didn't find any answer that can solve my problem. What i want to do is to add new permission named as view in django admin panel, so user can only view data!. I also followed different patches from django website and tried django-databrowse but nothing works as expected. I then finally decide to edit views of auth/admin. Now what i am going to do is to add view permission like:

1. Added 'view' to default permission list

#./contrib/auth/management/init.py
def _get_all_permissions(opts):

    "Returns (codename, name) for all permissions in the given opts."
    perms = []
    for action in ('add', 'change', 'delete', 'view'):

        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))

    return perms + list(opts.permissions)

2. Test the 'view' permission is added to all models

run manage.py syncdb

After this i can assign only view permission to user. Now this view permission must work too. So i am writing this code: in view.py of django-admin

for per in request.user.user_permissions_all():
    print per

This code prints permissions assigned to login user like auth | permission | can view department etc

Now i can get permission type and model name by splitting this sentence. I will get all the model name of application and will match that which data must b visible. This is again not what i really need but can work.

So my question is :

* Is this is what i should do or is there any other way too. I just want a solution that must works as expected. Need Your Assistance *

like image 860
Tameen Malik Avatar asked May 01 '14 14:05

Tameen Malik


People also ask

How do I give permission to user in Django?

If you have a set number of user types, you can create each user type as a group and give the necessary permissions to the group. Then, for every user that is added into the system and into the required group, the permissions are automatically granted to each user.

Is admin user permission Django?

Permissions and Authorization. Django comes with a built-in permissions system. It provides a way to assign permissions to specific users and groups of users. It's used by the Django admin site, but you're welcome to use it in your own code.

How do I add permission to group in Django?

To add the permissions, you can use the user_permissions. add() method.


1 Answers

Adding 'view' permission to default permissions list

Your solution works, but you should really avoid editing source code if possible. There's a few ways to accomplish this within the framework:

1. Add the permission during post_syncdb():

In a file under your_app/management/

from django.db.models.signals import post_syncdb
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission

def add_view_permissions(sender, **kwargs):
    """
    This syncdb hooks takes care of adding a view permission too all our 
    content types.
    """
    # for each of our content types
    for content_type in ContentType.objects.all():
        # build our permission slug
        codename = "view_%s" % content_type.model

        # if it doesn't exist..
        if not Permission.objects.filter(content_type=content_type, codename=codename):
            # add it
            Permission.objects.create(content_type=content_type,
                                      codename=codename,
                                      name="Can view %s" % content_type.name)
            print "Added view permission for %s" % content_type.name

# check for all our view permissions after a syncdb
post_syncdb.connect(add_view_permissions)

Whenever you issue a 'syncdb' command, all content types can be checked to see if they have a 'view' permission, and if not, create one.

  • SOURCE: The Nyaruka Blog

2. Add the permission to the Meta permissions option:

Under every model you would add something like this to its Meta options:

class Pizza(models.Model):
    cheesiness = models.IntegerField()

    class Meta:
        permissions = (
            ('view_pizza', 'Can view pizza'),
        )

This will accomplish the same as 1 except you have to manually add it to each class.

3. NEW in Django 1.7, Add the permission to the Meta default_permissions option:

In Django 1.7 they added the default_permissions Meta option. Under every model you would add 'view' to the default_permissions option:

class Pizza(models.Model):
    cheesiness = models.IntegerField()

    class Meta:
        default_permissions = ('add', 'change', 'delete', 'view')

Test the 'view' permission is added to all models

As for testing the whether a user has the permission, you can test on the has_perm() function. For example:

user.has_perm('appname.view_pizza') # returns True if user 'Can view pizza'

like image 74
pcoronel Avatar answered Oct 06 '22 02:10

pcoronel