Skip to content

Instantly share code, notes, and snippets.

@adrienlachaize

adrienlachaize/sync_perms

Last active Sep 29, 2015
Embed
What would you like to do?
Refresh apps permissions
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.apps import apps
from django.db.models import get_models
from django.core.management.base import BaseCommand
from django.contrib.auth.management import create_permissions
class Command(BaseCommand):
args = '<app app ...>'
help = 'Refresh permissions for specified apps, or all apps if no args are specified'
def handle(self, *args, **options):
if not args:
apps_set = set()
for model in get_models():
apps_set.add(model._meta.app_label)
apps_config = []
for app in set(apps_set):
apps_config.append(apps.get_app_config(app))
else:
apps_config = []
for arg in args:
apps_config.append(apps.get_app_config(arg))
for config in apps_config:
create_permissions(config, options.get('verbosity', 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.