Fix asset filter

This commit is contained in:
Mark Veidemanis 2023-02-14 07:20:47 +00:00
parent 0a89d96b86
commit b6952767d5
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 32 additions and 9 deletions

View File

@ -14,6 +14,7 @@ class AssetfilterTestCase(TestCase):
user=self.user,
name="Group1",
description="Test group",
when_no_data=6,
)
def test_get_allowed_prohibited(self):
@ -87,6 +88,18 @@ class AssetfilterTestCase(TestCase):
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
def test_get_allowed_no_data_prohibited(self):
self.group.when_no_data = 7
self.group.save()
self.asset_rule = AssetRule.objects.create(
user=self.user,
group=self.group,
asset="EUR",
status=0,
)
self.asset_rule.save()
self.assertFalse(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
def test_get_allowed_no_match(self):
self.asset_rule = AssetRule.objects.create(
user=self.user,
@ -118,6 +131,8 @@ class AssetfilterTestCase(TestCase):
)
self.asset_rule.save()
self.assertTrue(assetfilter.get_allowed(self.group, "EUR", "USD", "long"))
def test_get_allowed_always_allow(self):
self.asset_rule = AssetRule.objects.create(
user=self.user,

View File

@ -14,35 +14,43 @@ def get_allowed(group, base, quote, direction):
# If our base has allowed == False, we can only short it, or long the quote
base_rule = AssetRule.objects.filter(group=group, asset=base).first()
if base_rule:
if base_rule.status == 6:
mapped_status = update_status_from_mappings(base_rule.status, group)
if mapped_status == 6:
# Always allow
return True
elif base_rule.status == 7:
elif mapped_status == 7:
# Always deny
return False
elif base_rule.status == 3:
elif mapped_status == 3:
if direction == "long":
return False
elif base_rule.status == 2:
elif mapped_status == 2:
if direction == "short":
return False
# If our quote has allowed == False, we can only long it, or short the base
quote_rule = AssetRule.objects.filter(group=group, asset=quote).first()
if quote_rule:
if quote_rule.status == 6:
mapped_status = update_status_from_mappings(quote_rule.status, group)
if mapped_status == 6:
# Always allow
return True
elif quote_rule.status == 7:
elif mapped_status == 7:
# Always deny
return False
elif quote_rule.status == 3:
elif mapped_status == 3:
if direction == "short":
return False
elif quote_rule.status == 2:
elif mapped_status == 2:
if direction == "long":
return False
if not base_rule and not quote_rule:
if group.when_no_data == 7:
# Always deny
return False
elif group.when_no_data == 6:
# Always allow
return True
return True