@@ -1831,10 +1831,7 @@ def perform_destroy(self, instance):
18311831
18321832
18331833class TemplateViewSet (
1834- drf .mixins .CreateModelMixin ,
1835- drf .mixins .DestroyModelMixin ,
18361834 drf .mixins .RetrieveModelMixin ,
1837- drf .mixins .UpdateModelMixin ,
18381835 viewsets .GenericViewSet ,
18391836):
18401837 """Template ViewSet"""
@@ -1890,100 +1887,6 @@ def list(self, request, *args, **kwargs):
18901887 serializer = self .get_serializer (queryset , many = True )
18911888 return drf .response .Response (serializer .data )
18921889
1893- @transaction .atomic
1894- def perform_create (self , serializer ):
1895- """Set the current user as owner of the newly created object."""
1896- obj = serializer .save ()
1897- models .TemplateAccess .objects .create (
1898- template = obj ,
1899- user = self .request .user ,
1900- role = models .RoleChoices .OWNER ,
1901- )
1902-
1903-
1904- class TemplateAccessViewSet (
1905- ResourceAccessViewsetMixin ,
1906- drf .mixins .CreateModelMixin ,
1907- drf .mixins .DestroyModelMixin ,
1908- drf .mixins .RetrieveModelMixin ,
1909- drf .mixins .UpdateModelMixin ,
1910- viewsets .GenericViewSet ,
1911- ):
1912- """
1913- API ViewSet for all interactions with template accesses.
1914-
1915- GET /api/v1.0/templates/<template_id>/accesses/:<template_access_id>
1916- Return list of all template accesses related to the logged-in user or one
1917- template access if an id is provided.
1918-
1919- POST /api/v1.0/templates/<template_id>/accesses/ with expected data:
1920- - user: str
1921- - role: str [administrator|editor|reader]
1922- Return newly created template access
1923-
1924- PUT /api/v1.0/templates/<template_id>/accesses/<template_access_id>/ with expected data:
1925- - role: str [owner|admin|editor|reader]
1926- Return updated template access
1927-
1928- PATCH /api/v1.0/templates/<template_id>/accesses/<template_access_id>/ with expected data:
1929- - role: str [owner|admin|editor|reader]
1930- Return partially updated template access
1931-
1932- DELETE /api/v1.0/templates/<template_id>/accesses/<template_access_id>/
1933- Delete targeted template access
1934- """
1935-
1936- lookup_field = "pk"
1937- permission_classes = [permissions .ResourceAccessPermission ]
1938- throttle_scope = "template_access"
1939- queryset = models .TemplateAccess .objects .select_related ("user" ).all ()
1940- resource_field_name = "template"
1941- serializer_class = serializers .TemplateAccessSerializer
1942-
1943- @cached_property
1944- def template (self ):
1945- """Get related template from resource ID in url."""
1946- try :
1947- return models .Template .objects .get (pk = self .kwargs ["resource_id" ])
1948- except models .Template .DoesNotExist as excpt :
1949- raise drf .exceptions .NotFound () from excpt
1950-
1951- def list (self , request , * args , ** kwargs ):
1952- """Restrict templates returned by the list endpoint"""
1953- user = self .request .user
1954- teams = user .teams
1955- queryset = self .filter_queryset (self .get_queryset ())
1956-
1957- # Limit to resource access instances related to a resource THAT also has
1958- # a resource access instance for the logged-in user (we don't want to list
1959- # only the resource access instances pointing to the logged-in user)
1960- queryset = queryset .filter (
1961- db .Q (template__accesses__user = user )
1962- | db .Q (template__accesses__team__in = teams ),
1963- ).distinct ()
1964-
1965- serializer = self .get_serializer (queryset , many = True )
1966- return drf .response .Response (serializer .data )
1967-
1968- def perform_create (self , serializer ):
1969- """
1970- Actually create the new template access:
1971- - Ensures the `template_id` is explicitly set from the URL.
1972- - If the assigned role is `OWNER`, checks that the requesting user is an owner
1973- of the document. This is the only permission check deferred until this step;
1974- all other access checks are handled earlier in the permission lifecycle.
1975- """
1976- role = serializer .validated_data .get ("role" )
1977- if (
1978- role == choices .RoleChoices .OWNER
1979- and self .template .get_role (self .request .user ) != choices .RoleChoices .OWNER
1980- ):
1981- raise drf .exceptions .PermissionDenied (
1982- "Only owners of a template can assign other users as owners."
1983- )
1984-
1985- serializer .save (template_id = self .kwargs ["resource_id" ])
1986-
19871890
19881891class InvitationViewset (
19891892 drf .mixins .CreateModelMixin ,
0 commit comments