thorn.decorators¶
Webhook decorators.
-
class
thorn.decorators.WebhookCapable(on_create=None, on_change=None, on_delete=None, reverse=None, sender_field=None, **kwargs)[source]¶ Implementation of model.webhooks.
The decorator sets model.webhooks to be an instance of this type.
-
events= None¶
-
reverse= None¶
-
sender_field= None¶
-
-
thorn.decorators.webhook_model(*args, **kwargs)[source]¶ Decorate model to send webhooks based on changes to that model.
Keyword Arguments: on_create (~thorn.Event) – Event to dispatch whenever an instance of this model is created (
post_save).on_change (~thorn.Event) – Event to dispatch whenever an instance of this model is changed (
post_save).on_delete (~thorn.Event) – Event to dispatch whenever an instance of this model is deleted (
post_delete).on_$event (~thorn.Event) – Additional user defined events.,
sender_field (str) – Default field used as a sender for events, e.g.
"account.user", will useinstance.account.user.Individual events can override the sender field user.
reverse (Callable) – A
thorn.reverse.model_reverserinstance (or any callable taking an model instance as argument), that describes how to get the URL for an instance of this model.Individual events can override the reverser used.
Note: On Django you can instead define a get_absolute_url method on the Model.
Examples
Simple article model, where the URL reference is retrieved by
reverse('article-detail', kwargs={'uuid': article.uuid}):@webhook_model class Article(models.Model): uuid = models.UUIDField() class webhooks: on_create = ModelEvent('article.created') on_change = ModelEvent('article.changed') on_delete = ModelEvent('article.removed') on_deactivate = ModelEvent( 'article.deactivate', deactivated__eq=True, ) @models.permalink def get_absolute_url(self): return ('blog:article-detail', None, {'uuid': self.uuid})
The URL may not actually exist after deletion, so maybe we want to point the reference to something else in that special case, like a category that can be reversed by doing
reverse('category-detail', args=[article.category.name]).We can do that by having the
on_deleteevent override the method used to get the absolute url (reverser), for that event only:@webhook_model class Article(model.Model): uuid = models.UUIDField() category = models.ForeignKey('category') class webhooks: on_create = ModelEvent('article.created') on_change = ModelEvent('article.changed') on_delete = ModelEvent( 'article.removed', reverse=model_reverser( 'category:detail', 'category.name'), ) on_hipri_delete = ModelEvent( 'article.internal_delete', priority__gte=30.0, ).dispatches_on_delete() @models.permalink def get_absolute_url(self): return ('blog:article-detail', None, {'uuid': self.uuid})