99from typing import get_type_hints
1010from uuid import uuid4
1111
12+ import requests
13+ from birdseye import eye
1214from django .conf import settings
1315from django .contrib .auth .mixins import LoginRequiredMixin
1416from django .contrib .messages .views import SuccessMessageMixin
1517from django .forms import ModelForm
1618from django .http import HttpResponse ,JsonResponse
1719from django .views import View
1820from django .views .generic import CreateView
19-
20- import requests
21- from birdseye import eye
2221from django_user_agents .utils import get_user_agent
23- from littleutils import select_attrs
22+ from littleutils import select_attrs , only
2423from markdown import markdown
2524from sentry_sdk import capture_exception
2625
@@ -101,6 +100,11 @@ def run_code(self, code, source, page_index, step_index):
101100birdseye_objects = result ["birdseye_objects" ]
102101if birdseye_objects :
103102functions = birdseye_objects ["functions" ]
103+ top_old_function_id = only (
104+ f ["id" ]
105+ for f in functions
106+ if f ["name" ]== "<module>"
107+ )
104108function_ids = [d .pop ('id' )for d in functions ]
105109functions = [eye .db .Function (** {** d ,'hash' :uuid4 ().hex })for d in functions ]
106110with eye .db .session_scope ()as session :
@@ -109,13 +113,16 @@ def run_code(self, code, source, page_index, step_index):
109113session .commit ()
110114function_ids = {old :func .id for old ,func in zip (function_ids ,functions )}
111115
116+ call_id = None
112117for call in birdseye_objects ["calls" ]:
113- call ["function_id" ]= function_ids [call ["function_id" ]]
118+ old_function_id = call ["function_id" ]
119+ is_top_call = old_function_id == top_old_function_id
120+ call ["function_id" ]= function_ids [old_function_id ]
114121call ["start_time" ]= datetime .fromisoformat (call ["start_time" ])
115122call = eye .db .Call (** call )
116123session .add (call )
117- # TODO get correct call from top level
118- call_id = call .id
124+ if is_top_call :
125+ call_id = call .id
119126
120127birdseye_url = f"/birdseye/ipython_call/{ call_id } "
121128