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(
def get(self, request, *args, **kwargs):
fields_to_return = self.get_serializer_fields(request)
limit_to_latest_versions = is_true(
request.query_params.get("latest_versions", False)
)
......@@ -243,16 +244,16 @@ class RetrieveUpdateDestroyContributionView(
return super().get_serializer(*args, **kwargs)
def get_object(self):
version = self.kwargs["version"]
author_name = self.kwargs["author_name"]
object_name = self.kwargs["object_name"]
kwargs = dict(
version=self.kwargs["version"], name__iexact=self.kwargs["object_name"]
)
if hasattr(self.model, "author"):
kwargs["author__username__iexact"] = self.kwargs["author_name"]
user = self.request.user
try:
obj = self.model.objects.for_user(user, True).get(
author__username__iexact=author_name,
name__iexact=object_name,
version=version,
)
obj = self.model.objects.for_user(user, True).get(**kwargs)
except self.model.DoesNotExist:
raise drf_exceptions.NotFound()
return obj
......@@ -262,7 +263,10 @@ class RetrieveUpdateDestroyContributionView(
self.check_object_permissions(request, db_object)
# 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(
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