Skip to content

Commit

Permalink
bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Dec 30, 2021
1 parent bed6678 commit 0fec746
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
22 changes: 22 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
Changes
=======

0.31.0
------
Added hooks to ``PiccoloCRUD``. This allows the user to add their own logic
before a save / patch / delete (courtesy @trondhindenes).

For example:

.. code-block:: python

# Normal functions and async functions are supported:
def pre_save_hook(movie):
movie.rating = 90
return movie

PiccoloCRUD(
table=Movie,
read_only=False,
hooks=[
Hook(hook_type=HookType.pre_save, callable=pre_save_hook)
]
)

0.30.1
------
* Streamlined the ``CSRFMiddleware`` code, and added missing type annotations.
Expand Down
21 changes: 12 additions & 9 deletions docs/source/crud/hooks.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Hooks
=====

Hooks allow executing custom code as part of processing a crud request. You can use this to validate data, call another custom api, place messages on queues and many other things.
Hooks allow executing custom code as part of processing a CRUD request. You can use this to validate data, call another custom API, place messages on queues and many other things.

Enabling a hook
---------------

Define a method, and register it with PiccoloCRUD:
Define a method, and register it with ``PiccoloCRUD``:

.. code-block:: python
Expand All @@ -29,20 +29,23 @@ Define a method, and register it with PiccoloCRUD:
pass
# Register one or multiple hooks
app = PiccoloCRUD(table=Movie, read_only=False, hooks=[
Hook(hook_type=HookType.pre_save, callable=set_movie_rating_10),
Hook(hook_type=HookType.pre_save, callable=set_movie_rating_20),
Hook(hook_type=HookType.pre_delete, callable=pre_delete)
app = PiccoloCRUD(
table=Movie,
read_only=False,
hooks=[
Hook(hook_type=HookType.pre_save, callable=set_movie_rating_10),
Hook(hook_type=HookType.pre_save, callable=set_movie_rating_20),
Hook(hook_type=HookType.pre_delete, callable=pre_delete)
]
)
You can specify multiple hooks (also per hook_type). Hooks are executed in order.
You can specify multiple hooks (also per ``hook_type``). Hooks are executed in order.
You can use either async or regular functions.

Hook types
----------

There are different hook types, and each type takes a slightly different set of inputs.
There are different hook types, and each type takes a slightly different set of inputs.
It's also important to return the expected data from your hook.

pre_save
Expand Down Expand Up @@ -89,7 +92,7 @@ pre_delete

This hook runs during DELETE requests, prior to deleting the specified row in the database.
It takes one parameter, ``row_id`` which is the id of the row to be deleted.
pre_delete hooks should not return data
``pre_delete`` hooks should not return data.

.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion piccolo_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__VERSION__ = "0.30.1"
__VERSION__ = "0.31.0"

0 comments on commit 0fec746

Please sign in to comment.