from functools import wraps from django.urls import include, path, re_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 .views import * from .views_admin import * # Helper function to cache public views def cached_view(view_func): return cache_page(60 * 60 * 2, key_prefix='video_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 app_name = 'video' admin_router = SimpleRouter() admin_router.register(r'videos', AdminVideoViewSet, basename='admin-video') admin_router.register(r'playlists', AdminVideoPlaylistViewSet, basename='admin-video-playlist') admin_router.register(r'categories', AdminVideoCategoryViewSet, basename='admin-video-category') admin_router.register(r'collections', AdminVideoCollectionViewSet, basename='admin-video-collection') # Hide all admin viewsets from swagger for prefix, viewset, basename in admin_router.registry: viewset.swagger_schema = None dovodi_router = SimpleRouter() dovodi_router.register(r'categories', DovodiVideoCategoryViewSet, basename='dovodi-video-category') dovodi_router.register(r'collections', DovodiVideoCollectionViewSet, basename='dovodi-video-collection') dovodi_router.register(r'items', DovodiVideoItemViewSet, basename='dovodi-video-item') urlpatterns = [ path('admin/', include(admin_router.urls)), path('dovodi/', include(dovodi_router.urls)), path('categories/', cached_view(VideoCategoryListAPIView.as_view()), name='category-list'), path('pinned-collections/', cached_view(PinnedVideoCollectionListView.as_view()), name='pinned-collection-list'), path('collections/', cached_view(MiddleVideoCollectionListView.as_view()), name='collection-list'), path('playlists/', conditional_cached_view(60 * 30, 'video_playlists')(VideoPlaylistListAPIView.as_view()), name='playlist-list'), re_path(r'playlists/(?P[\w-]+)/$', VideoPlaylistDetailAPIView.as_view(), name='playlist-detail'), # Keep old video endpoints for backward compatibility if needed path('list/', conditional_cached_view(60 * 30, 'video_list')(VideoListAPIView.as_view()), name='video-list'), re_path(r'detail/(?P[\w-]+)/$', VideoDetailAPIView.as_view(), name='video-detail'), path('resolve-youtube/', ResolveYouTubeStreamURLAPIView.as_view(), name='resolve-youtube'), ]