From 06c9d1daa591fca21f37ec787472a6da7fd5d6e4 Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Wed, 31 Jan 2024 21:57:25 +0530 Subject: [PATCH] admin niceties for saved runs --- bots/admin.py | 31 ++++++++++++++++++++++++------- bots/models.py | 10 +++++++++- daras_ai_v2/base.py | 6 ++---- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/bots/admin.py b/bots/admin.py index e659865a8..49321ba35 100644 --- a/bots/admin.py +++ b/bots/admin.py @@ -219,6 +219,7 @@ class PublishedRunAdmin(admin.ModelAdmin): "view_user", "open_in_gooey", "linked_saved_run", + "view_runs", "created_at", "updated_at", ] @@ -227,6 +228,7 @@ class PublishedRunAdmin(admin.ModelAdmin): autocomplete_fields = ["saved_run", "created_by", "last_edited_by"] readonly_fields = [ "open_in_gooey", + "view_runs", "created_at", "updated_at", ] @@ -243,19 +245,28 @@ def linked_saved_run(self, published_run: PublishedRun): linked_saved_run.short_description = "Linked Run" + @admin.display(description="View Runs") + def view_runs(self, published_run: PublishedRun): + return list_related_html_url( + SavedRun.objects.filter(parent_version__published_run=published_run), + query_param="parent_version__published_run__id__exact", + instance_id=published_run.id, + show_add=False, + ) + @admin.register(SavedRun) class SavedRunAdmin(admin.ModelAdmin): list_display = [ "__str__", - "example_id", "run_id", "view_user", - "created_at", + "open_in_gooey", + "view_parent_published_run", "run_time", - "updated_at", "price", - "preview_input", + "created_at", + "updated_at", ] list_filter = ["workflow"] search_fields = ["workflow", "example_id", "run_id", "uid"] @@ -278,6 +289,11 @@ class SavedRunAdmin(admin.ModelAdmin): django.db.models.JSONField: {"widget": JSONEditorWidget}, } + def lookup_allowed(self, key, value): + if key in ["parent_version__published_run__id__exact"]: + return True + return super().lookup_allowed(key, value) + def view_user(self, saved_run: SavedRun): return change_obj_url( AppUser.objects.get(uid=saved_run.uid), @@ -291,9 +307,10 @@ def view_bots(self, saved_run: SavedRun): view_bots.short_description = "View Bots" - @admin.display(description="Input") - def preview_input(self, saved_run: SavedRun): - return truncate_text_words(BasePage.preview_input(saved_run.state) or "", 100) + @admin.display(description="View Published Run") + def view_parent_published_run(self, saved_run: SavedRun): + pr = saved_run.parent_published_run() + return pr and change_obj_url(pr) @admin.register(PublishedRunVersion) diff --git a/bots/models.py b/bots/models.py index 3ad94cd31..fd6071f58 100644 --- a/bots/models.py +++ b/bots/models.py @@ -280,7 +280,15 @@ class Meta: ] def __str__(self): - return self.get_app_url() + from daras_ai_v2.breadcrumbs import get_title_breadcrumbs + + title = get_title_breadcrumbs( + Workflow(self.workflow).page_cls, self, self.parent_published_run() + ).h1_title + return title or self.get_app_url() + + def parent_published_run(self) -> "PublishedRun": + return self.parent_version and self.parent_version.published_run def get_app_url(self): workflow = Workflow(self.workflow) diff --git a/daras_ai_v2/base.py b/daras_ai_v2/base.py index 519b70a28..7187178b7 100644 --- a/daras_ai_v2/base.py +++ b/daras_ai_v2/base.py @@ -923,7 +923,7 @@ def get_runs_from_query_params( ) -> tuple[SavedRun, PublishedRun | None]: if run_id and uid: sr = cls.run_doc_sr(run_id, uid) - pr = (sr and sr.parent_version and sr.parent_version.published_run) or None + pr = sr.parent_published_run() else: pr = cls.get_published_run(published_run_id=example_id or "") sr = pr.saved_run @@ -940,9 +940,7 @@ def get_pr_from_query_params( ) -> PublishedRun | None: if run_id and uid: sr = cls.get_sr_from_query_params(example_id, run_id, uid) - return ( - sr and sr.parent_version and sr.parent_version.published_run - ) or None + return sr.parent_published_run() elif example_id: return cls.get_published_run(published_run_id=example_id) else: