From 12dbc5e8d29a48c0ffdf55542765d995c1fc678f Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Fri, 13 Dec 2024 04:11:39 +0530 Subject: [PATCH] replace inline styles with gui.styled() --- daras_ai_v2/base.py | 94 +++++++++++---------------------- daras_ai_v2/breadcrumbs.py | 27 +++++----- daras_ai_v2/variables_widget.py | 15 ++++-- workspaces/admin.py | 17 +++++- 4 files changed, 69 insertions(+), 84 deletions(-) diff --git a/daras_ai_v2/base.py b/daras_ai_v2/base.py index 1e2948370..bdf14b168 100644 --- a/daras_ai_v2/base.py +++ b/daras_ai_v2/base.py @@ -484,20 +484,10 @@ def _render_options_button_with_dialog(self): self._unsaved_options_modal() def render_social_buttons(self): - with gui.div( - className="d-flex align-items-start right-action-icons gap-lg-2 gap-1" + with ( + gui.styled("& .btn { padding: 6px }"), + gui.div(className="d-flex align-items-start gap-lg-2 gap-1"), ): - gui.html( - # styling for buttons in this div - """ - - """.strip() - ) - if self.tab == RecipeTabs.run: if self.request.user and not self.request.user.is_anonymous: self._render_options_button_with_dialog() @@ -625,17 +615,6 @@ def _render_workspace_with_invite_button(self, workspace: Workspace): def _render_save_button(self): with gui.div(className="d-flex justify-content-end"): - gui.html( - """ - - """ - ) - if self.can_user_edit_published_run(self.current_pr): icon, label = icons.save, "Update" elif self._has_request_changed(): @@ -1173,15 +1152,6 @@ def _render_version_row( version: PublishedRunVersion, older_version: PublishedRunVersion | None, ): - gui.html( - """ - - """ - ) url = self.app_url( example_id=version.published_run.published_run_id, run_id=version.saved_run.run_id, @@ -1197,7 +1167,7 @@ def _render_version_row( version.changed_by, responsive=False, show_as_link=False ) else: - gui.write("###### Deleted User", className="disable-p-margin") + gui.write("###### Deleted User", className="container-margin-reset") with gui.tag("h6", className="mb-0"): gui.html( "Loading...", @@ -1206,7 +1176,7 @@ def _render_version_row( date_options={"month": "short", "day": "numeric"}, ), ) - with gui.div(className="disable-p-margin"): + with gui.div(className="container-margin-reset"): is_first_version = not older_version if is_first_version: with gui.tag("span", className="badge bg-secondary px-3"): @@ -1569,40 +1539,36 @@ def _render_author( if not photo and not name: return - responsive_image_size = ( - f"calc({image_size} * 0.67)" if responsive else image_size - ) - - # new class name so that different ones don't conflict - class_name = f"author-image-{image_size}" if responsive: - class_name += "-responsive" + responsive_image_size = f"calc({image_size} * 0.67)" + else: + responsive_image_size = image_size linkto = link and gui.link(to=link) or gui.dummy() with linkto, gui.div(className="d-flex align-items-center"): if photo: - gui.html( - f""" - - """ - ) - gui.image(photo, className=class_name) + with gui.styled( + """ + @media (min-width: 1024px) { + & { + width: %(image_size)s; + height: %(image_size)s; + } + } + """ + % dict(image_size=image_size) + ): + gui.image( + photo, + style=dict( + width=responsive_image_size, + height=responsive_image_size, + marginRight="6px", + borderRadius="50%", + objectFit="cover", + pointerEvents="none", + ), + ) if name: name_style = {"fontSize": text_size} if text_size else {} diff --git a/daras_ai_v2/breadcrumbs.py b/daras_ai_v2/breadcrumbs.py index 43da08574..c0f28b248 100644 --- a/daras_ai_v2/breadcrumbs.py +++ b/daras_ai_v2/breadcrumbs.py @@ -32,28 +32,27 @@ def has_breadcrumbs(self): def render_breadcrumbs(breadcrumbs: TitleBreadCrumbs, *, is_api_call: bool = False): - gui.html( - """ - - """ - ) - if not (breadcrumbs.root_title or breadcrumbs.published_title): # avoid empty space when breadcrumbs are not rendered return - with gui.breadcrumbs(): + with ( + gui.styled( + """ + @media (min-width: 1024px) { + & a { + font-size: 1.25rem !important; + } + } + """ + ), + gui.breadcrumbs(), + ): if breadcrumbs.root_title: gui.breadcrumb_item( breadcrumbs.root_title.title, link_to=breadcrumbs.root_title.url, - className="text-muted fs-lg-5", + className="text-muted", ) if breadcrumbs.published_title: gui.breadcrumb_item( diff --git a/daras_ai_v2/variables_widget.py b/daras_ai_v2/variables_widget.py index ea24131e0..321499f28 100644 --- a/daras_ai_v2/variables_widget.py +++ b/daras_ai_v2/variables_widget.py @@ -169,15 +169,17 @@ def render_list_item( gui.write(f"**{value_type}**", className="text-muted small") if schema.get("role") == "system": - gui.caption( + gui.write( "System provided", help="This variable is automatically provided by the system. ", + className="text-muted small", ) if is_template_var: - gui.caption( + gui.write( "Template variable", help="Your instruction or other prompts reference this variable. " "Add a value and tap Run to test a sample value.", + className="text-muted small", ) gui.div(className="flex-grow-1") @@ -188,9 +190,14 @@ def render_list_item( key=dialog_ref.open_btn_key, ) - item["value"] = json_value_editor(entry_key, value, value_type) + with ( + gui.styled(".gui-input:has(&) { margin-bottom: 0 }") + if description + else gui.dummy() + ): + item["value"] = json_value_editor(entry_key, value, value_type) - gui.caption(description, style=dict(marginTop="-5px", display="relative")) + gui.markdown(description, className="text-muted small") def json_value_editor(entry_key: str, value, value_type: "JsonTypes"): diff --git a/workspaces/admin.py b/workspaces/admin.py index 31524638f..6ee624632 100644 --- a/workspaces/admin.py +++ b/workspaces/admin.py @@ -1,8 +1,9 @@ from django.contrib import admin -from django.db.models import Sum +from django.db.models import Sum, Q from safedelete.admin import SafeDeleteAdmin, SafeDeleteAdminFilter from bots.admin_links import change_obj_url, open_in_new_tab +from payments.models import Subscription from usage_costs.models import UsageCost from . import models @@ -85,7 +86,19 @@ class WorkspaceAdmin(SafeDeleteAdmin): ] inlines = [WorkspaceMembershipInline, WorkspaceInviteInline] ordering = ["-created_at"] - autocomplete_fields = ["created_by", "subscription"] + autocomplete_fields = ["created_by"] + + def get_form(self, request, obj=None, change=False, **kwargs): + self.obj = obj + return super().get_form(request, obj, change, **kwargs) + + def formfield_for_foreignkey(self, db_field, request, **kwargs): + field = super().formfield_for_foreignkey(db_field, request, **kwargs) + if db_field.name == "subscription" and self.obj: + kwargs["queryset"] = Subscription.objects.filter( + Q(workspace=self.obj) | Q(workspace__isnull=True) + )[:10] + return field @admin.display(description="Name") def display_name(self, workspace: models.Workspace):