Doing a sort of catch-up on previous work after returning 2 months later
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@ from django.db.models.signals import post_save, pre_save
|
||||
from django.dispatch import receiver
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from frozendict import frozendict
|
||||
from guardian.shortcuts import assign_perm
|
||||
from hashid_field import HashidAutoField
|
||||
from trading.managers import UserManager
|
||||
|
||||
@@ -89,9 +90,9 @@ class User(PermissionsMixin, AbstractBaseUser):
|
||||
}
|
||||
tx_requests = {
|
||||
row["source_sends"]: row["source_amount__sum"]
|
||||
for row in self.transaction_requests_sent.filter(
|
||||
status=TxRequest.OPEN
|
||||
).values("source_sends").annotate(Sum("source_amount"))
|
||||
for row in self.transaction_requests_sent.filter(status=TxRequest.OPEN)
|
||||
.values("source_sends")
|
||||
.annotate(Sum("source_amount"))
|
||||
}
|
||||
keys = (
|
||||
set(ipos.keys())
|
||||
@@ -244,21 +245,27 @@ class TxRequest(models.Model):
|
||||
# )
|
||||
|
||||
source = models.ForeignKey(
|
||||
User, on_delete=models.CASCADE, related_name="transaction_requests_sent",
|
||||
User,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="transaction_requests_sent",
|
||||
editable=False,
|
||||
)
|
||||
dest = models.ForeignKey(
|
||||
User, on_delete=models.CASCADE, related_name="transaction_requests_received",
|
||||
User,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="transaction_requests_received",
|
||||
editable=False,
|
||||
)
|
||||
|
||||
source_sends = models.ForeignKey(
|
||||
Commodity, on_delete=models.CASCADE, related_name="+", null=True,
|
||||
Commodity, on_delete=models.CASCADE, related_name="+", null=True, editable=False,
|
||||
)
|
||||
dest_sends = models.ForeignKey(
|
||||
Commodity, on_delete=models.CASCADE, related_name="+", null=True,
|
||||
Commodity, on_delete=models.CASCADE, related_name="+", null=True, editable=False,
|
||||
)
|
||||
|
||||
source_amount = models.PositiveIntegerField()
|
||||
dest_amount = models.PositiveIntegerField()
|
||||
source_amount = models.PositiveIntegerField(editable=False)
|
||||
dest_amount = models.PositiveIntegerField(editable=False)
|
||||
|
||||
@staticmethod
|
||||
def open(
|
||||
@@ -275,7 +282,9 @@ class TxRequest(models.Model):
|
||||
# Check balance
|
||||
source_balance = source.balance_of(source_sends)
|
||||
if source_balance < source_amount:
|
||||
raise BalanceError(source_balance, source, dest, source_amount, source_sends)
|
||||
raise BalanceError(
|
||||
source_balance, source, dest, source_amount, source_sends
|
||||
)
|
||||
req = TxRequest.objects.create(
|
||||
status=TxRequest.OPEN,
|
||||
source=source,
|
||||
@@ -288,20 +297,28 @@ class TxRequest(models.Model):
|
||||
req.save()
|
||||
return req
|
||||
|
||||
def can_accept(self) -> bool:
|
||||
"""
|
||||
Gets whether this request can be accepted.
|
||||
"""
|
||||
dest_balance = self.dest.balance_of(self.dest_sends)
|
||||
return self.status == TxRequest.OPEN and dest_balance >= self.dest_amount
|
||||
|
||||
|
||||
def accept(self):
|
||||
"""
|
||||
Accepts an open transaction request.
|
||||
"""
|
||||
assert self.status == TxRequest.OPEN
|
||||
|
||||
# ensure destination balance before continuing
|
||||
dest_balance = self.dest.balance_of(self.dest_sends)
|
||||
if dest_balance < self.dest_amount:
|
||||
# Ensure destination balance before continuing
|
||||
if not self.can_accept():
|
||||
dest_balance = self.dest.balance_of(self.dest_sends)
|
||||
raise BalanceError(
|
||||
dest_balance, self.dest, self.source, self.dest_amount, self.dest_sends
|
||||
)
|
||||
|
||||
# update status
|
||||
# Update status
|
||||
self.status = TxRequest.ACCEPTED
|
||||
|
||||
# Create source transaction
|
||||
@@ -336,6 +353,8 @@ class TxRequest(models.Model):
|
||||
self.save()
|
||||
|
||||
|
||||
|
||||
|
||||
class BalanceError(Exception):
|
||||
def __init__(
|
||||
self, balance: int, source: User, dest: User, amount: int, commodity: Commodity
|
||||
@@ -364,3 +383,11 @@ def _tx_pre_save(sender, instance, *args, **kwargs):
|
||||
instance.amount,
|
||||
instance.commodity,
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_save, sender=TxRequest)
|
||||
def __tx_post_save(sender, instance, created, **kwargs):
|
||||
if not created:
|
||||
return
|
||||
assign_perm("view_txrequest", instance.source, instance)
|
||||
assign_perm("view_txrequest", instance.dest, instance)
|
||||
|
||||
Reference in New Issue
Block a user