Allow absent price

This commit is contained in:
Mark Veidemanis 2022-11-21 07:20:12 +00:00
parent f6b5652268
commit 46bba54cb7
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 26 additions and 21 deletions

View File

@ -17,6 +17,6 @@ class DrakdooCallback(BaseModel):
title: str title: str
message: str message: str
period: str period: str
price: str price: str | None
market: DrakdooMarket market: DrakdooMarket
timestamp: DrakdooTimestamp timestamp: DrakdooTimestamp

View File

@ -1,4 +1,4 @@
# import re import re
import orjson import orjson
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
@ -17,16 +17,16 @@ from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
log = logs.get_logger(__name__) log = logs.get_logger(__name__)
# def extract_price(message): def extract_price(message):
# result = re.findall("\d+\.\d+", message) # noqa result = re.findall("\d+\.\d+", message) # noqa
# if len(result) != 1: if len(result) != 1:
# log.error(f"Could not extract price from message: {message}") log.error(f"Could not extract price from message: {message}")
# return False return False
# try: try:
# log.debug(f"Extracted {result[0]} from '{message}'") log.debug(f"Extracted {result[0]} from '{message}'")
# return float(result[0]) return float(result[0])
# except ValueError: except ValueError:
# return False return False
class HookAPI(APIView): class HookAPI(APIView):
@ -48,15 +48,20 @@ class HookAPI(APIView):
log.error(f"HookAPI POST: {e}") log.error(f"HookAPI POST: {e}")
return HttpResponseBadRequest(e) return HttpResponseBadRequest(e)
# price = extract_price(hook_resp.message) if hasattr(hook_resp, "market.price"):
# if not price: try:
# log.debug(f"Could not extract price from message: {hook_resp.message}") price = float(hook_resp.market.price)
# return HttpResponseBadRequest("Could not extract price from message") except ValueError:
try: log.debug(
price = float(hook_resp.market.price) f"Could not extract price from message: {hook_resp.market.price}"
except ValueError: )
log.debug(f"Could not extract price from message: {hook_resp.market.price}") return HttpResponseBadRequest("Could not extract price from message")
return HttpResponseBadRequest("Could not extract price from message") else:
price = extract_price(hook_resp.message)
if not price:
log.debug(f"Could not extract price from message: {hook_resp.message}")
return HttpResponseBadRequest("Could not extract price from message")
base = hook_resp.market.item base = hook_resp.market.item
quote = hook_resp.market.currency quote = hook_resp.market.currency
symbol = f"{base.upper()}/{quote.upper()}" symbol = f"{base.upper()}/{quote.upper()}"