Files
igny8/backend/igny8_core/modules/writer/serializers.py
IGNY8 VPS (Salman) 8b6e18649c Refactor content handling in GenerateContentFunction and update related models and serializers
- Enhanced GenerateContentFunction to save content in a dedicated Content model, separating it from the Tasks model.
- Updated Tasks model to remove SEO-related fields, now managed in the Content model.
- Modified TasksSerializer to include new content fields and adjusted the API to reflect these changes.
- Improved the auto_generate_content_task method to utilize the new save_output method for better content management.
- Updated frontend components to display new content structure and metadata effectively.
2025-11-10 14:06:15 +00:00

181 lines
5.4 KiB
Python

from rest_framework import serializers
from .models import Tasks, Images, Content
from igny8_core.modules.planner.models import Clusters, ContentIdeas
class TasksSerializer(serializers.ModelSerializer):
"""Serializer for Tasks model"""
cluster_name = serializers.SerializerMethodField()
sector_name = serializers.SerializerMethodField()
idea_title = serializers.SerializerMethodField()
site_id = serializers.IntegerField(write_only=True, required=False)
sector_id = serializers.IntegerField(write_only=True, required=False)
content_html = serializers.SerializerMethodField()
content_primary_keyword = serializers.SerializerMethodField()
content_secondary_keywords = serializers.SerializerMethodField()
content_tags = serializers.SerializerMethodField()
content_categories = serializers.SerializerMethodField()
class Meta:
model = Tasks
fields = [
'id',
'title',
'description',
'keywords',
'cluster_id',
'cluster_name',
'sector_name',
'idea_id',
'idea_title',
'content_structure',
'content_type',
'status',
'content',
'word_count',
'meta_title',
'meta_description',
'content_html',
'content_primary_keyword',
'content_secondary_keywords',
'content_tags',
'content_categories',
'assigned_post_id',
'post_url',
'created_at',
'updated_at',
'site_id',
'sector_id',
'account_id',
]
read_only_fields = ['id', 'created_at', 'updated_at', 'account_id']
def get_cluster_name(self, obj):
"""Get cluster name from Clusters model"""
if obj.cluster_id:
try:
cluster = Clusters.objects.get(id=obj.cluster_id)
return cluster.name
except Clusters.DoesNotExist:
return None
return None
def get_sector_name(self, obj):
"""Get sector name from Sector model"""
if obj.sector_id:
try:
from igny8_core.auth.models import Sector
sector = Sector.objects.get(id=obj.sector_id)
return sector.name
except Sector.DoesNotExist:
return None
return None
def get_idea_title(self, obj):
"""Get idea title from ContentIdeas model"""
if obj.idea_id:
try:
idea = ContentIdeas.objects.get(id=obj.idea_id)
return idea.idea_title
except ContentIdeas.DoesNotExist:
return None
return None
def _get_content_record(self, obj):
try:
return obj.content_record
except AttributeError:
return None
def get_content_html(self, obj):
record = self._get_content_record(obj)
return record.html_content if record else None
def get_content_primary_keyword(self, obj):
record = self._get_content_record(obj)
return record.primary_keyword if record else None
def get_content_secondary_keywords(self, obj):
record = self._get_content_record(obj)
return record.secondary_keywords if record else []
def get_content_tags(self, obj):
record = self._get_content_record(obj)
return record.tags if record else []
def get_content_categories(self, obj):
record = self._get_content_record(obj)
return record.categories if record else []
class ImagesSerializer(serializers.ModelSerializer):
"""Serializer for Images model"""
task_title = serializers.SerializerMethodField()
class Meta:
model = Images
fields = [
'id',
'task_id',
'task_title',
'image_type',
'image_url',
'image_path',
'prompt',
'status',
'position',
'created_at',
'updated_at',
'account_id',
]
read_only_fields = ['id', 'created_at', 'updated_at', 'account_id']
def get_task_title(self, obj):
"""Get task title"""
if obj.task_id:
try:
task = Tasks.objects.get(id=obj.task_id)
return task.title
except Tasks.DoesNotExist:
return None
return None
class ContentSerializer(serializers.ModelSerializer):
"""Serializer for Content model"""
task_title = serializers.SerializerMethodField()
class Meta:
model = Content
fields = [
'id',
'task_id',
'task_title',
'html_content',
'word_count',
'metadata',
'title',
'meta_title',
'meta_description',
'primary_keyword',
'secondary_keywords',
'tags',
'categories',
'status',
'generated_at',
'updated_at',
'account_id',
]
read_only_fields = ['id', 'generated_at', 'updated_at', 'account_id']
def get_task_title(self, obj):
"""Get task title"""
if obj.task_id:
try:
task = Tasks.objects.get(id=obj.task_id)
return task.title
except Tasks.DoesNotExist:
return None
return None