From 9b6180ac5b3ed9d03ffa04f903db754cfac6765e Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 10 Mar 2023 15:04:39 +0000 Subject: [PATCH] Allow checking account ID whitelist --- core/clients/platform.py | 22 ++++++++++++++------ core/forms.py | 2 ++ core/management/commands/polling.py | 3 +++ core/migrations/0014_ad_account_whitelist.py | 18 ++++++++++++++++ core/models.py | 1 + 5 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 core/migrations/0014_ad_account_whitelist.py diff --git a/core/clients/platform.py b/core/clients/platform.py index 9420aac..d74007b 100644 --- a/core/clients/platform.py +++ b/core/clients/platform.py @@ -665,7 +665,7 @@ class LocalPlatformClient(ABC): ( supported_currencies, account_info, - ) = self.get_valid_account_details() + ) = self.get_valid_account_details(ad) # Let's get rid of the ad IDs and make it a tuple like dist_list our_ads = [(x[0], x[2], x[3], x[4]) for x in our_ads] if not our_ads: @@ -704,7 +704,7 @@ class LocalPlatformClient(ABC): ( supported_currencies, account_info, - ) = self.get_valid_account_details(self.name) + ) = self.get_valid_account_details(ad) if not our_ads: log.error("Could not get our ads.") return False @@ -935,7 +935,7 @@ class LocalPlatformClient(ABC): log.info(f"Sending bank details/reference for {trade_id}") if self.instance.send is True: print("SEND IS TRUE") - account_info = self.get_matching_account_details(currency) + account_info = self.get_matching_account_details(currency, ad) print("ACCOUNT INFO", account_info) formatted_account_info = self.format_payment_details( currency, account_info, ad, real=True @@ -1141,14 +1141,24 @@ class LocalPlatformClient(ABC): else: yield (asset, countrycode, currency, provider) - def get_valid_account_details(self): + def get_valid_account_details(self, ad): currencies = self.instance.currencies account_info = self.instance.account_info + print("GET VALID", ad) + account_whitelist = ad.account_whitelist + if account_whitelist: + whitelist = account_whitelist.splitlines() + else: + whitelist = None + print("WHITELIST", whitelist) currency_account_info_map = {} for currency in currencies: for bank, accounts in account_info.items(): for account in accounts: if account["currency"] == currency: + if whitelist: + if account["account_id"] not in whitelist: + continue currency_account_info_map[currency] = account["account_number"] currency_account_info_map[currency]["bank"] = bank.split("_")[0] currency_account_info_map[currency]["recipient"] = account[ @@ -1156,11 +1166,11 @@ class LocalPlatformClient(ABC): ] return (currencies, currency_account_info_map) - def get_matching_account_details(self, currency): + def get_matching_account_details(self, currency, ad): ( supported_currencies, currency_account_info_map, - ) = self.get_valid_account_details() + ) = self.get_valid_account_details(ad) if currency not in supported_currencies: return False return currency_account_info_map[currency] diff --git a/core/forms.py b/core/forms.py index 1ca7f3b..8ef4892 100644 --- a/core/forms.py +++ b/core/forms.py @@ -155,6 +155,7 @@ class AdForm(RestrictedFormMixin, ModelForm): "provider_list", "platforms", "aggregators", + "account_whitelist", "visible", "enabled", ) @@ -169,6 +170,7 @@ class AdForm(RestrictedFormMixin, ModelForm): "provider_list": "List of providers to distribute ads for.", "platforms": "Enabled platforms for this ad", "aggregators": "Enabled aggregators for this ad", + "account_whitelist": "List of account IDs to use, one per line.", "visible": "Whether or not this ad is visible.", "enabled": "Whether or not this ad is enabled.", } diff --git a/core/management/commands/polling.py b/core/management/commands/polling.py index 6e46956..107a111 100644 --- a/core/management/commands/polling.py +++ b/core/management/commands/polling.py @@ -12,14 +12,17 @@ log = logs.get_logger("polling") INTERVAL = 5 + async def poll_aggregator(aggregator): print("Polling aggregator", aggregator) + async def poll_platform(platform): print("Polling platform", platform) client = await AgoraClient(platform) await client.poll() + async def job(): platforms = Platform.objects.filter(enabled=True) aggregators = Aggregator.objects.filter(enabled=True) diff --git a/core/migrations/0014_ad_account_whitelist.py b/core/migrations/0014_ad_account_whitelist.py new file mode 100644 index 0000000..9401f6e --- /dev/null +++ b/core/migrations/0014_ad_account_whitelist.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.7 on 2023-03-10 15:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0013_platform_platform_ad_ids'), + ] + + operations = [ + migrations.AddField( + model_name='ad', + name='account_whitelist', + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/core/models.py b/core/models.py index 08867ce..06ac9db 100644 --- a/core/models.py +++ b/core/models.py @@ -225,6 +225,7 @@ class Ad(models.Model): aggregators = models.ManyToManyField(Aggregator) account_map = models.JSONField(default=dict) + account_whitelist = models.TextField(null=True, blank=True) visible = models.BooleanField(default=True) enabled = models.BooleanField(default=True)