From b6952767d5cf8b88bd691a930fc0077fd68445b1 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 14 Feb 2023 07:20:47 +0000 Subject: [PATCH] Fix asset filter --- core/tests/trading/test_assetfilter.py | 15 +++++++++++++++ core/trading/assetfilter.py | 26 +++++++++++++++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/core/tests/trading/test_assetfilter.py b/core/tests/trading/test_assetfilter.py index 535f7f2..8672ef5 100644 --- a/core/tests/trading/test_assetfilter.py +++ b/core/tests/trading/test_assetfilter.py @@ -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, diff --git a/core/trading/assetfilter.py b/core/trading/assetfilter.py index 2cee70e..86b79b7 100644 --- a/core/trading/assetfilter.py +++ b/core/trading/assetfilter.py @@ -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