Commit c80a19b5 authored by Samuel GAIST's avatar Samuel GAIST

[common][api] Make RetrieveUpdateDestroyContributionView usable with versionable

This makes it usable with ProtocolTemplate and
possibly Database as well.
parent 072089e5
...@@ -145,6 +145,7 @@ class ListCreateBaseView( ...@@ -145,6 +145,7 @@ class ListCreateBaseView(
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
fields_to_return = self.get_serializer_fields(request) fields_to_return = self.get_serializer_fields(request)
limit_to_latest_versions = is_true( limit_to_latest_versions = is_true(
request.query_params.get("latest_versions", False) request.query_params.get("latest_versions", False)
) )
...@@ -243,16 +244,16 @@ class RetrieveUpdateDestroyContributionView( ...@@ -243,16 +244,16 @@ class RetrieveUpdateDestroyContributionView(
return super().get_serializer(*args, **kwargs) return super().get_serializer(*args, **kwargs)
def get_object(self): def get_object(self):
version = self.kwargs["version"] kwargs = dict(
author_name = self.kwargs["author_name"] version=self.kwargs["version"], name__iexact=self.kwargs["object_name"]
object_name = self.kwargs["object_name"] )
if hasattr(self.model, "author"):
kwargs["author__username__iexact"] = self.kwargs["author_name"]
user = self.request.user user = self.request.user
try: try:
obj = self.model.objects.for_user(user, True).get( obj = self.model.objects.for_user(user, True).get(**kwargs)
author__username__iexact=author_name,
name__iexact=object_name,
version=version,
)
except self.model.DoesNotExist: except self.model.DoesNotExist:
raise drf_exceptions.NotFound() raise drf_exceptions.NotFound()
return obj return obj
...@@ -262,7 +263,10 @@ class RetrieveUpdateDestroyContributionView( ...@@ -262,7 +263,10 @@ class RetrieveUpdateDestroyContributionView(
self.check_object_permissions(request, db_object) self.check_object_permissions(request, db_object)
# Process the query string # Process the query string
allow_sharing = request.user == db_object.author allow_sharing = False
if hasattr(db_object, "author"):
allow_sharing = request.user == db_object.author
fields_to_return = self.get_serializer_fields( fields_to_return = self.get_serializer_fields(
request, allow_sharing=allow_sharing request, allow_sharing=allow_sharing
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment