예제 #1
0
    def handle(self, args):
        with transaction.manager:
            # permissions
            perms = {}

            # load some permissions for the API resource ACLAuthorization class
            for resource in ('user', 'group', 'permission'):
                perms[resource] = {
                    'create': Permission.objects.create(
                        name='create-' + resource,
                        description='User can create {} resources.'.format(resource)
                    ),
                    'read': Permission.objects.create(
                        name='read-' + resource,
                        description='User can read {} resources.'.format(resource)
                    ),
                    'update': Permission.objects.create(
                        name='update-' + resource,
                        description='User can update {} resources.'.format(resource)
                    ),
                    'delete': Permission.objects.create(
                        name='delete-' + resource,
                        description='User can delete {} resources.'.format(resource)
                    ),
                }

            # groups
            g1 = Group.objects.create(name='Group 1', permissions=[
                perms['user']['create'],
                perms['user']['read'],
                perms['user']['update'],
                perms['user']['delete'],
            ])
            g2 = Group.objects.create(name='Group 2', permissions=[
                perms['group']['create'],
                perms['group']['read'],
                perms['group']['update'],
                perms['group']['delete'],
            ])
            g3 = Group.objects.create(name='Group 3', permissions=[
                perms['permission']['create'],
                perms['permission']['read'],
                perms['permission']['update'],
                perms['permission']['delete'],
            ])

            # users
            admin_user = User(username='******', is_superuser=True, groups=[g1, g2])
            admin_user.set_password('admin')
            admin_user.save()
            test_user = User(username='******', groups=[g1, g3])
            test_user.set_password('test')
            test_user.save()
예제 #2
0
    def create_superuser(self, superuser_details):
        """
        Creates a superuser in the database according to the parameters
        given in superuser_details.

        :param superuser_details: a dict containing username, password, email
        """
        user = User(username=superuser_details['username'],
                    email=superuser_details['email'],
                    is_superuser=True)
        user.set_password(superuser_details['password'])
        with transaction.manager:
            user.save()
        print('Superuser created successfully')