diff --git a/InvenTree/common/admin.py b/InvenTree/common/admin.py index 50954924ff..088f53dc39 100644 --- a/InvenTree/common/admin.py +++ b/InvenTree/common/admin.py @@ -5,7 +5,7 @@ from django.contrib import admin from import_export.admin import ImportExportModelAdmin -from .models import InvenTreeSetting, InvenTreeUserSetting, WebhookEndpoint +from .models import InvenTreeSetting, InvenTreeUserSetting, WebhookEndpoint, WebhookMessage class SettingsAdmin(ImportExportModelAdmin): @@ -26,3 +26,4 @@ class WebhookAdmin(ImportExportModelAdmin): admin.site.register(InvenTreeSetting, SettingsAdmin) admin.site.register(InvenTreeUserSetting, UserSettingsAdmin) admin.site.register(WebhookEndpoint, WebhookAdmin) +admin.site.register(WebhookMessage, ImportExportModelAdmin) diff --git a/InvenTree/common/api.py b/InvenTree/common/api.py index a0d69bd2ab..bfd7163710 100644 --- a/InvenTree/common/api.py +++ b/InvenTree/common/api.py @@ -19,7 +19,7 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.exceptions import PermissionDenied, NotFound, NotAcceptable -from .models import WebhookEndpoint +from .models import WebhookEndpoint, WebhookMessage class CsrfExemptMixin(object): @@ -68,8 +68,8 @@ class WebhookView(CsrfExemptMixin, APIView): # validate self.validate_token(payload, headers, request) # process data - self.save_data(payload, headers, request) - self.process_payload(payload, headers, request) + message = self.save_data(payload, headers, request) + self.process_payload(message, payload, headers) # return results return_kwargs = self.get_result(payload, headers, request) @@ -122,11 +122,15 @@ class WebhookView(CsrfExemptMixin, APIView): return True def save_data(self, payload, headers=None, request=None): - # TODO safe data - return + return WebhookMessage.objects.create( + host=request.host, + header=headers, + body=payload, + endpoint=self.webhook, + ) - def process_payload(self, payload, headers=None, request=None): - return + def process_payload(self, message, payload=None, headers=None): + return True def get_result(self, payload, headers=None, request=None): context = {} diff --git a/InvenTree/common/migrations/0014_auto_20210912_1804.py b/InvenTree/common/migrations/0014_auto_20210912_1804.py new file mode 100644 index 0000000000..18feb0d4a4 --- /dev/null +++ b/InvenTree/common/migrations/0014_auto_20210912_1804.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.4 on 2021-09-12 18:04 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0013_auto_20210912_1443'), + ] + + operations = [ + migrations.CreateModel( + name='WebhookMessage', + fields=[ + ('message_id', models.UUIDField(default=uuid.uuid4, editable=False, help_text='Unique identifier for this message', primary_key=True, serialize=False, verbose_name='Message ID')), + ('host', models.CharField(editable=False, help_text='Host from which this message was received', max_length=255, verbose_name='Host')), + ('header', models.CharField(blank=True, editable=False, help_text='Header of this message', max_length=255, null=True, verbose_name='Header')), + ('body', models.JSONField(blank=True, editable=False, help_text='Body of this message', null=True, verbose_name='Body')), + ('worked_on', models.BooleanField(default=False, help_text='Was the work on this message finished?', verbose_name='Worked on')), + ('endpoint', models.ForeignKey(blank=True, help_text='Endpoint on which this message was received', null=True, on_delete=django.db.models.deletion.SET_NULL, to='common.webhookendpoint', verbose_name='Endpoint')), + ], + ), + ] diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index ca13e84357..4bd24e878e 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1223,3 +1223,60 @@ class WebhookEndpoint(models.Model): verbose_name=_('Secret'), help_text=_('Shared secret for HMAC'), ) + + +class WebhookMessage(models.Model): + """ Defines a webhook message + + Attributes: + message_id: Unique identifier for this message, + host: Host from which this message was received, + header: Header of this message, + body: Body of this message, + endpoint: Endpoint on which this message was received, + worked_on: Was the work on this message finished? + """ + + message_id = models.UUIDField( + verbose_name=_('Message ID'), + help_text=_('Unique identifier for this message'), + primary_key=True, + default=uuid.uuid4, + editable=False, + ) + + host = models.CharField( + max_length=255, + verbose_name=_('Host'), + help_text=_('Host from which this message was received'), + editable=False, + ) + + header = models.CharField( + max_length=255, + blank=True, null=True, + verbose_name=_('Header'), + help_text=_('Header of this message'), + editable=False, + ) + + body = models.JSONField( + blank=True, null=True, + verbose_name=_('Body'), + help_text=_('Body of this message'), + editable=False, + ) + + endpoint = models.ForeignKey( + WebhookEndpoint, + on_delete=models.SET_NULL, + blank=True, null=True, + verbose_name=_('Endpoint'), + help_text=_('Endpoint on which this message was received'), + ) + + worked_on = models.BooleanField( + default=False, + verbose_name=_('Worked on'), + help_text=_('Was the work on this message finished?'), + )