Fix calculation of stoken.

This commit is contained in:
Tom Hacohen 2020-04-16 17:14:03 +03:00
parent d134934f8c
commit f23815d46d

View File

@ -15,6 +15,7 @@
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import IntegrityError from django.db import IntegrityError
from django.db.models import Max
from django.http import HttpResponseBadRequest, HttpResponse, Http404 from django.http import HttpResponseBadRequest, HttpResponse, Http404
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -103,7 +104,9 @@ class CollectionViewSet(BaseViewSet):
serializer = self.serializer_class(queryset, context=self.get_serializer_context(), many=True) serializer = self.serializer_class(queryset, context=self.get_serializer_context(), many=True)
new_stoken = serializer.data[-1]['stoken'] if len(serializer.data) > 0 else stoken new_stoken_id = queryset.aggregate(stoken_id=Max('items__revisions__id'))['stoken_id']
new_stoken = CollectionItemRevision.objects.get(id=new_stoken_id).uid if new_stoken_id is not None else stoken
ret = { ret = {
'data': serializer.data, 'data': serializer.data,
} }
@ -163,16 +166,11 @@ class CollectionItemViewSet(BaseViewSet):
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
def list(self, request, collection_uid=None): def list(self, request, collection_uid=None):
stoken = request.GET.get('stoken', None)
queryset = self.get_queryset() queryset = self.get_queryset()
queryset = self.filter_by_stoken_and_limit(request, queryset) queryset, new_stoken = self.filter_by_stoken_and_limit(request, queryset)
serializer = self.serializer_class(queryset, context=self.get_serializer_context(), many=True) serializer = self.serializer_class(queryset, context=self.get_serializer_context(), many=True)
queryset = self.filter_by_stoken_and_limit(request, queryset)
new_stoken = serializer.data[-1]['content']['uid'] if len(serializer.data) > 0 else stoken
ret = { ret = {
'data': serializer.data, 'data': serializer.data,
} }
@ -189,17 +187,15 @@ class CollectionItemViewSet(BaseViewSet):
@action_decorator(detail=False, methods=['POST']) @action_decorator(detail=False, methods=['POST'])
def bulk_get(self, request, collection_uid=None): def bulk_get(self, request, collection_uid=None):
stoken = request.GET.get('stoken', None)
queryset = self.get_queryset() queryset = self.get_queryset()
if isinstance(request.data, list): if isinstance(request.data, list):
queryset = queryset.filter(uid__in=request.data) queryset = queryset.filter(uid__in=request.data)
queryset = self.filter_by_stoken_and_limit(request, queryset) queryset, new_stoken = self.filter_by_stoken_and_limit(request, queryset)
serializer = self.get_serializer_class()(queryset, context=self.get_serializer_context(), many=True) serializer = self.get_serializer_class()(queryset, context=self.get_serializer_context(), many=True)
new_stoken = serializer.data[-1]['content']['uid'] if len(serializer.data) > 0 else stoken
ret = { ret = {
'data': serializer.data, 'data': serializer.data,
} }
@ -213,7 +209,10 @@ class CollectionItemViewSet(BaseViewSet):
last_rev = get_object_or_404(CollectionItemRevision.objects.all(), uid=stoken) last_rev = get_object_or_404(CollectionItemRevision.objects.all(), uid=stoken)
queryset = queryset.filter(revisions__id__gt=last_rev.id) queryset = queryset.filter(revisions__id__gt=last_rev.id)
return queryset[:limit] new_stoken_id = queryset.aggregate(stoken_id=Max('revisions__id'))['stoken_id']
new_stoken = CollectionItemRevision.objects.get(id=new_stoken_id).uid if new_stoken_id is not None else stoken
return queryset[:limit], new_stoken
class CollectionItemChunkViewSet(viewsets.ViewSet): class CollectionItemChunkViewSet(viewsets.ViewSet):