Login

create custom permissions

Author:
agusmakmun
Posted:
September 10, 2018
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

create custom permissions

  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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 1. permissions.json
"""
{
    "mapping":{
        "c":"create",
        "r":"read",
        "u":"update",
        "d":"delete"
    },
    "group":{
        "admin":{
            "cart":"r,u,d,buy,send,telandc,read-pending,read-paid,read-bought",
            "province":"c,r,u,d",
            "district":"c,r,u,d",
            "sub_district":"c,r,u,d",
            "customer":"r,u,d,suspend",
            "admin":"c,r,u,d,suspend"
        },
        "operator":{
            "cart":"buy,r",
            "province":"r",
            "district":"r",
            "sub_district":"r"
        }
    }
}
"""

# app/management/commands/create_permissions.py

import os
import json

from django.db.models import Q
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import (User, Group, Permission)
from django.core.management.base import (BaseCommand, CommandError)

from apps.models_helper.address import (Province, District, SubDistrict)
from apps.models_helper.cart import Cart

PERMISSIONS_JSON_FILE = os.path.join(settings.BASE_DIR, 'permissions.json')


class Command(BaseCommand):
    """
    command to generate/recheck all groups & permissions
    which following `permissions.json` file.

    ./manage.py create_permissions
    """

    help = 'Command to generate groups and permissions for users'

    def add_message(self, message, status='success'):
        """ to add message for success or warning only """
        message = self.style.SUCCESS(message) \
            if status == 'success' \
            else self.style.WARNING(message)
        self.stdout.write(message)

    def load_json_file(self):
        """ load json file as dictionary """
        open_file = open(PERMISSIONS_JSON_FILE, 'r').read()
        return json.loads(open_file)

    def key_name_to_content_type(self, key_name):
        """
        parsing a `key_name` to `content_type` object.

        :param `key_name` is like 'cart', 'province', etc.
        """
        key_maps = {
            'admin': User,
            'operator': User,
            'customer': User,
            'cart': Cart,
            'province': Province,
            'district': District,
            'sub_district': SubDistrict
        }
        if key_name in key_maps.keys():
            class_name = key_maps.get(key_name)
            return ContentType.objects.get_for_model(class_name)

        # when `key_name` dosn't exist at `key_maps`
        raise Exception('You need to update `key_maps` in this file: %s' % __file__)

    def generate_permissions(self, key_name, str_permissions):
        """
        :param `key_name` is like "cart", "province", "admin", or etc.
        :param `str_permissions` is like "c,r,u,d,buy,send,telandc"

        parsing some access names:
            c: to create-<key_name>
            r: to read-<key_name>
            u: to update-<key_name>
            d: to delete-<key_name>

        return list objects of permissions.
        """
        list_permissions = str_permissions.split(',')
        json_file = self.load_json_file()
        mapping = json_file.get('mapping')
        new_permissions = []

        for permission in list_permissions:
            if permission in mapping.keys():
                permission = mapping.get(permission)

            code = '%s-%s' % (permission, key_name)
            name = 'can %s %s' % (permission, key_name)
            content_type = self.key_name_to_content_type(key_name)

            permission, truefalse = Permission.objects.get_or_create(codename=code, name=name,
                                                                     content_type=content_type)
            permission.save()
            new_permissions.append(permission)
        return new_permissions

    def check_group(self, group_name):
        """ to get or create the group """
        group, truefalse = Group.objects.get_or_create(name=group_name)
        group.save()
        return group

    def check_permissions(self):
        """
        to get or create the permissions,
        and register the permissions into groups.
        """
        group_names = self.load_json_file().get('group')

        for key_group_name, value_dict in group_names.items():
            permissions = []

            for key_name, str_permissions in value_dict.items():
                new_permissions = self.generate_permissions(key_name, str_permissions)
                permissions.extend(new_permissions)

            group = self.check_group(key_group_name)
            permissions = list(set(permissions))

            for permission in permissions:
                group.permissions.add(permission)

        self.add_message('Groups & Permissions successfully added!')

    def handle(self, *args, **kwargs):
        # noqa
        self.check_permissions()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.