Package: drf-webhooks
Django Rest Framework - WebhooksConfigurable webhooks for DRF SerializersGoals:Use existing DRF Serializers from REST API to serialize data in webhooksConsistent data formattingReusable OpenAPI schemasConfigurable webhooks that simply work(by way of django signals magic)without the developer having to keep track of where to trigger themStill allow for "manual" triggering of webhooksThis is useful because signals aren't always triggeredFor example:QuerySet.updatedoes not trigger signalsDisable webhooks using context managersThis can be useful when syncing large chunks of dataor with a duplex sync (when two systems sync with each other) to avoid endless loopsWebhook Signal SessionA context manager gathers all models signals and at the end of the session only triggers the resulting webhooksIf a model instance is bothcreatedanddeletedwithin the session, then no webhook is sent for that model instanceIf a model instance iscreatedand then alsoupdatedwithin the session, then acreatedevent is sent with the data from the lastupdatedsignal. Only one webhook even is sentIf a models instance isupdatedmultiple times within the session, then only one webhook event is sentMiddleware wraps each request inWebhook Signal SessioncontextNOTE:The developer will have to call the context manager in code that runs outside of requests (for example in celery tasks) manuallyAutomatically determine which nested models need to be monitored for changesExamples:fromdjango.dbimportmodelsfromdrf_webhooksimportModelSerializerWebhook,register_webhookfromrest_frameworkimportserializersclassMyModel(models.Model):name=models.CharField(max_lenght=100)classMyModelSerializer(serializers.ModelSerializer):classMeta:model=MyModelfields=['id','name']# Automatic:register_webhook(MyModel)()# ---- OR ----# If you need more configuration:@register_webhook(MyModel)classMyModelWebhook(ModelSerializerWebhook):base_name='core.my_model'Documentation:Quckstart:Installdrf-webhookspoetryadddrf-webhooks# ... or ...pipinstalldrf-webhooksUpdatesettings.py:INSTALLED_APPS=[# ...'drf_webhooks',]MIDDLEWARE=[# ...'drf_webhooks.middleware.WebhooksMiddleware',]# This is required if you don't want your database to fill up with logs:CELERY_BEAT_SCHEDULE={'clean-webhook-log':{'task':'drf_webhooks.tasks.auto_clean_log','schedule':60,'options':{'expires':10},},}Create a new django appRecommended app name:webhooks# ----------------------------------------------------------------------#  apps.py# ----------------------------------------------------------------------fromdjango.appsimportAppConfigclassWebhooksAppConfig(AppConfig):name="<your module name>"label="webhooks"# ----------------------------------------------------------------------#  models.py# ----------------------------------------------------------------------fromdjango.contrib.authimportget_user_modelfromdjango.dbimportmodelsfromdjango.utils.translationimportgettext_lazyas_fromdrf_webhooks.modelsimportAbstractWebhook,AbstractWebhookLogEntryclassWebhook(AbstractWebhook):# This can also be a group or an organization that the user belongs to:owner=models.ForeignKey(get_user_model(),on_delete=models.CASCADE)def__str__(self):return'id=%s, events=%s, owner=%s'%(str(self.id),', '.join(self.events),str(self.owner),)classWebhookLogEntry(AbstractWebhookLogEntry):# This can also be a group or an organization that the user belongs to:owner=models.ForeignKey(get_user_model(),on_delete=models.CASCADE)