save messages

This commit is contained in:
Matthias 2021-09-12 22:24:25 +02:00
parent e2bb5e978b
commit 0e2db232ae
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
4 changed files with 96 additions and 8 deletions

View File

@ -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)

View File

@ -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 = {}

View File

@ -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')),
],
),
]

View File

@ -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?'),
)