|
|
|
@ -15,6 +15,10 @@ from apps.course.serializers import ( |
|
|
|
ProfessorListSerializer, |
|
|
|
) |
|
|
|
from utils.pagination import StandardResultsSetPagination |
|
|
|
from django.utils.decorators import method_decorator |
|
|
|
from django.views.decorators.cache import never_cache |
|
|
|
from django.core.cache import cache |
|
|
|
from rest_framework.response import Response |
|
|
|
|
|
|
|
|
|
|
|
UserModel = get_user_model() |
|
|
|
@ -108,6 +112,25 @@ class ProfessorDetailAPIView(RetrieveAPIView): |
|
|
|
def get(self, request, *args, **kwargs): |
|
|
|
return super().get(request, *args, **kwargs) |
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs): |
|
|
|
slug = self.kwargs.get('slug') |
|
|
|
cache_key = f"professor_detail_{slug}" |
|
|
|
|
|
|
|
# Try to get the data from Redis/Cache |
|
|
|
cached_data = cache.get(cache_key) |
|
|
|
if cached_data: |
|
|
|
return Response(cached_data) |
|
|
|
|
|
|
|
# If not in cache, let DRF do the heavy lifting (DB queries, serialization) |
|
|
|
response = super().retrieve(request, *args, **kwargs) |
|
|
|
|
|
|
|
# Store the serialized data in the cache for 24 hours |
|
|
|
cache.set(cache_key, response.data, timeout=60 * 60 * 24) |
|
|
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_queryset(self): |
|
|
|
return UserModel.objects.filter(user_type=UserModel.UserType.PROFESSOR).annotate( |
|
|
|
course_count=Count('courses', distinct=True), |
|
|
|
@ -122,6 +145,11 @@ class ProfessorCourseListAPIView(ListAPIView): |
|
|
|
search_fields = ['title', 'category__name'] |
|
|
|
pagination_class = StandardResultsSetPagination |
|
|
|
|
|
|
|
@method_decorator(never_cache) |
|
|
|
def dispatch(self, request, *args, **kwargs): |
|
|
|
return super().dispatch(request, *args, **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema( |
|
|
|
operation_description='دریافت فهرست دورههای فعال یک استاد مشخصشده با اسلاگ.', |
|
|
|
tags=['Imam-Javad - Course'], |
|
|
|
|