first commit
This commit is contained in:
153
blog/serializers.py
Normal file
153
blog/serializers.py
Normal file
@@ -0,0 +1,153 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from blog.models import PCategory, Post, PComment
|
||||
from blog.tasks import send_comment_notification_email
|
||||
|
||||
|
||||
class CateSerializer(serializers.ModelSerializer):
|
||||
parent = serializers.StringRelatedField() # ID yerine __str__ metodundaki değeri döndürür
|
||||
|
||||
class Meta:
|
||||
model = PCategory
|
||||
fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'image', 'keywords', 'description']
|
||||
|
||||
|
||||
|
||||
|
||||
class CommentSerializer(serializers.ModelSerializer):
|
||||
child = serializers.SerializerMethodField()
|
||||
user = serializers.StringRelatedField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = PComment
|
||||
fields = ['id', 'user', 'post', 'title', 'body', 'created_at', 'slug', 'parent', 'child']
|
||||
read_only_fields = ['slug', 'created_at', 'user']
|
||||
|
||||
def get_child(self, obj):
|
||||
# Sadece aktif alt yorumları getir
|
||||
children = obj.child.filter(is_active=True).order_by('created_at')
|
||||
return CommentSerializer(children, many=True).data
|
||||
|
||||
def create(self, validated_data):
|
||||
# Kullanıcıyı request'ten al
|
||||
request = self.context.get('request')
|
||||
if request and hasattr(request, 'user'):
|
||||
validated_data['user'] = request.user
|
||||
|
||||
instance = super().create(validated_data)
|
||||
|
||||
# Celery task'ini tetikle
|
||||
# Kullanıcı email'i varsa al, yoksa username kullan
|
||||
user_email = instance.user.email if instance.user.email else instance.user.username
|
||||
|
||||
send_comment_notification_email.delay(
|
||||
comment_title=instance.title,
|
||||
comment_body=instance.body,
|
||||
post_title=instance.post.title,
|
||||
user_email=user_email
|
||||
)
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class PostSerializer(serializers.ModelSerializer):
|
||||
categories = CateSerializer(read_only=True, many=True)
|
||||
comments = serializers.SerializerMethodField()
|
||||
image = serializers.SerializerMethodField()
|
||||
thumb = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = ['id','title', 'content', 'categories', 'keywords', 'image', 'thumb', 'video',
|
||||
'slug', 'created_at', 'updated_at', 'is_active', 'is_front', 'comments']
|
||||
# fields = '__all__'
|
||||
|
||||
def get_image(self, obj):
|
||||
if obj.image:
|
||||
# Sadece path kısmını döndür (media/ ile başlayan kısım)
|
||||
url = obj.image.url
|
||||
# URL'de domain varsa çıkar, yoksa olduğu gibi döndür
|
||||
if 'http://' in url or 'https://' in url:
|
||||
# URL'den sadece path kısmını al
|
||||
from urllib.parse import urlparse
|
||||
parsed = urlparse(url)
|
||||
return parsed.path
|
||||
return url
|
||||
return None
|
||||
|
||||
def get_thumb(self, obj):
|
||||
if obj.thumb:
|
||||
# Sadece path kısmını döndür (media/ ile başlayan kısım)
|
||||
url = obj.thumb.url
|
||||
# URL'de domain varsa çıkar, yoksa olduğu gibi döndür
|
||||
if 'http://' in url or 'https://' in url:
|
||||
# URL'den sadece path kısmını al
|
||||
from urllib.parse import urlparse
|
||||
parsed = urlparse(url)
|
||||
return parsed.path
|
||||
return url
|
||||
return None
|
||||
|
||||
def get_comments(self, obj):
|
||||
# Sadece ana yorumları (parent=None) ve aktif olanları getir
|
||||
comments = obj._post.filter(parent__isnull=True, is_active=True).order_by('-created_at')
|
||||
return CommentSerializer(comments, many=True).data
|
||||
|
||||
|
||||
class PostSYalinerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = ['slug', ]
|
||||
# fields = '__all__'
|
||||
|
||||
|
||||
class CategorySerializer(serializers.ModelSerializer):
|
||||
|
||||
posts = PostSYalinerializer(source='c_categories', read_only=True, many=True)
|
||||
child = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = PCategory
|
||||
fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'image', 'keywords', 'description',
|
||||
'posts', 'child']
|
||||
|
||||
def get_child(self, obj):
|
||||
serializer = self.__class__(obj.child.all(), many=True, context=self.context)
|
||||
return serializer.data
|
||||
|
||||
|
||||
class CategoryPostSerializer(serializers.ModelSerializer):
|
||||
|
||||
posts = serializers.SerializerMethodField()
|
||||
child = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = PCategory
|
||||
fields = ['title', 'parent', 'is_active', 'created_at', 'order', 'slug', 'image', 'keywords', 'description',
|
||||
'posts', 'child']
|
||||
|
||||
def get_posts(self, obj):
|
||||
# Pagination context'ini al
|
||||
paginator = self.context.get('paginator')
|
||||
request = self.context.get('request')
|
||||
|
||||
posts = obj.c_categories.all()
|
||||
|
||||
if paginator and request:
|
||||
# Pagination uygula
|
||||
paginated_posts = paginator.paginate_queryset(posts, request)
|
||||
serializer = PostSerializer(paginated_posts, many=True, context=self.context)
|
||||
return {
|
||||
'results': serializer.data,
|
||||
'count': posts.count(),
|
||||
'next': paginator.get_next_link(),
|
||||
'previous': paginator.get_previous_link(),
|
||||
}
|
||||
else:
|
||||
# Pagination yoksa normal döndür
|
||||
serializer = PostSerializer(posts, many=True, context=self.context)
|
||||
return serializer.data
|
||||
|
||||
def get_child(self, obj):
|
||||
serializer = self.__class__(obj.child.all(), many=True, context=self.context)
|
||||
return serializer.data
|
||||
Reference in New Issue
Block a user