Přidán management command pro ukládání a načítání práv skupiny org.
This commit is contained in:
parent
3088c5ce18
commit
f28e1cec30
2 changed files with 48 additions and 0 deletions
26
seminar/management/commands/load_org_permissions.py
Normal file
26
seminar/management/commands/load_org_permissions.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.contrib.sessions.models import Session
|
||||||
|
from django.contrib.auth.models import Group, Permission
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
import json
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument('file', nargs=1, type=argparse.FileType('r', encoding='utf8'))
|
||||||
|
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
orgroup = Group.objects.get(name='org')
|
||||||
|
permissions = json.load(options['file'][0])
|
||||||
|
for jp in permissions:
|
||||||
|
ct = ContentType.objects.get(app_label = jp['ct_app_label'], model = jp['ct_model'])
|
||||||
|
perm = Permission.objects.get(content_type = ct, codename = jp['codename'])
|
||||||
|
orgroup.permissions.add(perm)
|
||||||
|
orgroup.save()
|
||||||
|
|
22
seminar/management/commands/save_org_permissions.py
Normal file
22
seminar/management/commands/save_org_permissions.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.contrib.sessions.models import Session
|
||||||
|
from django.contrib.auth.models import Group, Permission
|
||||||
|
import json
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""
|
||||||
|
Dump permissions for group 'org' such that them can be used on an other machine.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
orgroup = Group.objects.get(name='org')
|
||||||
|
permissions = []
|
||||||
|
for p in orgroup.permissions.all():
|
||||||
|
permissions.append({
|
||||||
|
'codename': p.codename,
|
||||||
|
'ct_app_label': p.content_type.app_label,
|
||||||
|
'ct_model': p.content_type.model})
|
||||||
|
print(json.dumps(permissions))
|
||||||
|
|
Loading…
Reference in a new issue