Split cstoken and stoken to be different concepts

The stokens are really just integrity checks for items, and are really
just tied to what revision we expected to have first what we have. So we
will rename stoken to lastRev or something, and have them completely
separate.

A partial revert of e22a49f982046e875d4e1c5007a91353527d7a0f
master
Tom Hacohen 5 years ago
parent 6e7ad92a12
commit 8eee280bbb

@ -41,13 +41,13 @@ class Collection(models.Model):
def main_item(self): def main_item(self):
return self.items.get(uid=None) return self.items.get(uid=None)
@cached_property @property
def content(self): def content(self):
return self.main_item.content return self.main_item.content
@property @property
def stoken(self): def stoken(self):
return self.main_item.stoken return self.content.uid
@cached_property @cached_property
def cstoken(self): def cstoken(self):
@ -78,7 +78,7 @@ class CollectionItem(models.Model):
@property @property
def stoken(self): def stoken(self):
return self.content.stoken.uid return self.content.uid
def chunk_directory_path(instance, filename): def chunk_directory_path(instance, filename):

@ -67,24 +67,6 @@ from .serializers import (
User = get_user_model() User = get_user_model()
def get_fresh_stoken(obj):
try:
del obj.main_item
except AttributeError:
pass
return obj.stoken
def get_fresh_item_stoken(obj):
try:
del obj.content
except AttributeError:
pass
return obj.stoken
class BaseViewSet(viewsets.ModelViewSet): class BaseViewSet(viewsets.ModelViewSet):
authentication_classes = tuple(app_settings.API_AUTHENTICATORS) authentication_classes = tuple(app_settings.API_AUTHENTICATORS)
permission_classes = tuple(app_settings.API_PERMISSIONS) permission_classes = tuple(app_settings.API_PERMISSIONS)
@ -173,18 +155,18 @@ class CollectionViewSet(BaseViewSet):
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
self.perform_update(serializer) self.perform_update(serializer)
return Response({'stoken': get_fresh_stoken(instance)}) return Response({})
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, context=self.get_serializer_context()) serializer = self.serializer_class(data=request.data, context=self.get_serializer_context())
if serializer.is_valid(): if serializer.is_valid():
try: try:
instance = serializer.save(owner=self.request.user) serializer.save(owner=self.request.user)
except IntegrityError: except IntegrityError:
content = {'code': 'integrity_error'} content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST) return Response(content, status=status.HTTP_400_BAD_REQUEST)
return Response({'stoken': get_fresh_stoken(instance)}, status=status.HTTP_201_CREATED) return Response({}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@ -298,7 +280,7 @@ class CollectionItemViewSet(BaseViewSet):
queryset, cstoken_rev = self.filter_by_cstoken(request, queryset) queryset, cstoken_rev = self.filter_by_cstoken(request, queryset)
uids, stokens = zip(*[(item['uid'], item.get('stoken')) for item in serializer.validated_data]) uids, stokens = zip(*[(item['uid'], item.get('stoken')) for item in serializer.validated_data])
revs = CollectionItemRevision.objects.filter(stoken__uid__in=stokens, current=True) revs = CollectionItemRevision.objects.filter(uid__in=stokens, current=True)
queryset = queryset.filter(uid__in=uids).exclude(revisions__in=revs) queryset = queryset.filter(uid__in=uids).exclude(revisions__in=revs)
queryset, new_cstoken = self.get_queryset_cstoken(queryset) queryset, new_cstoken = self.get_queryset_cstoken(queryset)
@ -338,7 +320,6 @@ class CollectionItemViewSet(BaseViewSet):
return Response(content, status=status.HTTP_400_BAD_REQUEST) return Response(content, status=status.HTTP_400_BAD_REQUEST)
ret = { ret = {
"data": [get_fresh_item_stoken(item) for item in items],
} }
return Response(ret, status=status.HTTP_200_OK) return Response(ret, status=status.HTTP_200_OK)
@ -377,7 +358,6 @@ class CollectionItemViewSet(BaseViewSet):
return Response(content, status=status.HTTP_400_BAD_REQUEST) return Response(content, status=status.HTTP_400_BAD_REQUEST)
ret = { ret = {
"data": [get_fresh_item_stoken(item) for item in items],
} }
return Response(ret, status=status.HTTP_200_OK) return Response(ret, status=status.HTTP_200_OK)

Loading…
Cancel
Save