Event-based actions
You can register methods as pre- or post- actions for document events.
Currently supported events:
- Insert
- Replace
- Update
- SaveChanges
- Delete
- ValidateOnSave
Currently supported directions:
Before
After
Current operations creating events:
insert()
for Insertreplace()
for Replacesave()
triggers Insert if it is creating a new document, triggers Replace if it replaces an existing documentsave_changes()
for SaveChangesinsert()
,replace()
,save_changes()
, andsave()
for ValidateOnSaveset()
,update()
for Updatedelete()
for Delete
To register an action, you can use @before_event
and @after_event
decorators respectively:
from bunnet import Document, before_event, after_event, Insert, Replace
class Sample(Document):
num: int
name: str
@before_event(Insert)
def capitalize_name(self):
self.name = self.name.capitalize()
@after_event(Replace)
def num_change(self):
self.num -= 1
It is possible to register action for several events:
from bunnet import Document, before_event, Insert, Replace
class Sample(Document):
num: int
name: str
@before_event(Insert, Replace)
def capitalize_name(self):
self.name = self.name.capitalize()
This will capitalize the name
field value before each document's Insert and Replace.
Actions can be selectively skipped by passing the skip_actions
argument when calling
the operations that trigger events. skip_actions
accepts a list of directions and action names.
from bunnet import Document, before_event, after_event, After, Before, Insert, Replace
class Sample(Document):
num: int
name: str
@before_event(Insert)
def capitalize_name(self):
self.name = self.name.capitalize()
@before_event(Replace)
def redact_name(self):
self.name = "[REDACTED]"
@after_event(Replace)
def num_change(self):
self.num -= 1
sample = Sample()
# capitalize_name will not be executed
sample.insert(skip_actions=['capitalize_name'])
# num_change will not be executed
sample.replace(skip_actions=[After])
# redact_name and num_change will not be executed
sample.replace(skip_actions=[Before, 'num_change'])