You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.8 KiB
55 lines
2.8 KiB
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 AdminArticleCategoryViewSet, AdminArticleContentViewSet, AdminArticleViewSet
|
|
|
|
# Helper function to cache public views
|
|
def cached_view(view_func):
|
|
return cache_page(60 * 60 * 2, key_prefix='article_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 = 'article'
|
|
|
|
admin_router = SimpleRouter()
|
|
admin_router.register(r'articles', AdminArticleViewSet, basename='admin-articles')
|
|
admin_router.register(r'categories', AdminArticleCategoryViewSet, basename='admin-article-categories')
|
|
admin_router.register(r'contents', AdminArticleContentViewSet, basename='admin-article-contents')
|
|
|
|
# Hide admin viewsets from swagger
|
|
for prefix, viewset, basename in admin_router.registry:
|
|
viewset.swagger_schema = None
|
|
|
|
dovodi_router = SimpleRouter()
|
|
dovodi_router.register(r'categories', DovodiArticleCategoryViewSet, basename='dovodi-article-category')
|
|
dovodi_router.register(r'collections', DovodiArticleCollectionViewSet, basename='dovodi-article-collection')
|
|
|
|
urlpatterns = [
|
|
path('dovodi/', include(dovodi_router.urls)),
|
|
path('categories/', cached_view(ArticleCategoryListAPIView.as_view()), name='category-list'),
|
|
path('pinned-collections/', cached_view(PinnedArticleCollectionListView.as_view()), name='pinned-collection-list'),
|
|
path('collections/', cached_view(MiddleArticleCollectionListView.as_view()), name='collection-list'),
|
|
|
|
path('list/', conditional_cached_view(60 * 30, 'article_list')(ArticleListAPIView.as_view()), name='article-list'),
|
|
path('detail/<str:slug>/', ArticleDetailAPIView.as_view(), name='article-detail'),
|
|
re_path(r'detail/(?P<slug>.+)/$', ArticleDetailAPIView.as_view(), name='podcast-detail'),
|
|
path('admin/', include(admin_router.urls)),
|
|
|
|
# # User playlist endpoints
|
|
# path('user-playlist/', UserPlaylistCreateAPIView.as_view(), name='user-playlist-create'),
|
|
# path('user-playlist/list/', UserPlaylistListAPIView.as_view(), name='user-playlist-list'),
|
|
]
|