diff --git a/social_ads_generator/processor.py b/social_ads_generator/processor.py index bb24ca7..b93c7f8 100644 --- a/social_ads_generator/processor.py +++ b/social_ads_generator/processor.py @@ -51,6 +51,7 @@ Please create platform-optimized ad copy that: request_obj.status = 'processing' request_obj.save() + # Handle array response from N8N (extract first item) if isinstance(response_data, list) and len(response_data) > 0: response_data = response_data[0] diff --git a/social_ads_generator/templates/social_ads_generator/detail.html b/social_ads_generator/templates/social_ads_generator/detail.html index ebce096..65effbf 100644 --- a/social_ads_generator/templates/social_ads_generator/detail.html +++ b/social_ads_generator/templates/social_ads_generator/detail.html @@ -102,7 +102,7 @@
- +
@@ -201,6 +201,7 @@ // Display social ads results with engaging ad copy formatting function displayResults(result) { + const resultsContainer = document.getElementById('adResults'); const contentElement = document.getElementById('adContent'); @@ -211,7 +212,7 @@ // Update wallet balance if provided if (result.wallet_balance !== undefined) { - updateWalletBalance(result.wallet_balance); + AgentUtils.updateWalletBalance(result.wallet_balance); } // Handle errors diff --git a/social_ads_generator/views.py b/social_ads_generator/views.py index 6ec7d1d..e266353 100644 --- a/social_ads_generator/views.py +++ b/social_ads_generator/views.py @@ -138,19 +138,25 @@ def social_ads_generator_status(request, request_id): # Refresh user to get current wallet balance request.user.refresh_from_db() - return JsonResponse({ + ad_copy = getattr(response, 'ad_copy', None) + raw_response = getattr(response, 'raw_response', None) + + + json_response = { 'success': response.success, 'status': agent_request.status, - 'content': getattr(response, 'ad_copy', None), - 'ad_copy_content': getattr(response, 'ad_copy', None), + 'content': ad_copy, + 'ad_copy_content': ad_copy, 'hashtags': getattr(response, 'hashtags', None), 'targeting_suggestions': getattr(response, 'targeting_suggestions', None), 'formatted_ad': getattr(response, 'formatted_ad', None), - 'raw_response': getattr(response, 'raw_response', None), + 'raw_response': raw_response, 'processing_time': float(response.processing_time) if response.processing_time else None, 'error_message': response.error_message, 'wallet_balance': float(request.user.wallet_balance) - }) + } + + return JsonResponse(json_response) else: return JsonResponse({ 'success': False, diff --git a/weather_reporter/templates/weather_reporter/detail.html b/weather_reporter/templates/weather_reporter/detail.html index 0c926a5..ddae13d 100644 --- a/weather_reporter/templates/weather_reporter/detail.html +++ b/weather_reporter/templates/weather_reporter/detail.html @@ -271,7 +271,7 @@ // Update wallet balance if provided if (result.wallet_balance !== undefined) { - updateWalletBalance(result.wallet_balance); + AgentUtils.updateWalletBalance(result.wallet_balance); } // Handle errors @@ -481,7 +481,7 @@ // Submit form via AJAX const formData = new FormData(this); - fetch(window.location.href, { + fetch('/agents/weather-reporter/process/', { method: 'POST', body: formData, headers: { diff --git a/weather_reporter/urls.py b/weather_reporter/urls.py index 75f5c32..0b535b9 100644 --- a/weather_reporter/urls.py +++ b/weather_reporter/urls.py @@ -6,5 +6,6 @@ app_name = 'weather_reporter' urlpatterns = [ path('', views.weather_reporter_detail, name='detail'), path('process/', views.WeatherReporterProcessView.as_view(), name='process'), + path('status//', views.weather_reporter_status, name='status'), path('result//', views.weather_reporter_result, name='result'), ] \ No newline at end of file diff --git a/weather_reporter/views.py b/weather_reporter/views.py index 5c77b36..484c3a1 100644 --- a/weather_reporter/views.py +++ b/weather_reporter/views.py @@ -57,8 +57,7 @@ class WeatherReporterProcessView(View): agent=agent, cost=agent.price, location=data.get('location', ''), - report_type=data.get('report_type', 'current'), - + report_type=data.get('report_type', 'current') ) # Process request immediately (API-based agent) @@ -139,6 +138,48 @@ def weather_reporter_result(request, request_id): 'message': 'Processing in progress...' }) + except WeatherReporterRequest.DoesNotExist: + return JsonResponse({'error': 'Request not found'}, status=404) + except Exception as e: + return JsonResponse({'error': str(e)}, status=500) + + +@login_required +def weather_reporter_status(request, request_id): + """Get status for a specific request (for polling)""" + try: + agent_request = WeatherReporterRequest.objects.get( + id=request_id, + user=request.user + ) + + if hasattr(agent_request, 'response'): + response = agent_request.response + # Refresh user to get current wallet balance + request.user.refresh_from_db() + + return JsonResponse({ + 'success': response.success, + 'status': agent_request.status, + 'content': getattr(response, 'weather_data', None), + 'weather_data': getattr(response, 'weather_data', None), + 'temperature': getattr(response, 'temperature', None), + 'description': getattr(response, 'description', None), + 'humidity': getattr(response, 'humidity', None), + 'wind_speed': getattr(response, 'wind_speed', None), + 'formatted_report': getattr(response, 'formatted_report', None), + 'raw_response': getattr(response, 'raw_response', None), + 'processing_time': float(response.processing_time) if response.processing_time else None, + 'error_message': response.error_message, + 'wallet_balance': float(request.user.wallet_balance) + }) + else: + return JsonResponse({ + 'success': False, + 'status': agent_request.status, + 'message': 'Processing in progress...' + }) + except WeatherReporterRequest.DoesNotExist: return JsonResponse({'error': 'Request not found'}, status=404) except Exception as e: