Created
March 22, 2011 07:43
-
-
Save nicpottier/880901 to your computer and use it in GitHub Desktop.
Adds a view permission to all your models in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Hola a todos, muy bueno!!!, para integrar con el admin se me ocurrio extender el admin. Como ejemplo utilizamos un usuario le damos solo permiso de ver y que sea staff.
Lo probe con la version django 1.6.
Este es un ejemplo de una app, el archivo es "admin.py":
https://gist.github.com/fstnando/11291377
Espero que les sirva.
Saludos
se ve muy bueno voy a probarlo...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll keep a look out for that, thanks