From 70ac711269c18f78b17d37f580c279f58f9acadd Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Fri, 5 Jul 2024 21:01:02 -0700 Subject: [PATCH] added an example to chapter 14 about Grid + checkboxes --- docs/chapter-06.rst | 48 +++++++++++++++++++++++++++++++++++++++++++++ docs/chapter-14.rst | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/docs/chapter-06.rst b/docs/chapter-06.rst index b3437314a..abf5ef31f 100644 --- a/docs/chapter-06.rst +++ b/docs/chapter-06.rst @@ -156,6 +156,7 @@ templates. Here is a simple example: .. code:: python + from py4web.utils.factories import Inject my_var = "Example variable to be passed to a Template" ... @@ -213,6 +214,28 @@ action with a counter that counts “visits”. session['counter'] = counter return str(T("You have been here {n} times").format(n=counter)) + +If the `T` fixture is to be used from inside a template you may want to pass it to the template: + +.. code:: python + + @action('index') + @action.uses("index.html", session, T) + def index(): + return dict(T=T) + +Or perahps inject (same effect as above) + +.. code:: python + + from py4web.utils.factories import Inject + + @action('index') + @action.uses("index.html", session, Inject(T=T) + def index(): + return dict() + + Now create the following translation file ``translations/en.json``: .. code:: json @@ -258,6 +281,31 @@ Now try create a file called ``translations/it.json`` which contains: Set your browser preference to Italian: now the messages will be automatically translated to Italian. +Notice there is UI for creating, updating, and updating translation files. +The UI is accessing via a button from the Dashboard. + +If you want to force an action to use language defined somewhere else, for example from a session variable, you can do: + +.. code:: python + + @action('index') + @action.uses("index.html", session, T) + def index(): + T.select(session.get("lang", "it")) + return dict(T=T) + +If you want all of your action to use the same pre-defined language and ignore browser preferences, +you have to redefine the select method for the T instance: + +.. code:: python + + T.on_request = lambda *_: T.local.__dict__.update(tag="it", language=T.languages["it"]) + +This is to be done outside any action and will apply to all actions. Action will still need todeclare +`action.uses(T)` else the behavior is undefined. + + + The Flash fixture ----------------- diff --git a/docs/chapter-14.rst b/docs/chapter-14.rst index a3bacce8c..483f4fd56 100644 --- a/docs/chapter-14.rst +++ b/docs/chapter-14.rst @@ -584,3 +584,39 @@ combine fields to be displayed together as the filter_out method would You need to determine which method is best for your use case understanding the different grids in the same application may need to behave differently. + + +Grids with checkboxes +--------------------- + +While the grid, per se, does not support checkboxes, you can use custom columns to add one or more columns of checboxes. +You can also add the helpers logic (the grid uses helpers to generate HTML) to wrap it in a ``
`` and add one +or more submit bottons. You can then add logic to process the selected rows when the button is selected. For example: + +.. code:: python + + column = Column("select", lambda row: INPUT(_type="checkbox",_name="selected_id",_value=row.id)) + + @action("manage") + @action("manage/") + @action.uses("manage.html", db) + def manage(path=None): + + grid = Grid(path, db.thing, columns=[column, db.thing.name]) + + # if we are displaying a "select" grid page (not a form) + if not grid.form: + grid = grid.render() + # if checkboxes selection was submitted + if request.method == "POST": + # do something with the selected ids + print("you selected", request.POST.get("selected_id")) + # inject a ```` and a ``submit`` button + grid.children[1:] = [FORM( + *grid.children[1:], + DIV(INPUT(_type="submit",_value="do it!")), + _method="POST", + _action=request.url)] + return locals() + +Notice in the above example ``request.POST.get("selected_id")`` can be a single ID (if one selected) or a list of IDs (if more than one elsected).