Prefetch: change the type of value prefetch accept.

It's 'auto' by default, but can be changed to 'medium' and soon
another value.
This commit is contained in:
Tom Hacohen 2020-08-04 17:44:57 +03:00
parent a613a32628
commit cf9b6f5904
2 changed files with 3 additions and 3 deletions

View File

@ -109,7 +109,7 @@ class UserSlugRelatedField(serializers.SlugRelatedField):
class ChunksField(serializers.RelatedField): class ChunksField(serializers.RelatedField):
def to_representation(self, obj): def to_representation(self, obj):
obj = obj.chunk obj = obj.chunk
if self.context.get('prefetch'): if self.context.get('prefetch') == 'auto':
with open(obj.chunkFile.path, 'rb') as f: with open(obj.chunkFile.path, 'rb') as f:
return (obj.uid, f.read()) return (obj.uid, f.read())
else: else:

View File

@ -190,7 +190,7 @@ class CollectionViewSet(BaseViewSet):
def get_serializer_context(self): def get_serializer_context(self):
context = super().get_serializer_context() context = super().get_serializer_context()
prefetch = self.request.query_params.get('prefetch', 'true') != 'false' prefetch = self.request.query_params.get('prefetch', 'auto')
context.update({'request': self.request, 'prefetch': prefetch}) context.update({'request': self.request, 'prefetch': prefetch})
return context return context
@ -256,7 +256,7 @@ class CollectionItemViewSet(BaseViewSet):
def get_serializer_context(self): def get_serializer_context(self):
context = super().get_serializer_context() context = super().get_serializer_context()
prefetch = self.request.query_params.get('prefetch', 'true') != 'false' prefetch = self.request.query_params.get('prefetch', 'auto')
context.update({'request': self.request, 'prefetch': prefetch}) context.update({'request': self.request, 'prefetch': prefetch})
return context return context