コード例 #1
0
def permissions_import_json(request, ufp_id=0):
    upload_file = request.FILES.get('perms_upload')

    if upload_file:
        try:
            perms = json.loads(upload_file.read().decode('utf-8'))
            username = perms.get('username')

            if ufp_id:
                ufp = get_object_or_404(UserFieldPermissions, pk=ufp_id)
                if username == ufp.user.username:
                    # Delete the old field permissions
                    ufp.permissions.all().delete()
                    _add_permissions(request, ufp, perms)
                else:
                    message = 'The wrong user is defined in the imported file'
                    messages.add_message(request, messages.ERROR, message)
            else:
                user = get_object_or_404(User, username=username)
                ufp = UserFieldPermissions(user=user)
                ufp.save()
                _add_permissions(request, ufp, perms)
                ufp_id = ufp.pk
        except Exception as e:
            message = 'Error in the import : %s' % e
            messages.add_message(request, messages.ERROR, message)
    else:
        message = 'Missing imported file'
        messages.add_message(request, messages.ERROR, message)

    return redirect(
        '/admin/rest_framework_fine_permissions/userfieldpermissions/%s'\
            % ufp_id)
コード例 #2
0
def permissions_import_json(request, ufp_id=0):
    upload_file = request.FILES.get('perms_upload')

    if upload_file:
        try:
            perms = json.loads(upload_file.read().decode('utf-8'))
            username = perms.get('username')

            if ufp_id:
                ufp = get_object_or_404(UserFieldPermissions, pk=ufp_id)
                if username == ufp.user.username:
                    # Clear the old field permissions
                    ufp.permissions.clear()
                else:
                    message = 'The wrong user is defined in the imported file'
                    messages.add_message(request, messages.ERROR, message)
            else:
                user = get_object_or_404(User, username=username)
                ufp = UserFieldPermissions(user=user)
                ufp.save()

            _add_permissions(request, ufp, perms)
            ufp_id = ufp.pk
        except Exception as e:
            message = 'Error in the import : %s' % e
            messages.add_message(request, messages.ERROR, message)
    else:
        message = 'Missing imported file'
        messages.add_message(request, messages.ERROR, message)

    return redirect(
        '/admin/rest_framework_fine_permissions/userfieldpermissions/%s' %
        ufp_id)
    def handle(self, *args, **options):
        """
        dump fields permissions for a user
        """

        def get_user(username):
            try:
                return User.objects.get(username=username)
            except ObjectDoesNotExist as e:
                raise CommandError("This user doesn't exist in the database")

        def add_permissions(user_field_permissions, content_type, name):

            p = None
            try:
                p = FieldPermission.objects.get(content_type=content_type, name=name)
            except ObjectDoesNotExist:
                p = FieldPermission(content_type=content_type, name=name)
                p.save()
            finally:
                user_field_permissions.permissions.add(p)



        if len(args) !=1:
             raise CommandError("Specifies a json file created by the fine_permissions_dump command")
        else:
            try:


                with open(args[0], 'r') as json_file:
                    myjson = json.load(json_file)

                    user = get_user(options.get('user')) if options['user'] else get_user(myjson['username'])
                    fields_permissions = myjson['fields_permissions']

                    user_field_permissions = UserFieldPermissions(user=user)
                    user_field_permissions.save()

                    for f in fields_permissions:
                        content_type = ContentType.objects.get(app_label=f["app_label"], model=f["model"])
                        add_permissions(user_field_permissions, content_type, f['name'])


            except Exception as e:
                raise CommandError(e)