Skip to content

Instantly share code, notes, and snippets.

@xangmuve
Forked from nicpottier/view_permission.py
Last active August 29, 2015 14:14
Show Gist options
  • Save xangmuve/121b65e8c9859b327e58 to your computer and use it in GitHub Desktop.
Save xangmuve/121b65e8c9859b327e58 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment