X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/55efada7f210cda246203bdef15bdd781ad26cfe..cba9f6f86615660a7dd4d63def4cbf9c6751e928:/src/bookmarks/api/views.py diff --git a/src/bookmarks/api/views.py b/src/bookmarks/api/views.py index 82502e9e2..6aeb82de8 100644 --- a/src/bookmarks/api/views.py +++ b/src/bookmarks/api/views.py @@ -3,6 +3,7 @@ from api.utils import never_cache from django.db.models import Q from django.http import Http404, JsonResponse from django.shortcuts import render, get_object_or_404 +from django.utils.timezone import now from django.views.decorators import cache import catalogue.models from wolnelektury.utils import is_ajax @@ -48,6 +49,17 @@ class BookmarkSerializer(serializers.ModelSerializer): fields = ['book_slug', 'book', 'anchor', 'audio_timestamp', 'mode', 'note', 'href', 'uuid', 'location', 'timestamp', 'deleted'] read_only_fields = ['uuid', 'mode'] + def create(self, validated_data): + book = validated_data.pop('book_slug', None) + if book is not None: + validated_data['book'] = book + return super().create(validated_data) + + def update(self, instance, validated_data): + book = validated_data.pop('book_slug', None) + if book is not None: + validated_data['book'] = book + return super().update(instance, validated_data) @never_cache class BookmarksView(ListCreateAPIView): @@ -100,3 +112,8 @@ class BookmarkView(RetrieveUpdateDestroyAPIView): return models.Bookmark.objects.filter(q) else: return self.request.user.bookmark_set.all() + + def perform_destroy(self, instance): + instance.deleted = True + instance.updated_at = now() + instance.save()