2021-03-16 21:36:59 +01:00
|
|
|
# -*- 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
|
2021-03-16 22:20:41 +01:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-03-16 21:36:59 +01:00
|
|
|
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):
|
2021-03-16 22:20:41 +01:00
|
|
|
try:
|
|
|
|
orgroup = Group.objects.get(name='org')
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
orgroup = Group(name='org')
|
|
|
|
orgroup.save()
|
2021-03-16 21:36:59 +01:00
|
|
|
permissions = json.load(options['file'][0])
|
2021-03-16 21:38:07 +01:00
|
|
|
orgroup.permissions.clear()
|
2021-03-16 21:36:59 +01:00
|
|
|
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()
|
|
|
|
|