Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@action()
def test_sync_action(self, pk=None, **kwargs):
user = self.get_object(pk=pk)
s = self.get_serializer(
action_kwargs={'pk': pk},
instance=user
)
return s.data, 200
@action()
def test_sync_action(self, pk=None, **kwargs):
results["test_sync_action"] = pk
return {"pk": pk, "sync": True}, 200
@action()
async def test_async_action(self, pk=None, **kwargs):
results["test_action"] = pk
return {"pk": pk}, 200
@action()
async def update_username(self, pk=None, name=None, **kwargs):
tm = await database_sync_to_async(self.get_object)(pk=pk)
tm.name = name
await database_sync_to_async(tm.save)()
return {'pk': pk}, 200
@action()
async def unsubscribe_instance(self, request_id=None, **kwargs):
if request_id is None:
raise ValueError("request_id must have a value set")
# subscribe!
instance = await database_sync_to_async(self.get_object)(**kwargs)
await self.handle_instance_change.unsubscribe(instance=instance)
del self.subscribed_requests[self.__class__.handle_instance_change]
return None, status.HTTP_204_NO_CONTENT
@action()
def retrieve(self, **kwargs):
instance = self.get_object(**kwargs)
serializer = self.get_serializer(instance=instance, action_kwargs=kwargs)
return serializer.data, status.HTTP_200_OK
@action()
def update(self, data, **kwargs):
instance = self.get_object(data=data, **kwargs)
serializer = self.get_serializer(
instance=instance, data=data, action_kwargs=kwargs, partial=False
)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer, **kwargs)
if getattr(instance, "_prefetched_objects_cache", None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
return serializer.data, status.HTTP_200_OK
@action()
def patch(self, data, **kwargs):
instance = self.get_object(data=data, **kwargs)
serializer = self.get_serializer(
instance=instance, data=data, action_kwargs=kwargs, partial=True
)
serializer.is_valid(raise_exception=True)
self.perform_patch(serializer, **kwargs)
if getattr(instance, "_prefetched_objects_cache", None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
return serializer.data, status.HTTP_200_OK
@action()
def create(self, data, **kwargs):
serializer = self.get_serializer(data=data, action_kwargs=kwargs)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer, **kwargs)
return serializer.data, status.HTTP_201_CREATED
@action()
def delete(self, **kwargs):
instance = self.get_object(**kwargs)
self.perform_delete(instance, **kwargs)
return None, status.HTTP_204_NO_CONTENT