Parse Drakdoo response into model fields

This commit is contained in:
Mark Veidemanis 2022-10-15 23:28:46 +01:00
parent 20a4e21eaa
commit bc27748396
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 35 additions and 12 deletions

View File

@ -1,18 +1,21 @@
from serde import Model, fields from serde import Model, fields
class Market(Model): class Market(Model):
exchange = fields.Str() exchange = fields.Str()
item = fields.Str() item = fields.Str()
currency = fields.Str() currency = fields.Str()
contract = fields.Str() contract = fields.Str()
class Timestamp(Model): class Timestamp(Model):
sent = fields.DateTime() sent = fields.DateTime()
trade = fields.DateTime() trade = fields.DateTime()
class BaseDrakdoo(Model): class BaseDrakdoo(Model):
title = fields.Str() title = fields.Str()
message = fields.Str() message = fields.Str()
period = fields.Str() period = fields.Str()
market = fields.Nested(Market) market = fields.Nested(Market)
timestamp = fields.Nested(Timestamp) timestamp = fields.Nested(Timestamp)

View File

@ -83,7 +83,16 @@ class Hook(models.Model):
class Callback(models.Model): class Callback(models.Model):
hook = models.ForeignKey(Hook, on_delete=models.CASCADE) hook = models.ForeignKey(Hook, on_delete=models.CASCADE)
data = models.JSONField() title = models.CharField(max_length=1024, null=True, blank=True)
message = models.CharField(max_length=1024, null=True, blank=True)
period = models.CharField(max_length=255, null=True, blank=True)
market = models.CharField(max_length=255, null=True, blank=True)
timestamp_sent = models.DateTimeField(null=True, blank=True)
timestamp_trade = models.DateTimeField(null=True, blank=True)
market_exchange = models.CharField(max_length=255, null=True, blank=True)
market_item = models.CharField(max_length=255, null=True, blank=True)
market_currency = models.CharField(max_length=255, null=True, blank=True)
market_contract = models.CharField(max_length=255, null=True, blank=True)
# class Perms(models.Model): # class Perms(models.Model):

View File

@ -21,5 +21,5 @@
</div> </div>
</div> </div>
</div> </div>
</section> </section>\
{% endblock %} {% endblock %}

View File

@ -7,16 +7,16 @@ from django.shortcuts import render
from django.views import View from django.views import View
from rest_framework.parsers import FormParser, JSONParser from rest_framework.parsers import FormParser, JSONParser
from rest_framework.views import APIView from rest_framework.views import APIView
from core.forms import HookForm
from core.models import Callback, Hook
from core.lib.serde import drakdoo
from serde import ValidationError from serde import ValidationError
from util import log from util import log
from core.forms import HookForm
from core.lib.serde import drakdoo
from core.models import Callback, Hook
log = log.get_logger(__name__) log = log.get_logger(__name__)
def get_hooks(user): def get_hooks(user):
hooks = Hook.objects.filter(user=user) hooks = Hook.objects.filter(user=user)
return hooks return hooks
@ -41,6 +41,20 @@ class HookAPI(APIView):
log.error(f"HookAPI POST: {e}") log.error(f"HookAPI POST: {e}")
return HttpResponseBadRequest(e) return HttpResponseBadRequest(e)
data = {
"title": hook_resp.title,
"message": hook_resp.message,
"period": hook_resp.period,
"market": hook_resp.market,
"timestamp_sent": hook_resp.timestamp.sent,
"timestamp_trade": hook_resp.timestamp.trade,
"market_exchange": hook_resp.market.exchange,
"market_item": hook_resp.market.item,
"market_currency": hook_resp.market.currency,
"market_contract": hook_resp.market.contract,
}
log.debug("HookAPI callback: data: %s", data)
# Try getting the hook # Try getting the hook
try: try:
hook = Hook.objects.get(hook=hook_name) hook = Hook.objects.get(hook=hook_name)
@ -48,10 +62,7 @@ class HookAPI(APIView):
return HttpResponseBadRequest("Hook does not exist.") return HttpResponseBadRequest("Hook does not exist.")
# Create the callback object # Create the callback object
callback = Callback.objects.create( callback = Callback.objects.create(hook=hook, **data)
hook=hook,
data=request.data,
)
callback.save() callback.save()
# Bump received count # Bump received count
hook.received = hook.received + 1 hook.received = hook.received + 1