def permissions(self, request, pk): """ List user permissions. """ user = self.get_object() object_permission = PermissionSerializer( user.userobjectpermission_set.all(), many=True, context={"request": request}) permissions = PermissionSerializer(user.user_permissions.all(), many=True) return Response(object_permission.data + permissions.data)
def create(self, request, group_pk): """ Create an object permission to a group. """ group = Group.objects.get(pk=group_pk) permission = self.get_model_permission(request) object_pk = self.get_object_pk(request) object_permission = GroupObjectPermission( group=group, permission=permission, content_type_id=permission.content_type_id, object_pk=object_pk, ) try: object_permission.save() except IntegrityError: raise ValidationError(_("The assigned permission already exists.")) serializer = PermissionSerializer(object_permission, context={ "group_pk": group_pk, "request": request }) return Response(serializer.data, status=status.HTTP_201_CREATED)
def retrieve(self, request, group_pk, pk): instance = get_object_or_404(GroupObjectPermission, pk=pk) serializer = PermissionSerializer(instance, context={ "group_pk": group_pk, "request": request }) return Response(serializer.data)
def list(self, request, group_pk): """ List group model permissions. """ group = Group.objects.get(pk=group_pk) queryset = group.permissions.all() page = self.paginate_queryset(queryset) if page is not None: serializer = PermissionSerializer(page, context={"group_pk": group_pk}, many=True) return self.get_paginated_response(serializer.data) serializer = PermissionSerializer(queryset, context={"group_pk": group_pk}, many=True) return Response(serializer.data)
def create(self, request, group_pk): """ Add a model permission to a group. """ group = Group.objects.get(pk=group_pk) permission = self.get_model_permission(request) group.permissions.add(permission) group.save() serializer = PermissionSerializer(permission, context={"group_pk": group_pk}) return Response(serializer.data, status=status.HTTP_201_CREATED)