Begin working on replacing Redis with Django ORM
This commit is contained in:
parent
beb5049fec
commit
1a34121da6
|
@ -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
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 4.1.7 on 2023-03-12 12:22
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0018_transaction_trade'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='trade',
|
||||
name='status',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='trade',
|
||||
name='open',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,26 @@
|
|||
# Generated by Django 4.1.7 on 2023-03-12 12:38
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0019_remove_trade_status_trade_open'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='trade',
|
||||
old_name='coin',
|
||||
new_name='asset',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='trade',
|
||||
name='seller',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='trade',
|
||||
name='type',
|
||||
),
|
||||
]
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.1.7 on 2023-03-12 12:42
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0020_rename_coin_trade_asset_remove_trade_seller_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='trade',
|
||||
name='ad_id',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
]
|
|
@ -196,6 +196,51 @@ class Platform(models.Model):
|
|||
|
||||
return list(providers)
|
||||
|
||||
@property
|
||||
def references(self):
|
||||
"""
|
||||
Get references of all our trades that are open.
|
||||
"""
|
||||
references = []
|
||||
our_trades = Trade.objects.filter(platform=self, open=True)
|
||||
for trade in our_trades:
|
||||
references.append(trade.reference)
|
||||
|
||||
return references
|
||||
|
||||
def contact_id_to_reference(self, contact_id):
|
||||
"""
|
||||
Get a reference from a contact_id.
|
||||
"""
|
||||
trade = Trade.objects.filter(
|
||||
platform=self, open=True, contact_id=contact_id
|
||||
).first()
|
||||
if not trade:
|
||||
return None
|
||||
return trade.reference
|
||||
|
||||
def new_trade(self, trade_cast):
|
||||
trade = Trade.objects.create(
|
||||
platform=self,
|
||||
**trade_cast,
|
||||
)
|
||||
return trade
|
||||
|
||||
def remove_trades_with_reference_not_in(self, reference_list):
|
||||
"""
|
||||
Set trades with reference not in list to open=False.
|
||||
"""
|
||||
trades = Trade.objects.filter(platform=self, open=True)
|
||||
messages = []
|
||||
for trade in trades:
|
||||
if trade.reference not in reference_list:
|
||||
trade.open = False
|
||||
trade.save()
|
||||
msg = f"[{trade.reference}]: Archiving ID: {trade.contact_id}"
|
||||
messages.append(msg)
|
||||
log.info(msg)
|
||||
return messages
|
||||
|
||||
|
||||
class Asset(models.Model):
|
||||
code = models.CharField(max_length=64)
|
||||
|
@ -292,16 +337,14 @@ class Trade(models.Model):
|
|||
contact_id = models.CharField(max_length=255)
|
||||
reference = models.CharField(max_length=255)
|
||||
buyer = models.CharField(max_length=255)
|
||||
seller = models.CharField(max_length=255)
|
||||
amount_fiat = models.FloatField()
|
||||
currency = models.CharField(max_length=16)
|
||||
amount_crypto = models.FloatField()
|
||||
coin = models.CharField(max_length=16)
|
||||
asset = models.CharField(max_length=16)
|
||||
provider = models.CharField(max_length=255)
|
||||
type = models.CharField(max_length=255)
|
||||
ad_id = models.CharField(max_length=255)
|
||||
ad_id = models.CharField(max_length=255, null=True, blank=True)
|
||||
|
||||
status = models.CharField(max_length=255)
|
||||
open = models.BooleanField(default=True)
|
||||
|
||||
linked = models.ManyToManyField(Transaction)
|
||||
reconciled = models.BooleanField(default=False)
|
||||
|
|
Loading…
Reference in New Issue