Friday, October 28, 2016

Re: Read Only by user in Django Admin

Hi,

The following PR is ready to merge and implements this: https://github.com/django/django/pull/6734

I will squash the commits  as soon as I'm back in the office (next week) and it will probably be merged soon.

Bests

Olivier


On 25 Oct 2016 19:03, "Luis Zárate" <luisza14@gmail.com> wrote:
There is a lot of scenarios where you need readonly view for your staff.  And yes you can implement a readonly  in django admin without modify django core admin with some tricks.

I



2016-10-24 7:26 GMT-06:00 Derek <gamesbook@gmail.com>:
Would it be possible to add these extra admin methods into a parent class; and then have all your individual model admins inherit from it?  Ditto for the models.py

On Sunday, 8 February 2015 21:15:42 UTC+2, Hangloser Firestarter wrote:
Solved.

In __init__.py
...
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)
...

In admin.py
...
class MyAdmin(admin.ModelAdmin):
    def has_change_permission(self, request, obj=None):
        ct = ContentType.objects.get_for_model(self.model)
        salida = False
        if request.user.is_superuser:
            salida = True
        else:
            if request.user.has_perm('%s.view_%s' % (ct.app_label, ct.model)):
                salida = True
            else:
                if request.user.has_perm('%s.change_%s' % (ct.app_label, ct.model)):
                    salida = True
                else:
                    salida = False
        return salida

    def get_readonly_fields(self, request, obj=None):
        ct = ContentType.objects.get_for_model(self.model)
        if not request.user.is_superuser and request.user.has_perm('%s.view_%s' % (ct.app_label, ct.model)):
            return [el.name for el in self.model._meta.fields]
        return self.readonly_fields
...

in models.py
...
class City(models.Model):
    nome_cidade = models.CharField(max_length=100)
    estado_cidade = models.CharField(max_length=100)
    pais_cidade = models.CharField(max_length=100)

    def __str__(self):
        return self.nome_cidade

    class Meta:
        permissions = (
            ('view_city', 'Can view city'),
        )
...

Thank's for help!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/74f952fe-f0f5-4fb3-abd4-d6736cbd2743%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
"La utopía sirve para caminar" Fernando Birri


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG%2B5VyMVxD7n3FoJf%3D8xgweBAXueK6qv5tUAQhe_Dg8A%2B6ZLLQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAExk7p3TrNVek7jD0KL2Zx_x8sAi6azW5WKXtqbgsk8hhKoWFA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment