Skip to content

Commit

Permalink
refactor row actions
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oleshkevich committed Dec 20, 2023
1 parent ee1f034 commit 21a8476
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 49 deletions.
53 changes: 27 additions & 26 deletions examples/resources/users_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from examples.models import User
from ohmyadmin import colors, formatters
from ohmyadmin.actions import actions
from ohmyadmin.datasources.datasource import DataSource, InFilter
from ohmyadmin.datasources.datasource import InFilter
from ohmyadmin.datasources.sqlalchemy import get_dbsession, SADataSource
from ohmyadmin.filters import (
ChoiceFilter,
Expand Down Expand Up @@ -73,7 +73,9 @@ async def create_user_callback(request: Request, form: CreateUserForm) -> Respon
return response().toast("New user created!").close_modal()


async def object_toast_callback(request: Request, query: DataSource) -> Response:
async def object_toast_callback(request: Request) -> Response:
view: TableView = request.state.view
query = view.get_query(request).filter(InFilter("id", request.query_params.get("selected")))
count = await query.count(request)
return response().toast(f"Object selected: {count}.").close_modal()

Expand Down Expand Up @@ -213,6 +215,29 @@ class UsersTable(TableView):
icon=PLUS_ICON,
),
]
row_actions = [
actions.LinkAction(url="/admin", label="Show details"),
actions.CallbackAction(label="Show toast", callback=object_toast_callback),
actions.CallbackAction(
dangerous=True,
label="Show toast with confirmation",
callback=object_toast_callback,
confirmation="Call this dangerous action?",
),
actions.ModalAction(
label="Form action",
callback=object_form_callback,
form_class=RowObjectForm,
icon=PLUS_ICON,
),
actions.ModalAction(
label="Dangerous form action",
dangerous=True,
callback=object_form_callback,
form_class=RowObjectForm,
icon=PLUS_ICON,
),
]
actions = [
actions.LinkAction(url="/admin", label="To Main page"),
actions.CallbackAction("Show toast", callback=show_toast_callback),
Expand All @@ -226,30 +251,6 @@ class UsersTable(TableView):
modal_description="Create a new user right now!",
),
]
row_actions = [
# object_actions.LinkAction(url="/admin", label="Show details"),
# object_actions.LinkAction(url=lambda r, o: f"/admin?id={o.id}", label="Generated URL"),
# object_actions.CallbackAction(label="Show toast", callback=object_toast_callback),
# object_actions.CallbackAction(
# dangerous=True,
# label="Show toast with confirmation",
# callback=object_toast_callback,
# confirmation="Call this dangerous action?",
# ),
# object_actions.FormAction(
# label="Form action",
# callback=object_form_callback,
# form_class=RowObjectForm,
# icon=PLUS_ICON,
# ),
# object_actions.FormAction(
# label="Dangerous form action",
# dangerous=True,
# callback=object_form_callback,
# form_class=RowObjectForm,
# icon=PLUS_ICON,
# ),
]

