106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
"""
|
|
Serializers for Social Media Comments app
|
|
"""
|
|
from rest_framework import serializers
|
|
from .models import SocialMediaComment, SocialPlatform
|
|
|
|
|
|
class SocialMediaCommentSerializer(serializers.ModelSerializer):
|
|
"""Serializer for SocialMediaComment model with bilingual AI analysis"""
|
|
|
|
platform_display = serializers.CharField(source='get_platform_display', read_only=True)
|
|
is_analyzed = serializers.ReadOnlyField()
|
|
sentiment_classification_en = serializers.SerializerMethodField()
|
|
sentiment_classification_ar = serializers.SerializerMethodField()
|
|
sentiment_score = serializers.SerializerMethodField()
|
|
confidence = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = SocialMediaComment
|
|
fields = [
|
|
'id',
|
|
'platform',
|
|
'platform_display',
|
|
'comment_id',
|
|
'comments',
|
|
'author',
|
|
'raw_data',
|
|
'post_id',
|
|
'media_url',
|
|
'like_count',
|
|
'reply_count',
|
|
'rating',
|
|
'published_at',
|
|
'scraped_at',
|
|
'ai_analysis',
|
|
'is_analyzed',
|
|
'sentiment_classification_en',
|
|
'sentiment_classification_ar',
|
|
'sentiment_score',
|
|
'confidence',
|
|
]
|
|
read_only_fields = [
|
|
'scraped_at',
|
|
]
|
|
|
|
def get_sentiment_classification_en(self, obj):
|
|
"""Get English sentiment classification"""
|
|
if not obj.ai_analysis:
|
|
return None
|
|
return obj.ai_analysis.get('sentiment', {}).get('classification', {}).get('en')
|
|
|
|
def get_sentiment_classification_ar(self, obj):
|
|
"""Get Arabic sentiment classification"""
|
|
if not obj.ai_analysis:
|
|
return None
|
|
return obj.ai_analysis.get('sentiment', {}).get('classification', {}).get('ar')
|
|
|
|
def get_sentiment_score(self, obj):
|
|
"""Get sentiment score"""
|
|
if not obj.ai_analysis:
|
|
return None
|
|
return obj.ai_analysis.get('sentiment', {}).get('score')
|
|
|
|
def get_confidence(self, obj):
|
|
"""Get confidence score"""
|
|
if not obj.ai_analysis:
|
|
return None
|
|
return obj.ai_analysis.get('sentiment', {}).get('confidence')
|
|
|
|
def validate_platform(self, value):
|
|
"""Validate platform choice"""
|
|
if value not in SocialPlatform.values:
|
|
raise serializers.ValidationError(f"Invalid platform. Must be one of: {', '.join(SocialPlatform.values)}")
|
|
return value
|
|
|
|
|
|
class SocialMediaCommentListSerializer(serializers.ModelSerializer):
|
|
"""Lightweight serializer for list views"""
|
|
|
|
platform_display = serializers.CharField(source='get_platform_display', read_only=True)
|
|
is_analyzed = serializers.ReadOnlyField()
|
|
sentiment = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = SocialMediaComment
|
|
fields = [
|
|
'id',
|
|
'platform',
|
|
'platform_display',
|
|
'comment_id',
|
|
'comments',
|
|
'author',
|
|
'like_count',
|
|
'reply_count',
|
|
'rating',
|
|
'published_at',
|
|
'is_analyzed',
|
|
'sentiment',
|
|
]
|
|
|
|
def get_sentiment(self, obj):
|
|
"""Get sentiment classification (English)"""
|
|
if not obj.ai_analysis:
|
|
return None
|
|
return obj.ai_analysis.get('sentiment', {}).get('classification', {}).get('en')
|