mirror of
https://github.com/etesync/server
synced 2024-11-26 02:38:15 +00:00
Cleanup how we handle inline serializers.
This commit is contained in:
parent
5dfa2ac8cb
commit
963dc3c62d
@ -66,44 +66,40 @@ class CollectionItemChunkSerializer(serializers.ModelSerializer):
|
|||||||
fields = ('uid', 'chunkFile')
|
fields = ('uid', 'chunkFile')
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemRevisionBaseSerializer(serializers.ModelSerializer):
|
class CollectionItemRevisionSerializer(serializers.ModelSerializer):
|
||||||
chunks = serializers.SlugRelatedField(
|
chunks = serializers.SlugRelatedField(
|
||||||
slug_field='uid',
|
slug_field='uid',
|
||||||
queryset=models.CollectionItemChunk.objects.all(),
|
queryset=models.CollectionItemChunk.objects.all(),
|
||||||
many=True
|
many=True
|
||||||
)
|
)
|
||||||
|
chunksUrls = serializers.SerializerMethodField('get_chunks_urls')
|
||||||
|
chunksData = serializers.SerializerMethodField('get_chunks_data')
|
||||||
meta = BinaryBase64Field()
|
meta = BinaryBase64Field()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.CollectionItemRevision
|
model = models.CollectionItemRevision
|
||||||
fields = ('chunks', 'meta', 'uid', 'deleted')
|
fields = ('chunks', 'meta', 'uid', 'deleted', 'chunksUrls', 'chunksData')
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemRevisionSerializer(CollectionItemRevisionBaseSerializer):
|
|
||||||
chunksUrls = serializers.SerializerMethodField('get_chunks_urls')
|
|
||||||
|
|
||||||
class Meta(CollectionItemRevisionBaseSerializer.Meta):
|
|
||||||
fields = CollectionItemRevisionBaseSerializer.Meta.fields + ('chunksUrls', )
|
|
||||||
|
|
||||||
# FIXME: currently the user is exposed in the url. We don't want that, and we can probably avoid that but still
|
# FIXME: currently the user is exposed in the url. We don't want that, and we can probably avoid that but still
|
||||||
# save it under the user.
|
# save it under the user.
|
||||||
# We would probably be better off just let the user calculate the urls from the uid and a base url for the snapshot.
|
# We would probably be better off just let the user calculate the urls from the uid and a base url for the snapshot.
|
||||||
# E.g. chunkBaseUrl: "/media/bla/bla/" or chunkBaseUrl: "https://media.etesync.com/bla/bla"
|
# E.g. chunkBaseUrl: "/media/bla/bla/" or chunkBaseUrl: "https://media.etesync.com/bla/bla"
|
||||||
def get_chunks_urls(self, obj):
|
def get_chunks_urls(self, obj):
|
||||||
|
prefer_inline = self.context.get('prefer_inline', False)
|
||||||
|
if prefer_inline:
|
||||||
|
return None
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for chunk in obj.chunks.all():
|
for chunk in obj.chunks.all():
|
||||||
ret.append(chunk.chunkFile.url)
|
ret.append(chunk.chunkFile.url)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemRevisionInlineSerializer(CollectionItemRevisionBaseSerializer):
|
|
||||||
chunksData = serializers.SerializerMethodField('get_chunks_data')
|
|
||||||
|
|
||||||
class Meta(CollectionItemRevisionBaseSerializer.Meta):
|
|
||||||
fields = CollectionItemRevisionBaseSerializer.Meta.fields + ('chunksData', )
|
|
||||||
|
|
||||||
def get_chunks_data(self, obj):
|
def get_chunks_data(self, obj):
|
||||||
|
prefer_inline = self.context.get('prefer_inline', False)
|
||||||
|
if not prefer_inline:
|
||||||
|
return None
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for chunk in obj.chunks.all():
|
for chunk in obj.chunks.all():
|
||||||
with open(chunk.chunkFile.path, 'rb') as f:
|
with open(chunk.chunkFile.path, 'rb') as f:
|
||||||
@ -111,6 +107,16 @@ class CollectionItemRevisionInlineSerializer(CollectionItemRevisionBaseSerialize
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def to_representation(self, instance):
|
||||||
|
ret = super().to_representation(instance)
|
||||||
|
prefer_inline = self.context.get('prefer_inline', False)
|
||||||
|
if prefer_inline:
|
||||||
|
ret.pop('chunksUrls')
|
||||||
|
else:
|
||||||
|
ret.pop('chunksData')
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemSerializer(serializers.ModelSerializer):
|
class CollectionItemSerializer(serializers.ModelSerializer):
|
||||||
encryptionKey = BinaryBase64Field()
|
encryptionKey = BinaryBase64Field()
|
||||||
@ -154,10 +160,6 @@ class CollectionItemSerializer(serializers.ModelSerializer):
|
|||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemInlineSerializer(CollectionItemSerializer):
|
|
||||||
content = CollectionItemRevisionInlineSerializer(read_only=True, many=False)
|
|
||||||
|
|
||||||
|
|
||||||
class CollectionSerializer(serializers.ModelSerializer):
|
class CollectionSerializer(serializers.ModelSerializer):
|
||||||
encryptionKey = CollectionEncryptionKeyField()
|
encryptionKey = CollectionEncryptionKeyField()
|
||||||
accessLevel = serializers.SerializerMethodField('get_access_level_from_context')
|
accessLevel = serializers.SerializerMethodField('get_access_level_from_context')
|
||||||
|
@ -29,7 +29,6 @@ from .models import Collection, CollectionItem
|
|||||||
from .serializers import (
|
from .serializers import (
|
||||||
CollectionSerializer,
|
CollectionSerializer,
|
||||||
CollectionItemSerializer,
|
CollectionItemSerializer,
|
||||||
CollectionItemInlineSerializer,
|
|
||||||
CollectionItemRevisionSerializer,
|
CollectionItemRevisionSerializer,
|
||||||
CollectionItemChunkSerializer
|
CollectionItemChunkSerializer
|
||||||
)
|
)
|
||||||
@ -66,12 +65,18 @@ class CollectionViewSet(BaseViewSet):
|
|||||||
queryset = type(self).queryset
|
queryset = type(self).queryset
|
||||||
return self.get_collection_queryset(queryset)
|
return self.get_collection_queryset(queryset)
|
||||||
|
|
||||||
|
def get_serializer_context(self):
|
||||||
|
context = super().get_serializer_context()
|
||||||
|
prefer_inline = self.request.method == 'GET' and 'prefer_inline' in self.request.query_params
|
||||||
|
context.update({'request': self.request, 'prefer_inline': prefer_inline})
|
||||||
|
return context
|
||||||
|
|
||||||
def destroy(self, request, uid=None):
|
def destroy(self, request, uid=None):
|
||||||
# FIXME: implement
|
# FIXME: implement
|
||||||
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data, context=self.get_serializer_context())
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
try:
|
try:
|
||||||
serializer.save(owner=self.request.user)
|
serializer.save(owner=self.request.user)
|
||||||
@ -86,7 +91,7 @@ class CollectionViewSet(BaseViewSet):
|
|||||||
def list(self, request):
|
def list(self, request):
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
|
|
||||||
serializer = self.serializer_class(queryset, context={'request': request}, many=True)
|
serializer = self.serializer_class(queryset, context=self.get_serializer_context(), many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
@ -98,12 +103,6 @@ class CollectionItemViewSet(BaseViewSet):
|
|||||||
pagination_class = paginators.LinkHeaderPagination
|
pagination_class = paginators.LinkHeaderPagination
|
||||||
lookup_field = 'uid'
|
lookup_field = 'uid'
|
||||||
|
|
||||||
def get_serializer_class(self):
|
|
||||||
if self.request.method == 'GET' and self.request.query_params.get('prefer_inline'):
|
|
||||||
return CollectionItemInlineSerializer
|
|
||||||
|
|
||||||
return super().get_serializer_class()
|
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
collection_uid = self.kwargs['collection_uid']
|
collection_uid = self.kwargs['collection_uid']
|
||||||
try:
|
try:
|
||||||
@ -117,6 +116,12 @@ class CollectionItemViewSet(BaseViewSet):
|
|||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
def get_serializer_context(self):
|
||||||
|
context = super().get_serializer_context()
|
||||||
|
prefer_inline = self.request.method == 'GET' and 'prefer_inline' in self.request.query_params
|
||||||
|
context.update({'request': self.request, 'prefer_inline': prefer_inline})
|
||||||
|
return context
|
||||||
|
|
||||||
def create(self, request, collection_uid=None):
|
def create(self, request, collection_uid=None):
|
||||||
collection_object = get_object_or_404(self.get_collection_queryset(Collection.objects), uid=collection_uid)
|
collection_object = get_object_or_404(self.get_collection_queryset(Collection.objects), uid=collection_uid)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user