0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 04:12:23 +01:00
posthog/ee/api/organization_resource_access.py
Tom Owers 5513be7731
chore: upgraded Ruff linter (#18188)
* Upgraded Ruff linter

* Formatted whole codebase with new ruff rules

* Revert import removal

* Fixed mypi issues or added ignores

* Fixed schema formatting

* Fixed hogvm failing tests

* Remove duplicate key in list
2023-10-26 12:38:15 +02:00

53 lines
1.7 KiB
Python

from rest_framework import mixins, serializers, viewsets
from rest_framework.permissions import IsAuthenticated
from ee.api.role import RolePermissions
from ee.models.organization_resource_access import OrganizationResourceAccess
from posthog.api.routing import StructuredViewSetMixin
from posthog.permissions import OrganizationMemberPermissions
class OrganizationResourceAccessSerializer(serializers.ModelSerializer):
class Meta:
model = OrganizationResourceAccess
fields = [
"id",
"resource",
"access_level",
"organization",
"created_at",
"updated_at",
"created_by",
]
read_only_fields = ["id", "created_at", "created_by", "organization"]
def validate_resource(self, resource):
if OrganizationResourceAccess.objects.filter(
organization=self.context["request"].user.organization,
resource=resource,
).exists():
raise serializers.ValidationError("This resource access already exists.", code="unique")
return resource
def create(self, validated_data):
validated_data["organization"] = self.context["request"].user.organization
return super().create(validated_data)
class OrganizationResourceAccessViewSet(
StructuredViewSetMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
permission_classes = [
IsAuthenticated,
OrganizationMemberPermissions,
RolePermissions,
]
serializer_class = OrganizationResourceAccessSerializer
queryset = OrganizationResourceAccess.objects.all()