1
0
mirror of https://github.com/etesync/server synced 2024-12-29 10:48:07 +00:00

CollectionItem: implement batch updating.

This commit is contained in:
Tom Hacohen 2020-05-19 16:16:40 +03:00
parent b6571c93f6
commit c63210fe77
2 changed files with 36 additions and 5 deletions

View File

@ -121,6 +121,7 @@ class CollectionItemSerializer(serializers.ModelSerializer):
def create(self, validated_data): def create(self, validated_data):
"""Function that's called when this serializer creates an item""" """Function that's called when this serializer creates an item"""
validate_stoken = self.context.get('validate_stoken', False)
stoken = validated_data.pop('stoken') stoken = validated_data.pop('stoken')
revision_data = validated_data.pop('content') revision_data = validated_data.pop('content')
uid = validated_data.pop('uid') uid = validated_data.pop('uid')
@ -131,7 +132,7 @@ class CollectionItemSerializer(serializers.ModelSerializer):
instance, created = Model.objects.get_or_create(uid=uid, defaults=validated_data) instance, created = Model.objects.get_or_create(uid=uid, defaults=validated_data)
cur_stoken = instance.stoken if not created else None cur_stoken = instance.stoken if not created else None
if cur_stoken != stoken: if validate_stoken and cur_stoken != stoken:
raise serializers.ValidationError('Wrong stoken. Expected {} got {}'.format(cur_stoken, stoken)) raise serializers.ValidationError('Wrong stoken. Expected {} got {}'.format(cur_stoken, stoken))
if not created: if not created:

View File

@ -245,8 +245,36 @@ class CollectionItemViewSet(BaseViewSet):
@action_decorator(detail=False, methods=['POST']) @action_decorator(detail=False, methods=['POST'])
def batch(self, request, collection_uid=None): def batch(self, request, collection_uid=None):
# FIXME: different to transaction slightly cstoken = request.GET.get('cstoken', None)
return self.transaction(request, collection_uid) collection_object = get_object_or_404(self.get_collection_queryset(Collection.objects), uid=collection_uid)
if cstoken is not None and cstoken != collection_object.cstoken:
content = {'code': 'stale_cstoken', 'detail': 'CSToken is too old'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
items = request.data.get('items')
context = self.get_serializer_context()
serializer = self.get_serializer_class()(data=items, context=context, many=True)
if serializer.is_valid():
try:
with transaction.atomic():
items = serializer.save(collection=collection_object)
except IntegrityError:
# FIXME: should return the items with a bad token (including deps) so we don't have to fetch them after
content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
ret = {
"data": [item.stoken for item in items],
}
return Response(ret, status=status.HTTP_200_OK)
return Response(
{
"items": serializer.errors,
},
status=status.HTTP_400_BAD_REQUEST)
@action_decorator(detail=False, methods=['POST']) @action_decorator(detail=False, methods=['POST'])
def transaction(self, request, collection_uid=None): def transaction(self, request, collection_uid=None):
@ -260,8 +288,10 @@ class CollectionItemViewSet(BaseViewSet):
items = request.data.get('items') items = request.data.get('items')
deps = request.data.get('deps', None) deps = request.data.get('deps', None)
# FIXME: It should just be one serializer # FIXME: It should just be one serializer
serializer = self.get_serializer_class()(data=items, context=self.get_serializer_context(), many=True) context = self.get_serializer_context()
deps_serializer = CollectionItemDepSerializer(data=deps, context=self.get_serializer_context(), many=True) context.update({'validate_stoken': True})
serializer = self.get_serializer_class()(data=items, context=context, many=True)
deps_serializer = CollectionItemDepSerializer(data=deps, context=context, many=True)
ser_valid = serializer.is_valid() ser_valid = serializer.is_valid()
deps_ser_valid = (deps is None or deps_serializer.is_valid()) deps_ser_valid = (deps is None or deps_serializer.is_valid())