Skip to content

Commit

Permalink
admin niceties for saved runs
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Jan 31, 2024
1 parent 975dae3 commit 06c9d1d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
31 changes: 24 additions & 7 deletions bots/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class PublishedRunAdmin(admin.ModelAdmin):
"view_user",
"open_in_gooey",
"linked_saved_run",
"view_runs",
"created_at",
"updated_at",
]
Expand All @@ -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",
]
Expand All @@ -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"]
Expand All @@ -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),
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 06c9d1d

Please sign in to comment.