Updated iamge prompt flow adn frotnend backend
This commit is contained in:
@@ -2,7 +2,8 @@ from rest_framework import viewsets, filters, status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django.db import transaction
|
||||
from django.db import transaction, models
|
||||
from django.db.models import Q
|
||||
from igny8_core.api.base import SiteSectorModelViewSet
|
||||
from igny8_core.api.pagination import CustomPageNumberPagination
|
||||
from .models import Tasks, Images, Content
|
||||
@@ -348,15 +349,15 @@ class TasksViewSet(SiteSectorModelViewSet):
|
||||
|
||||
class ImagesViewSet(SiteSectorModelViewSet):
|
||||
"""
|
||||
ViewSet for managing task images
|
||||
ViewSet for managing content images
|
||||
"""
|
||||
queryset = Images.objects.all()
|
||||
serializer_class = ImagesSerializer
|
||||
|
||||
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
|
||||
ordering_fields = ['created_at', 'position']
|
||||
ordering = ['task', 'position', '-created_at']
|
||||
filterset_fields = ['task_id', 'image_type', 'status']
|
||||
ordering = ['content', 'position', '-created_at']
|
||||
filterset_fields = ['task_id', 'content_id', 'image_type', 'status']
|
||||
|
||||
def perform_create(self, serializer):
|
||||
"""Override to automatically set account"""
|
||||
@@ -432,6 +433,86 @@ class ImagesViewSet(SiteSectorModelViewSet):
|
||||
'error': f'Failed to start image generation: {str(e)}',
|
||||
'type': 'TaskError'
|
||||
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
@action(detail=False, methods=['get'], url_path='content_images', url_name='content_images')
|
||||
def content_images(self, request):
|
||||
"""Get images grouped by content - one row per content with featured and in-article images"""
|
||||
from .serializers import ContentImagesGroupSerializer, ContentImageSerializer
|
||||
|
||||
account = getattr(request, 'account', None)
|
||||
|
||||
# Get all content that has images (either directly or via task)
|
||||
# First, get content with direct image links
|
||||
queryset = Content.objects.filter(images__isnull=False)
|
||||
if account:
|
||||
queryset = queryset.filter(account=account)
|
||||
|
||||
# Also get content from images linked via task
|
||||
task_linked_images = Images.objects.filter(task__isnull=False, content__isnull=True)
|
||||
if account:
|
||||
task_linked_images = task_linked_images.filter(account=account)
|
||||
|
||||
# Get content IDs from task-linked images
|
||||
task_content_ids = set()
|
||||
for image in task_linked_images:
|
||||
if image.task and hasattr(image.task, 'content_record'):
|
||||
try:
|
||||
content = image.task.content_record
|
||||
if content:
|
||||
task_content_ids.add(content.id)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Combine both sets of content IDs
|
||||
content_ids = set(queryset.values_list('id', flat=True).distinct())
|
||||
content_ids.update(task_content_ids)
|
||||
|
||||
# Build grouped response
|
||||
grouped_data = []
|
||||
for content_id in content_ids:
|
||||
try:
|
||||
content = Content.objects.get(id=content_id)
|
||||
# Get images linked directly to content OR via task
|
||||
content_images = Images.objects.filter(
|
||||
Q(content=content) | Q(task=content.task)
|
||||
).order_by('position')
|
||||
|
||||
# Get featured image
|
||||
featured_image = content_images.filter(image_type='featured').first()
|
||||
|
||||
# Get in-article images (sorted by position)
|
||||
in_article_images = list(content_images.filter(image_type='in_article').order_by('position'))
|
||||
|
||||
# Determine overall status
|
||||
all_images = list(content_images)
|
||||
if not all_images:
|
||||
overall_status = 'pending'
|
||||
elif all(img.status == 'generated' for img in all_images):
|
||||
overall_status = 'complete'
|
||||
elif any(img.status == 'failed' for img in all_images):
|
||||
overall_status = 'failed'
|
||||
elif any(img.status == 'generated' for img in all_images):
|
||||
overall_status = 'partial'
|
||||
else:
|
||||
overall_status = 'pending'
|
||||
|
||||
grouped_data.append({
|
||||
'content_id': content.id,
|
||||
'content_title': content.title or content.meta_title or f"Content #{content.id}",
|
||||
'featured_image': ContentImageSerializer(featured_image).data if featured_image else None,
|
||||
'in_article_images': [ContentImageSerializer(img).data for img in in_article_images],
|
||||
'overall_status': overall_status,
|
||||
})
|
||||
except Content.DoesNotExist:
|
||||
continue
|
||||
|
||||
# Sort by content title
|
||||
grouped_data.sort(key=lambda x: x['content_title'])
|
||||
|
||||
return Response({
|
||||
'count': len(grouped_data),
|
||||
'results': grouped_data
|
||||
}, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class ContentViewSet(SiteSectorModelViewSet):
|
||||
|
||||
Reference in New Issue
Block a user