From dded2ac016adf8602a803fff5bedde76ad0b87f8 Mon Sep 17 00:00:00 2001 From: winkidney Date: Thu, 5 Dec 2019 23:13:06 +0800 Subject: [PATCH] Feature: Add pin-add and pin-remove at backend --- core/serializers.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/core/serializers.py b/core/serializers.py index fe29a8e..59f3d94 100644 --- a/core/serializers.py +++ b/core/serializers.py @@ -129,16 +129,25 @@ class PinSerializer(serializers.HyperlinkedModelSerializer): return super(PinSerializer, self).update(instance, validated_data) +class PinIdListField(serializers.ListField): + child = serializers.IntegerField( + min_value=1 + ) + + class BoardSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Board fields = ( + settings.DRF_URL_FIELD_NAME, "id", "name", "pins", "pins_detail", "published", "submitter", + "pins_to_add", + "pins_to_remove", ) read_only_fields = ('submitter', 'published') extra_kwargs = { @@ -153,8 +162,45 @@ class BoardSerializer(serializers.HyperlinkedModelSerializer): many=True, required=False, ) + pins_to_add = PinIdListField( + max_length=10, + write_only=True, + required=False, + allow_empty=False, + help_text="only patch method works for this field", + ) + pins_to_remove = PinIdListField( + max_length=10, + write_only=True, + required=False, + allow_empty=False, + help_text="only patch method works for this field" + ) + + @staticmethod + def _get_list(pins_id): + return tuple(Pin.objects.filter(id__in=pins_id)) + + def update(self, instance: Board, validated_data): + pins_to_add = validated_data.pop("pins_to_add", []) + pins_to_remove = validated_data.pop("pins_to_remove", []) + instance = super(BoardSerializer, self).update(instance, validated_data) + changed = False + if pins_to_add: + changed = True + for pin in self._get_list(pins_to_add): + instance.pins.add(pin) + if pins_to_remove: + changed = True + for pin in self._get_list(pins_to_remove): + instance.pins.remove(pin) + if changed: + instance.save() + return instance def create(self, validated_data): + validated_data.pop('pins_to_remove', None) + validated_data.pop('pins_to_remove', None) user = self.context['request'].user if Board.objects.filter(name=validated_data['name'], submitter=user).exists(): raise ValidationError(