Enable dummy mode

This commit is contained in:
Mark Veidemanis 2023-04-19 10:12:16 +01:00
parent 2b7e83dc0d
commit da5d1badd8
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 107 additions and 41 deletions

View File

@ -41,6 +41,8 @@ ELASTICSEARCH_INDEX_ADS = getenv("ELASTICSEARCH_INDEX_ADS", "ads")
DEBUG = getenv("DEBUG", "false").lower() in trues DEBUG = getenv("DEBUG", "false").lower() in trues
PROFILER = getenv("PROFILER", "false").lower() in trues PROFILER = getenv("PROFILER", "false").lower() in trues
DUMMY = getenv("DUMMY", "false").lower() in trues
if DEBUG: if DEBUG:
import socket # only if you haven't already imported this import socket # only if you haven't already imported this

View File

@ -28,6 +28,10 @@ class LocalPlatformClient(ABC):
async def connect(self): async def connect(self):
self.api = AgoraDesk(self.instance.token) 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): async def call_method(self, method, *args, **kwargs):
""" """
@ -471,7 +475,11 @@ class LocalPlatformClient(ABC):
assets.add(asset) assets.add(asset)
currencies.add(currency) currencies.add(currency)
if not actioned: 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"]: if rtrn["success"]:
ads[ad_index][4] = True ads[ad_index][4] = True
throttled = 0 throttled = 0
@ -511,7 +519,11 @@ class LocalPlatformClient(ABC):
if ads is False: if ads is False:
return False return False
for ad_id in ads: 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_ids.append(rtrn["success"])
return all(return_ids) return all(return_ids)
@ -589,12 +601,20 @@ class LocalPlatformClient(ABC):
form["max_amount"] = round(max_amount, 2) form["max_amount"] = round(max_amount, 2)
if edit: 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"]: if ad_response["success"]:
self.instance.platform_ad_ids[ad_id] = str(ad.id) self.instance.platform_ad_ids[ad_id] = str(ad.id)
self.instance.save() self.instance.save()
else: 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"]: if ad_response["success"]:
ad_id = ad_response["response"]["data"]["ad_id"] ad_id = ad_response["response"]["data"]["ad_id"]
self.instance.platform_ad_ids[ad_id] = str(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: if currency not in supported_currencies:
continue continue
# Create the actual ad and pass in all the stuff # Create the actual ad and pass in all the stuff
rtrn = await self.create_ad( if not settings.DUMMY:
asset, rtrn = await self.create_ad(
countrycode, asset,
currency, countrycode,
provider, currency,
payment_details=account_info[currency], provider,
ad=ad, 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 # Bail on first error, let's not continue
if rtrn is False: if rtrn is False:
return False return False
@ -671,17 +698,25 @@ class LocalPlatformClient(ABC):
if currency in supported_currencies: if currency in supported_currencies:
if currency not in account_info: if currency not in account_info:
continue continue
rtrn = await self.create_ad( if not settings.DUMMY:
asset, rtrn = await self.create_ad(
countrycode, asset,
currency, countrycode,
provider, currency,
payment_details=account_info[currency], provider,
ad=ad, payment_details=account_info[currency],
visible=ad.visible, ad=ad,
edit=True, visible=ad.visible,
ad_id=ad_id, 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 # Bail on first error, let's not continue
if rtrn is False: if rtrn is False:
return False return False
@ -711,7 +746,11 @@ class LocalPlatformClient(ABC):
actioned = [] actioned = []
print("repeated", repeated) print("repeated", repeated)
for ad_id, country, currency in 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"]) actioned.append(rtrn["success"])
print("actioned", actioned) print("actioned", actioned)
@ -723,9 +762,18 @@ class LocalPlatformClient(ABC):
title = "Releasing escrow" title = "Releasing escrow"
await notify.sendmsg(self.instance.user, logmessage, title=title) 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": 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 return True
else: else:
logmessage = f"Release funds unsuccessful: {rtrn['message']}" logmessage = f"Release funds unsuccessful: {rtrn['message']}"
@ -925,6 +973,9 @@ class LocalPlatformClient(ABC):
""" """
Send the reference to a customer. 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: if self.instance.send is True:
await self.api.contact_message_post( await self.api.contact_message_post(
trade_id, trade_id,
@ -944,10 +995,15 @@ class LocalPlatformClient(ABC):
if not formatted_account_info: if not formatted_account_info:
log.error(f"Payment info invalid: {formatted_account_info}") log.error(f"Payment info invalid: {formatted_account_info}")
return return
await self.api.contact_message_post( if not settings.DUMMY:
trade_id, await self.api.contact_message_post(
f"Payment details: \n{formatted_account_info}", 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): def get_all_assets(self, platform):
raise Exception raise Exception
@ -1225,17 +1281,25 @@ class LocalPlatformClient(ABC):
currency = ad[3] currency = ad[3]
provider = ad[4] provider = ad[4]
ad_id = ad[1] ad_id = ad[1]
await self.create_ad( if not settings.DUMMY:
asset, await self.create_ad(
countrycode, asset,
currency, countrycode,
provider, currency,
payment_details=False, provider,
ad=None, payment_details=False,
visible=False, ad=None,
edit=True, visible=False,
ad_id=ad_id, 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): def format_ad(self, asset, currency, payment_details_text, ad):
""" """