e497bcb5c0
* #24 Implement readonly bookmark API * #24 Implement create/update bookmark API * #24 Fix title, description not allowing blank values * #24 Code cleanup * #24 Add modification dates to response * #24 Add API docs * #24 Implement delete bookmark API * #24 Fix API docs link * #24 Fix API docs link * #24 Implement tag API Co-authored-by: Sascha Ißbrücker <sissbruecker@lyska.io>
48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
from rest_framework import viewsets, mixins
|
|
from rest_framework.routers import DefaultRouter
|
|
|
|
from bookmarks import queries
|
|
from bookmarks.api.serializers import BookmarkSerializer, TagSerializer
|
|
from bookmarks.models import Bookmark, Tag
|
|
|
|
|
|
class BookmarkViewSet(viewsets.GenericViewSet,
|
|
mixins.ListModelMixin,
|
|
mixins.RetrieveModelMixin,
|
|
mixins.CreateModelMixin,
|
|
mixins.UpdateModelMixin,
|
|
mixins.DestroyModelMixin):
|
|
serializer_class = BookmarkSerializer
|
|
|
|
def get_queryset(self):
|
|
user = self.request.user
|
|
# For list action, use query set that applies search and tag projections
|
|
if self.action == 'list':
|
|
query_string = self.request.GET.get('q')
|
|
return queries.query_bookmarks(user, query_string)
|
|
|
|
# For single entity actions use default query set without projections
|
|
return Bookmark.objects.all().filter(owner=user)
|
|
|
|
def get_serializer_context(self):
|
|
return {'user': self.request.user}
|
|
|
|
|
|
class TagViewSet(viewsets.GenericViewSet,
|
|
mixins.ListModelMixin,
|
|
mixins.RetrieveModelMixin,
|
|
mixins.CreateModelMixin):
|
|
serializer_class = TagSerializer
|
|
|
|
def get_queryset(self):
|
|
user = self.request.user
|
|
return Tag.objects.all().filter(owner=user)
|
|
|
|
def get_serializer_context(self):
|
|
return {'user': self.request.user}
|
|
|
|
|
|
router = DefaultRouter()
|
|
router.register(r'bookmarks', BookmarkViewSet, basename='bookmark')
|
|
router.register(r'tags', TagViewSet, basename='tag')
|