public
Created

Django: Sync new permissions for specified apps with the database, or all apps if no args are specified

  • Download Gist
update_permissions.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
from django.core.management.base import BaseCommand
from django.db.models import get_models, get_app
from django.contrib.auth.management import create_permissions
 
class Command(BaseCommand):
args = '<app app ...>'
help = 'Sync new permissions for specified apps with the database, or all apps if no args are specified'
 
def handle(self, *args, **options):
 
verbosity = options.get('verbosity')
verbose = False if verbosity == '0' else True
apps = []
 
if not args:
for model in get_models():
apps.append(get_app(model._meta.app_label))
else:
for arg in args:
apps.append(get_app(arg))
 
apps = set(apps)
 
for app in apps:
if verbose:
print '.'.join(app.__name__.split('.')[:-1]),
 
create_permissions(app, get_models(), options.get('verbosity', 0))
 
if verbose:
print ' [OK]'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.