first commit
This commit is contained in:
82
portfolio/views.py
Normal file
82
portfolio/views.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from django.core.cache import cache
|
||||
from rest_framework.generics import ListAPIView, RetrieveAPIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.Permission import ReadOnly
|
||||
from portfolio.models import Portfolio, Category
|
||||
from portfolio.serializers import PortfolioSerializer, CategorySerializer
|
||||
|
||||
CACHE_TTL_SECONDS = 60 * 5
|
||||
|
||||
|
||||
class CategoryList(ListAPIView):
|
||||
permission_classes = [ReadOnly]
|
||||
queryset = Category.objects.order_by('order').filter(is_active=True, parent__isnull=True).all()
|
||||
# serializer_class = ParentSerializer
|
||||
serializer_class = CategorySerializer
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
cache_key = 'portfolio:category_list'
|
||||
cached_data = cache.get(cache_key)
|
||||
if cached_data:
|
||||
return Response(cached_data)
|
||||
|
||||
response = super().list(request, *args, **kwargs)
|
||||
cache.set(cache_key, response.data, timeout=CACHE_TTL_SECONDS)
|
||||
return response
|
||||
|
||||
|
||||
class CategoryDetail(RetrieveAPIView):
|
||||
permission_classes = [ReadOnly]
|
||||
queryset = Category.objects.order_by('order').filter(is_active=True).all()
|
||||
serializer_class = CategorySerializer
|
||||
lookup_field = 'slug' # Slug ile arama yapılacak
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
cache_key = f"portfolio:category_detail:{kwargs.get('slug')}"
|
||||
cached_data = cache.get(cache_key)
|
||||
if cached_data:
|
||||
return Response(cached_data)
|
||||
|
||||
response = super().retrieve(request, *args, **kwargs)
|
||||
cache.set(cache_key, response.data, timeout=CACHE_TTL_SECONDS)
|
||||
return response
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
return context
|
||||
|
||||
|
||||
# Create your views here.
|
||||
class PortfolioList(ListAPIView):
|
||||
permission_classes = [ReadOnly]
|
||||
queryset = Portfolio.objects.all()
|
||||
serializer_class = PortfolioSerializer
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
cache_key = 'portfolio:portfolio_list'
|
||||
cached_data = cache.get(cache_key)
|
||||
if cached_data:
|
||||
return Response(cached_data)
|
||||
|
||||
response = super().list(request, *args, **kwargs)
|
||||
cache.set(cache_key, response.data, timeout=CACHE_TTL_SECONDS)
|
||||
return response
|
||||
|
||||
|
||||
|
||||
class PortfolioDetail(RetrieveAPIView):
|
||||
permission_classes = [ReadOnly]
|
||||
queryset = Portfolio.objects.all()
|
||||
serializer_class = PortfolioSerializer
|
||||
lookup_field = 'pk' # Portfolio modelinde slug olmadığı için pk kullanılıyor
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
cache_key = f"portfolio:portfolio_detail:{kwargs.get('pk')}"
|
||||
cached_data = cache.get(cache_key)
|
||||
if cached_data:
|
||||
return Response(cached_data)
|
||||
|
||||
response = super().retrieve(request, *args, **kwargs)
|
||||
cache.set(cache_key, response.data, timeout=CACHE_TTL_SECONDS)
|
||||
return response
|
||||
Reference in New Issue
Block a user