Begin working on replacing Redis with Django ORM

This commit is contained in:
2023-03-12 12:43:22 +00:00
parent beb5049fec
commit 1a34121da6
5 changed files with 156 additions and 21 deletions

View File

@@ -116,7 +116,7 @@ class LocalPlatformClient(ABC):
if dash["contact_count"] > 0:
for contact in dash["contact_list"]:
contact_id = contact["data"]["contact_id"]
reference = await db.tx_to_ref(str(contact_id))
reference = self.instance.contact_id_to_reference(str(contact_id))
contact["reference"] = reference
dash_tmp[contact_id] = contact
return dash_tmp
@@ -175,7 +175,8 @@ class LocalPlatformClient(ABC):
if not dash.items():
return
for contact_id, contact in dash.items():
reference = await db.tx_to_ref(str(contact_id))
contact_id = str(contact_id)
reference = self.instance.contact_id_to_reference(contact_id)
if reference:
current_trades.append(reference)
buyer = contact["data"]["buyer"]["username"]
@@ -185,7 +186,7 @@ class LocalPlatformClient(ABC):
elif self.name == "lbtc":
asset = "BTC"
provider = contact["data"]["advertisement"]["payment_method"]
ad_id = provider = contact["data"]["advertisement"]["id"]
ad_id = contact["data"]["advertisement"]["id"]
if asset == "XMR":
amount_crypto = contact["data"]["amount_xmr"]
elif asset == "BTC":
@@ -211,7 +212,8 @@ class LocalPlatformClient(ABC):
if reference and reference not in current_trades:
current_trades.append(reference)
messages = await db.cleanup(self.name, current_trades)
messages = self.instance.remove_trades_with_reference_not_in(current_trades)
for message in messages:
await notify.sendmsg(self.instance.user, message, title="Cleanup")
@@ -860,24 +862,48 @@ class LocalPlatformClient(ABC):
Store details in Redis, generate a reference and optionally let the customer
know the reference.
"""
print(
"NEW TRADE",
asset,
trade_id,
buyer,
currency,
amount,
amount_crypto,
provider,
ad_id,
)
reference = "".join(choices(ascii_uppercase, k=5))
reference = f"AGR-{reference}"
existing_ref = await db.r.get(f"trade.{trade_id}.reference")
existing_ref = self.instance.contact_id_to_reference(trade_id)
if not existing_ref:
to_store = {
"id": trade_id,
"tx": "",
"asset": asset,
"buyer": buyer,
"currency": currency,
"amount": amount,
"amount_crypto": amount_crypto,
# to_store = {
# "id": trade_id,
# "tx": "",
# "asset": asset,
# "buyer": buyer,
# "currency": currency,
# "amount": amount,
# "amount_crypto": amount_crypto,
# "reference": reference,
# "provider": provider,
# }
trade_cast = {
"contact_id": trade_id,
"reference": reference,
"buyer": buyer,
"amount_fiat": amount,
"amount_crypto": amount_crypto,
"asset": asset,
"currency": currency,
"provider": provider,
"ad_id": ad_id,
}
log.info(f"Storing trade information: {str(to_store)}")
await db.r.hmset(f"trade.{reference}", to_store)
await db.r.set(f"trade.{trade_id}.reference", reference)
log.info(f"Storing trade information: {str(trade_cast)}")
trade = self.instance.new_trade(trade_cast)
# await db.r.hmset(f"trade.{reference}", to_store)
# await db.r.set(f"trade.{trade_id}.reference", reference)
message = f"Generated reference for {trade_id}: {reference}"
title = "Generated reference"
await notify.sendmsg(self.instance.user, message, title=title)
@@ -896,7 +922,7 @@ class LocalPlatformClient(ABC):
await self.send_bank_details(currency, trade_id, ad_obj)
await self.send_reference(trade_id, reference)
if existing_ref:
return db.convert(existing_ref)
return existing_ref
else:
return reference