from django.urls import include, path from django.views.decorators.cache import cache_page from django.views.decorators.vary import vary_on_headers from rest_framework.routers import SimpleRouter from apps.library.views import ( CategoryListView, PinnedBookCollectionListView, MiddleBookCollectionListView, BookListView, BookDetailView, AuthorListView, AuthorDetailView, AuthorBookListView, DownloadedBooksListView, BookDownloadCreateAPIView, ) from functools import wraps # Helper function to cache public views def cached_view(view_func): return cache_page(60 * 60 * 2, key_prefix='library_api')(vary_on_headers('Accept-Language')(view_func)) # Helper to conditionally cache list views (bypasses cache when is_bookmark=true) def conditional_cached_view(timeout, key_prefix): def decorator(view_func): cached_func = cache_page(timeout, key_prefix=key_prefix)(vary_on_headers('Accept-Language')(view_func)) @wraps(view_func) def _wrapped_view(request, *args, **kwargs): is_bookmark = (request.GET.get('is_bookmark') or request.GET.get('is_bookmarked') or '').lower() if is_bookmark in ('true', '1'): return view_func(request, *args, **kwargs) return cached_func(request, *args, **kwargs) return _wrapped_view return decorator from apps.library.views_admin import ( AdminAuthorViewSet, AdminBookViewSet, AdminLibraryCategoryViewSet, AdminLibraryCollectionViewSet, DovodiAdminLibraryCategoryViewSet, DovodiAdminLibraryCollectionViewSet, ) admin_router = SimpleRouter() admin_router.register(r'books', AdminBookViewSet, basename='admin-library-books') admin_router.register(r'authors', AdminAuthorViewSet, basename='admin-library-authors') admin_router.register(r'categories', AdminLibraryCategoryViewSet, basename='admin-library-categories') admin_router.register(r'dovodi-categories', DovodiAdminLibraryCategoryViewSet, basename='admin-library-dovodi-categories') admin_router.register(r'collections', AdminLibraryCollectionViewSet, basename='admin-library-collections') admin_router.register(r'dovodi-collections', DovodiAdminLibraryCollectionViewSet, basename='admin-library-dovodi-collections') dovodi_router = SimpleRouter() dovodi_router.register(r'collections', DovodiAdminLibraryCollectionViewSet, basename='dovodi-library-collections') # Hide admin viewsets from swagger for prefix, viewset, basename in admin_router.registry: viewset.swagger_schema = None urlpatterns = [ path('admin/', include(admin_router.urls)), path('dovodi/', include(dovodi_router.urls)), path('categories/', cached_view(CategoryListView.as_view()), name='category-list'), path('pinned-collections/', cached_view(PinnedBookCollectionListView.as_view()), name='pinned-collection-list'), path('collections/', cached_view(MiddleBookCollectionListView.as_view()), name='collection-list'), path('authors/', cached_view(AuthorListView.as_view()), name='author-list'), path('authors//', AuthorDetailView.as_view(), name='author-detail'), path('authors//books/', AuthorBookListView.as_view(), name='author-books'), path('books/', conditional_cached_view(60 * 30, 'library_books')(BookListView.as_view()), name='book-list'), path('books//', BookDetailView.as_view(), name='book-detail'), path('books/downloaded/', DownloadedBooksListView.as_view(), name='downloaded-books-list'), path('books/download/', BookDownloadCreateAPIView.as_view(), name='book-download'), ]