From da5d1badd8dd06332d0432ccb5c1aff1fa2f5424 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 19 Apr 2023 10:12:16 +0100 Subject: [PATCH] Enable dummy mode --- app/local_settings.py | 2 + core/clients/platform.py | 146 ++++++++++++++++++++++++++++----------- 2 files changed, 107 insertions(+), 41 deletions(-) diff --git a/app/local_settings.py b/app/local_settings.py index b087578..632b4cf 100644 --- a/app/local_settings.py +++ b/app/local_settings.py @@ -41,6 +41,8 @@ ELASTICSEARCH_INDEX_ADS = getenv("ELASTICSEARCH_INDEX_ADS", "ads") DEBUG = getenv("DEBUG", "false").lower() in trues PROFILER = getenv("PROFILER", "false").lower() in trues +DUMMY = getenv("DUMMY", "false").lower() in trues + if DEBUG: import socket # only if you haven't already imported this diff --git a/core/clients/platform.py b/core/clients/platform.py index a0b4d92..32c0cd5 100644 --- a/core/clients/platform.py +++ b/core/clients/platform.py @@ -28,6 +28,10 @@ class LocalPlatformClient(ABC): async def connect(self): self.api = AgoraDesk(self.instance.token) + if settings.DUMMY: + log.info( + "DUMMY: Dummy mode is enabled. Destructive commands will be printed." + ) async def call_method(self, method, *args, **kwargs): """ @@ -471,7 +475,11 @@ class LocalPlatformClient(ABC): assets.add(asset) currencies.add(currency) if not actioned: - rtrn = await self.api.ad_equation(ad_id, new_formula) + if not settings.DUMMY: + rtrn = await self.api.ad_equation(ad_id, new_formula) + else: + log.debug(f"DUMMY: ad_equation({ad_id}, {new_formula})") + rtrn = {"success": True} if rtrn["success"]: ads[ad_index][4] = True throttled = 0 @@ -511,7 +519,11 @@ class LocalPlatformClient(ABC): if ads is False: return False for ad_id in ads: - rtrn = await self.api.ad_delete(ad_id) + if not settings.DUMMY: + rtrn = await self.api.ad_delete(ad_id) + else: + log.debug(f"DUMMY: ad_delete({ad_id})") + rtrn = {"success": True} return_ids.append(rtrn["success"]) return all(return_ids) @@ -589,12 +601,20 @@ class LocalPlatformClient(ABC): form["max_amount"] = round(max_amount, 2) if edit: - ad_response = await self.api.ad(ad_id=ad_id, **form) + if not settings.DUMMY: + ad_response = await self.api.ad(ad_id=ad_id, **form) + else: + log.debug(f"DUMMY: ad({ad_id}, {form})") + ad_response = {"success": True} if ad_response["success"]: self.instance.platform_ad_ids[ad_id] = str(ad.id) self.instance.save() else: - ad_response = await self.api.ad_create(**form) + if not settings.DUMMY: + ad_response = await self.api.ad_create(**form) + else: + log.debug(f"DUMMY: ad_create({form})") + ad_response = {"success": True} if ad_response["success"]: ad_id = ad_response["response"]["data"]["ad_id"] self.instance.platform_ad_ids[ad_id] = str(ad.id) @@ -630,14 +650,21 @@ class LocalPlatformClient(ABC): if currency not in supported_currencies: continue # Create the actual ad and pass in all the stuff - rtrn = await self.create_ad( - asset, - countrycode, - currency, - provider, - payment_details=account_info[currency], - ad=ad, - ) + if not settings.DUMMY: + rtrn = await self.create_ad( + asset, + countrycode, + currency, + provider, + payment_details=account_info[currency], + ad=ad, + ) + else: + log.debug( + f"DUMMY: create_ad({asset}, {countrycode}, {currency}, " + f"{provider}, {account_info[currency]}, {ad})" + ) + rtrn = {"success": True} # Bail on first error, let's not continue if rtrn is False: return False @@ -671,17 +698,25 @@ class LocalPlatformClient(ABC): if currency in supported_currencies: if currency not in account_info: continue - rtrn = await self.create_ad( - asset, - countrycode, - currency, - provider, - payment_details=account_info[currency], - ad=ad, - visible=ad.visible, - edit=True, - ad_id=ad_id, - ) + if not settings.DUMMY: + rtrn = await self.create_ad( + asset, + countrycode, + currency, + provider, + payment_details=account_info[currency], + ad=ad, + visible=ad.visible, + edit=True, + ad_id=ad_id, + ) + else: + log.debug( + f"DUMMY: create_ad({asset}, {countrycode}, {currency}, " + f"{provider}, {account_info[currency]}, {ad}, " + f"{ad.visible}, True, {ad_id})" + ) + rtrn = {"success": True} # Bail on first error, let's not continue if rtrn is False: return False @@ -711,7 +746,11 @@ class LocalPlatformClient(ABC): actioned = [] print("repeated", repeated) for ad_id, country, currency in repeated: - rtrn = await self.api.ad_delete(ad_id) + if not settings.DUMMY: + rtrn = await self.api.ad_delete(ad_id) + else: + log.debug(f"DUMMY: ad_delete({ad_id})") + rtrn = {"success": True} actioned.append(rtrn["success"]) print("actioned", actioned) @@ -723,9 +762,18 @@ class LocalPlatformClient(ABC): title = "Releasing escrow" await notify.sendmsg(self.instance.user, logmessage, title=title) - rtrn = await self.release_funds(trade_id) + if not settings.DUMMY: + rtrn = await self.release_funds(trade_id) + else: + log.debug(f"DUMMY: release_funds({trade_id})") + rtrn = {"message": "OK"} if rtrn["message"] == "OK": - await self.api.contact_message_post(trade_id, "Thanks! Releasing now :)") + if not settings.DUMMY: + await self.api.contact_message_post( + trade_id, "Thanks! Releasing now :)" + ) + else: + log.debug(f"DUMMY: contact_message_post({trade_id}, 'Thanks!')") return True else: logmessage = f"Release funds unsuccessful: {rtrn['message']}" @@ -925,6 +973,9 @@ class LocalPlatformClient(ABC): """ Send the reference to a customer. """ + if settings.DUMMY: + log.debug(f"DUMMY: Sending reference {reference} to {trade_id}") + return if self.instance.send is True: await self.api.contact_message_post( trade_id, @@ -944,10 +995,15 @@ class LocalPlatformClient(ABC): if not formatted_account_info: log.error(f"Payment info invalid: {formatted_account_info}") return - await self.api.contact_message_post( - trade_id, - f"Payment details: \n{formatted_account_info}", - ) + if not settings.DUMMY: + await self.api.contact_message_post( + trade_id, + f"Payment details: \n{formatted_account_info}", + ) + else: + log.debug( + f"DUMMY: contact_message_post({trade_id}, {formatted_account_info})" + ) def get_all_assets(self, platform): raise Exception @@ -1225,17 +1281,25 @@ class LocalPlatformClient(ABC): currency = ad[3] provider = ad[4] ad_id = ad[1] - await self.create_ad( - asset, - countrycode, - currency, - provider, - payment_details=False, - ad=None, - visible=False, - edit=True, - ad_id=ad_id, - ) + if not settings.DUMMY: + await self.create_ad( + asset, + countrycode, + currency, + provider, + payment_details=False, + ad=None, + visible=False, + edit=True, + ad_id=ad_id, + ) + else: + log.debug( + ( + f"DUMMY: create_ad({ad_id}, {asset}, {countrycode}, {currency}," + f" {provider}, False, None, False, True, {ad_id})" + ) + ) def format_ad(self, asset, currency, payment_details_text, ad): """