columns = [
Column("photo", formatter=AvatarFormatter()),
Expand Down
5 changes: 5 additions & 0 deletions ohmyadmin/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,26 @@ def get_url(self, request: Request) -> str:

class CallbackAction(Action):
confirmation: str = ""
dangerous: bool = False
button_template: str = "ohmyadmin/actions/callback.html"
dropdown_template: str = "ohmyadmin/actions/callback_menu.html"

def __init__(
self,
label: str = "",
callback: ActionCallback | None = None,
dangerous: bool | None = None,
icon: str = "",
confirmation: str = "",
variant: ActionVariant = "default",
) -> None:
self.slug = random_slug()
self.variant = variant
self.callback = callback
self.icon = icon or self.icon
self.label = label or self.label
self.confirmation = confirmation or self.confirmation
self.dangerous = self.dangerous if dangerous is None else dangerous

async def dispatch(self, request: Request) -> Response:
if request.method == "POST":
Expand Down
4 changes: 2 additions & 2 deletions ohmyadmin/templates/ohmyadmin/actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
</div>
{% endmacro %}

{% macro action_button(request, action, view) %}
{% macro action_button(request, action, view, object_id='') %}
{% include action.button_template %}
{% endmacro %}

{% macro action_menu_item(request, action, view) %}
{% macro action_menu_item(request, action, view, object_id='') %}
{% include action.dropdown_template %}
{% endmacro %}
8 changes: 5 additions & 3 deletions ohmyadmin/templates/ohmyadmin/actions/callback.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<button type="button" class="btn btn-{{ action.variant }}"
hx-push-url="false"
hx-post="{{ url_for(view.get_action_route_name(action)) }}">
{% set url = url_for(view.get_action_route_name(action)) %}
{% if object_id %}
{% set url = url.include_query_params(selected=object_id) %}
{% endif %}
<button type="button" class="btn btn-{{ action.variant }}" hx-push-url="false" hx-post="{{ url }}">
{{ action.icon }}
{{ action.label }}
</button>
15 changes: 9 additions & 6 deletions ohmyadmin/templates/ohmyadmin/actions/callback_menu.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{% set confirmation = action.get_confirmation(request, object) %}
{% set pk_value = object.get_pk() %}
<button type="button" hx-target="o-modals"
{% set confirmation = action.confirmation %}
{% set url = url_for(view.get_action_route_name(action)) %}
{% if object_id %}
{% set url = url.include_query_params(selected=object_id) %}
{% endif %}
<button type="button" hx-target="o-modals" hx-push-url="false"
class="list-menu-item {{ 'danger' if action.dangerous else '' }}"
hx-get="{{ url_for(action.get_url_name(view.url_name)).include_query_params(selected=pk_value) }}"
hx-post="{{ url }}"
{% if confirmation %}hx-confirm="{{ confirmation }}"{% endif %}>
{{ action.get_icon(request, object) }}
{{ action.get_label(request, object) }}
{{ action.icon }}
{{ action.label }}
</button>
6 changes: 5 additions & 1 deletion ohmyadmin/templates/ohmyadmin/actions/modal.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% set url = url_for(view.get_action_route_name(action)) %}
{% if object_id %}
{% set url = url.include_query_params(selected=object_id) %}
{% endif %}
<button type="button" class="btn btn-{{ action.variant }}"
hx-target="o-modals"
hx-push-url="false"
hx-get="{{ url_for(view.get_action_route_name(action)) }}">
hx-get="{{ url }}">
{{ action.icon }}
{{ action.label }}
</button>
15 changes: 8 additions & 7 deletions ohmyadmin/templates/ohmyadmin/actions/modal_menu.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% set confirmation = action.get_confirmation(request, object) %}
{% set pk_value = object.get_pk() %}
<button type="button" hx-target="o-modals"
{% set url = url_for(view.get_action_route_name(action)) %}
{% if object_id %}
{% set url = url.include_query_params(selected=object_id) %}
{% endif %}
<button type="button" hx-target="o-modals" hx-push-url="false"
class="list-menu-item {{ 'danger' if action.dangerous else '' }}"
hx-get="{{ url_for(action.get_url_name(view.url_name)).include_query_params(selected=pk_value) }}"
{% if confirmation %}hx-confirm="{{ confirmation }}"{% endif %}>
{{ action.get_icon(request, object) }}
{{ action.get_label(request, object) }}
hx-get="{{ url }}">
{{ action.icon }}
{{ action.label }}
</button>
7 changes: 3 additions & 4 deletions ohmyadmin/templates/ohmyadmin/views/table/table.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% import 'ohmyadmin/pagination.html' as pagination %}
{% import 'ohmyadmin/icons.html' as icons %}
{% import 'ohmyadmin/actions.html' as actions %}

{% macro render_head_cell(request, column) %}
{% include 'ohmyadmin/views/table/table_head_cell.html' %}
Expand Down Expand Up @@ -80,11 +81,9 @@
</div>
<o-popover trigger="#object-menu-{{ loop.index0 }}" placement="bottom-end">
<div class="dropdown absolute hidden">
<div class="list-menu">
<div class="list-menu" hx-include="this">
{% for row_action in table.row_actions %}
{% with action=row_action %}
{% include row_action.template %}
{% endwith %}
{{ actions.action_menu_item(request, row_action, view, object.get_pk()) }}
{% endfor %}
</div>
</div>
Expand Down

0 comments on commit 21a8476

Please sign in to comment.