@@ -110,38 +110,49 @@ def get_chart_image_response(chart_image: ResourceChartImage) -> HttpResponse:
110110
111111async def download (request : HttpRequest , type : str , id : uuid .UUID ) -> HttpResponse :
112112 """Handle download requests for resources, chart images, and charts."""
113- if type == "resource" :
114- try :
115- resource = await get_resource (id )
116- return await get_resource_response (resource , request )
117- except Resource .DoesNotExist :
118- return JsonResponse ({"error" : "Resource not found" }, status = 404 )
119- except Exception as e :
120- return JsonResponse ({"error" : str (e )}, status = 500 )
121-
122- elif type == "chart_image" :
123- try :
124- chart_image = await get_chart_image (id )
125- return await get_chart_image_response (chart_image )
126- except ResourceChartImage .DoesNotExist :
127- return JsonResponse ({"error" : "Chart image not found" }, status = 404 )
128- except Exception as e :
129- return JsonResponse ({"error" : str (e )}, status = 500 )
130-
131- elif type == "chart" :
132- try :
133- # Fetch the chart asynchronously
134- resource_chart = await get_resource_chart (id )
135-
136- # Assuming generate_chart is an async function
137- response = await generate_chart (resource_chart )
138- response ["Content-Disposition" ] = 'attachment; filename="chart.png"'
139- return response
140-
141- except ObjectDoesNotExist :
142- return HttpResponse ("Chart not found" , content_type = "text/plain" )
143-
144- return HttpResponse ("Invalid type" , content_type = "text/plain" )
113+ try :
114+ if type == "resource" :
115+ try :
116+ resource = await get_resource (id )
117+ return await get_resource_response (resource , request )
118+ except Resource .DoesNotExist :
119+ return JsonResponse ({"error" : "Resource not found" }, status = 404 )
120+ except Exception as e :
121+ return JsonResponse ({"error" : str (e )}, status = 500 )
122+
123+ elif type == "chart_image" :
124+ try :
125+ chart_image = await get_chart_image (id )
126+ return await get_chart_image_response (chart_image )
127+ except ResourceChartImage .DoesNotExist :
128+ return JsonResponse ({"error" : "Chart image not found" }, status = 404 )
129+ except Exception as e :
130+ return JsonResponse ({"error" : str (e )}, status = 500 )
131+
132+ elif type == "chart" :
133+ try :
134+ # Fetch the chart asynchronously
135+ resource_chart = await get_resource_chart (id )
136+
137+ # Assuming generate_chart is an async function
138+ response = await generate_chart (resource_chart )
139+ response ["Content-Disposition" ] = 'attachment; filename="chart.png"'
140+ return response
141+
142+ except ObjectDoesNotExist :
143+ return HttpResponse ("Chart not found" , content_type = "text/plain" )
144+
145+ return HttpResponse ("Invalid type" , content_type = "text/plain" )
146+ except RuntimeError as e :
147+ if "no running event loop" in str (e ):
148+ import asyncio
149+
150+ # Create a new event loop if one doesn't exist
151+ loop = asyncio .new_event_loop ()
152+ asyncio .set_event_loop (loop )
153+ # Re-run the function with the new loop
154+ return asyncio .run (download (request , type , id ))
155+ raise
145156
146157
147158def get_file_resource_response (resource : Resource ) -> HttpResponse :
0 commit comments