fix: one-side pairlocks not showing in UI

This commit is contained in:
Matthias
2024-12-21 14:30:47 +01:00
parent 0b0bbd4c42
commit 40cb70fca1
3 changed files with 11 additions and 7 deletions

View File

@@ -39,7 +39,7 @@ class PairLock(ModelBase):
@staticmethod @staticmethod
def query_pair_locks( def query_pair_locks(
pair: str | None, now: datetime, side: str = "*" pair: str | None, now: datetime, side: str | None = None
) -> ScalarResult["PairLock"]: ) -> ScalarResult["PairLock"]:
""" """
Get all currently active locks for this pair Get all currently active locks for this pair
@@ -53,9 +53,9 @@ class PairLock(ModelBase):
] ]
if pair: if pair:
filters.append(PairLock.pair == pair) filters.append(PairLock.pair == pair)
if side != "*": if side is not None and side != "*":
filters.append(or_(PairLock.side == side, PairLock.side == "*")) filters.append(or_(PairLock.side == side, PairLock.side == "*"))
else: elif side is not None:
filters.append(PairLock.side == "*") filters.append(PairLock.side == "*")
return PairLock.session.scalars(select(PairLock).filter(*filters)) return PairLock.session.scalars(select(PairLock).filter(*filters))

View File

@@ -67,13 +67,14 @@ class PairLocks:
@staticmethod @staticmethod
def get_pair_locks( def get_pair_locks(
pair: str | None, now: datetime | None = None, side: str = "*" pair: str | None, now: datetime | None = None, side: str | None = None
) -> Sequence[PairLock]: ) -> Sequence[PairLock]:
""" """
Get all currently active locks for this pair Get all currently active locks for this pair
:param pair: Pair to check for. Returns all current locks if pair is empty :param pair: Pair to check for. Returns all current locks if pair is empty
:param now: Datetime object (generated via datetime.now(timezone.utc)). :param now: Datetime object (generated via datetime.now(timezone.utc)).
defaults to datetime.now(timezone.utc) defaults to datetime.now(timezone.utc)
:param side: Side get locks for, can be 'long', 'short', '*' or None
""" """
if not now: if not now:
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
@@ -88,7 +89,7 @@ class PairLocks:
lock.lock_end_time >= now lock.lock_end_time >= now
and lock.active is True and lock.active is True
and (pair is None or lock.pair == pair) and (pair is None or lock.pair == pair)
and (lock.side == "*" or lock.side == side) and (side is None or lock.side == "*" or lock.side == side)
) )
] ]
return locks return locks

View File

@@ -37,6 +37,7 @@ def test_PairLocks(use_db):
assert not PairLocks.is_pair_locked(pair) assert not PairLocks.is_pair_locked(pair)
assert not PairLocks.is_pair_locked(pair, side="long") assert not PairLocks.is_pair_locked(pair, side="long")
assert PairLocks.is_pair_locked(pair, side="short") assert PairLocks.is_pair_locked(pair, side="short")
assert len(PairLocks.get_pair_locks(pair)) == 1
# XRP/BTC should not be locked now # XRP/BTC should not be locked now
pair = "XRP/BTC" pair = "XRP/BTC"
@@ -47,9 +48,11 @@ def test_PairLocks(use_db):
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4)) PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4))
assert PairLocks.is_pair_locked(pair) assert PairLocks.is_pair_locked(pair)
# Get both locks from above # Get all locks from above
locks = PairLocks.get_pair_locks(None) locks = PairLocks.get_pair_locks(None)
assert len(locks) == 2 assert len(locks) == 4
assert len(PairLocks.get_pair_locks(None, side="*")) == 2
# Unlock original pair # Unlock original pair
pair = "ETH/BTC" pair = "ETH/BTC"