Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Created October 24, 2012 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/3946353 to your computer and use it in GitHub Desktop.
Save mariocesar/3946353 to your computer and use it in GitHub Desktop.
Django: Sync new permissions for specified apps with the database, or all apps if no args are specified
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]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment