Browse Source

feat(library): enforce single category in admin and include categories in public book serializer

master
Mohsen Taba 4 days ago
parent
commit
f4d065155d
  1. 12
      apps/library/serializers.py
  2. 4
      apps/library/serializers_admin.py
  3. 3
      apps/library/urls.py
  4. 4
      apps/library/views.py

12
apps/library/serializers.py

@ -134,6 +134,8 @@ class BookSerializer(serializers.ModelSerializer):
share_link = serializers.CharField(read_only=True) share_link = serializers.CharField(read_only=True)
author = serializers.SerializerMethodField() author = serializers.SerializerMethodField()
author_detail = AuthorSummarySerializer(source='author', read_only=True) author_detail = AuthorSummarySerializer(source='author', read_only=True)
categories = CategorySerializer(many=True, read_only=True)
category = serializers.SerializerMethodField()
language = serializers.SlugRelatedField( language = serializers.SlugRelatedField(
slug_field='code', slug_field='code',
queryset=Language.objects.all(), queryset=Language.objects.all(),
@ -149,8 +151,8 @@ class BookSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Book model = Book
fields = ( fields = (
'id', 'title', 'slug', 'summary', 'summary_title', 'thumbnail', 'slogan',
'status', 'pin', 'view_count', 'download_count', 'publisher', 'year_of_publication', 'author', 'author_detail', 'isbn', 'numnber_of_volume',
'id', 'title', 'slug', 'summary', 'summary_title', 'description', 'thumbnail', 'slogan',
'status', 'pin', 'view_count', 'download_count', 'publisher', 'year_of_publication', 'author', 'author_detail', 'categories', 'category', 'isbn', 'numnber_of_volume',
'language', 'main_themes', 'notable_works', 'language', 'main_themes', 'notable_works',
'file_type', 'book_file', 'created_at', 'bookmark', 'user_rate', 'file_type', 'book_file', 'created_at', 'bookmark', 'user_rate',
'average_rate', 'share_link' 'average_rate', 'share_link'
@ -159,6 +161,12 @@ class BookSerializer(serializers.ModelSerializer):
def get_author(self, obj): def get_author(self, obj):
return obj.author.full_name if obj.author_id else None return obj.author.full_name if obj.author_id else None
def get_category(self, obj):
first_cat = obj.categories.filter(status=True).first() or obj.categories.first()
if first_cat:
return CategorySerializer(first_cat, context=self.context).data
return None
def get_bookmark(self, obj): def get_bookmark(self, obj):
""" """
Get bookmark information for this book. Get bookmark information for this book.

4
apps/library/serializers_admin.py

@ -253,7 +253,7 @@ class AdminBookDetailSerializer(serializers.ModelSerializer):
collections = validated_data.pop("collections", []) collections = validated_data.pop("collections", [])
book = super().create(validated_data) book = super().create(validated_data)
if categories: if categories:
book.categories.set(categories)
book.categories.set(categories[:1] if len(categories) > 1 else categories)
if collections: if collections:
book.collections.set(collections) book.collections.set(collections)
return book return book
@ -274,7 +274,7 @@ class AdminBookDetailSerializer(serializers.ModelSerializer):
book = super().update(instance, validated_data) book = super().update(instance, validated_data)
if categories is not None: if categories is not None:
book.categories.set(categories)
book.categories.set(categories[:1] if len(categories) > 1 else categories)
if collections is not None: if collections is not None:
book.collections.set(collections) book.collections.set(collections)
return book return book

3
apps/library/urls.py

@ -39,10 +39,11 @@ admin_router.register(r'collections', AdminLibraryCollectionViewSet, basename='a
admin_router.register(r'dovodi-collections', DovodiAdminLibraryCollectionViewSet, basename='admin-library-dovodi-collections') admin_router.register(r'dovodi-collections', DovodiAdminLibraryCollectionViewSet, basename='admin-library-dovodi-collections')
dovodi_router = SimpleRouter() dovodi_router = SimpleRouter()
dovodi_router.register(r'categories', DovodiAdminLibraryCategoryViewSet, basename='dovodi-library-categories')
dovodi_router.register(r'collections', DovodiAdminLibraryCollectionViewSet, basename='dovodi-library-collections') dovodi_router.register(r'collections', DovodiAdminLibraryCollectionViewSet, basename='dovodi-library-collections')
# Hide admin viewsets from swagger # Hide admin viewsets from swagger
for prefix, viewset, basename in admin_router.registry:
for prefix, viewset, basename in admin_router.registry + dovodi_router.registry:
viewset.swagger_schema = None viewset.swagger_schema = None
urlpatterns = [ urlpatterns = [

4
apps/library/views.py

@ -120,7 +120,7 @@ class BookListView(ListAPIView):
return context return context
def get_queryset(self): def get_queryset(self):
queryset = Book.objects.filter(status=True).select_related('author', 'language')
queryset = Book.objects.filter(status=True).select_related('author', 'language').prefetch_related('categories')
# Filter by collection if provided # Filter by collection if provided
collection_id = self.request.query_params.get('collection_id') collection_id = self.request.query_params.get('collection_id')
@ -226,7 +226,7 @@ class BookDetailView(Gone410ViewMixin, CanonicalSlugViewSetMixin, RetrieveAPIVie
serializer_class = BookSerializer serializer_class = BookSerializer
permission_classes = (IsAuthenticated,) permission_classes = (IsAuthenticated,)
authentication_classes = [TokenAuthentication] authentication_classes = [TokenAuthentication]
queryset = Book.objects.all()
queryset = Book.objects.all().select_related('author').prefetch_related('categories')
lookup_field = 'slug' lookup_field = 'slug'
active_field_name = 'status' active_field_name = 'status'

Loading…
Cancel
Save