Files
atabackend/portfolio/views.py
Beyhan Oğur d50f14bcb1 first commit
2026-04-26 22:20:45 +03:00

83 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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