From 2b7e83dc0de1f1bd070bb020fe94581b63a7c649 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 18 Apr 2023 11:25:56 +0100 Subject: [PATCH] Begin implementing ad deduplication and re-enable releasing --- app/urls.py | 5 +++++ core/clients/platform.py | 32 +++++++++++++++++--------------- core/lib/schemas/agora_s.py | 1 + core/views/ads.py | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/urls.py b/app/urls.py index 086e731..5c37927 100644 --- a/app/urls.py +++ b/app/urls.py @@ -200,6 +200,11 @@ urlpatterns = [ ads.AdRedist.as_view(), name="ad_redist", ), + path( + "ops/ads/dedup/", + ads.AdDedup.as_view(), + name="ad_dedup", + ), path( "ops/ads/cheat/", ads.Cheat.as_view(), diff --git a/core/clients/platform.py b/core/clients/platform.py index a6f5b7a..a0b4d92 100644 --- a/core/clients/platform.py +++ b/core/clients/platform.py @@ -610,6 +610,7 @@ class LocalPlatformClient(ABC): :rtype: bool or dict """ dist_list = list(self.create_distribution_list(ad, filter_asset)) + print("Dist list", dist_list) our_ads = await self.enum_ads() ( supported_currencies, @@ -620,10 +621,12 @@ class LocalPlatformClient(ABC): log.error("Could not get our ads") return False our_ads = [(x[0], x[2], x[3], x[4]) for x in our_ads] + print("our_ads", our_ads) to_return = [] for asset, countrycode, currency, provider in dist_list: if (asset, countrycode, currency, provider) not in our_ads: + print("NOT IN OUR ADS", asset, countrycode, currency, provider) if currency not in supported_currencies: continue # Create the actual ad and pass in all the stuff @@ -692,7 +695,9 @@ class LocalPlatformClient(ABC): :rtype: list """ existing_ads = await self.enum_ads() + print("EXISTING", existing_ads) _size = len(existing_ads) + print("SIZE", _size) repeated = [] for i in range(_size): k = i + 1 @@ -704,10 +709,12 @@ class LocalPlatformClient(ABC): repeated.append(existing_ads[i]) actioned = [] + print("repeated", repeated) for ad_id, country, currency in repeated: rtrn = await self.api.ad_delete(ad_id) actioned.append(rtrn["success"]) + print("actioned", actioned) return all(actioned) async def release_trade_escrow(self, trade_id, reference): @@ -716,21 +723,16 @@ class LocalPlatformClient(ABC): title = "Releasing escrow" await notify.sendmsg(self.instance.user, logmessage, title=title) - # THIS IS NOT A COMMENT - # THIS IS FOR SECURITY - # WHEN IT HAS BEEN CONFIRMED TO WORK - # THIS CAN BE UNCOMMENTED - # rtrn = await self.release_funds(trade_id) - # if rtrn["message"] == "OK": - # await self.api.contact_message_post(trade_id, "Thanks! Releasing now :)") - # return True - # else: - # logmessage = f"Release funds unsuccessful: {rtrn['message']}" - # title = "Release unsuccessful" - # log.error(logmessage) - # await notify.sendmsg(self.instance.user, logmessage, title=title) - # return - # UNCOMMENT TO HERE + rtrn = await self.release_funds(trade_id) + if rtrn["message"] == "OK": + await self.api.contact_message_post(trade_id, "Thanks! Releasing now :)") + return True + else: + logmessage = f"Release funds unsuccessful: {rtrn['message']}" + title = "Release unsuccessful" + log.error(logmessage) + await notify.sendmsg(self.instance.user, logmessage, title=title) + return async def update_trade_tx(self, stored_trade, tx_obj): """ diff --git a/core/lib/schemas/agora_s.py b/core/lib/schemas/agora_s.py index d510dcc..9c6bed0 100644 --- a/core/lib/schemas/agora_s.py +++ b/core/lib/schemas/agora_s.py @@ -294,4 +294,5 @@ AdsSchema = { "message": "message", "ad_count": "response.data.ad_count", "ad_list": "response.data.ad_list", + "pagination": "response.pagination", } diff --git a/core/views/ads.py b/core/views/ads.py index 67a259c..4f68da2 100644 --- a/core/views/ads.py +++ b/core/views/ads.py @@ -81,6 +81,19 @@ class AdRedist(LoginRequiredMixin, OTPRequiredMixin, View): return render(request, self.template_name, context) +class AdDedup(LoginRequiredMixin, OTPRequiredMixin, View): + template_name = "mixins/partials/notify.html" + + def get(self, request): + platforms = get_platforms(request.user) + for platform in platforms: + run = synchronize_async_helper(AgoraClient(platform)) + synchronize_async_helper(run.strip_duplicate_ads()) + + context = {"class": "success", "message": "Ads deduplicated"} + return render(request, self.template_name, context) + + class AdList(LoginRequiredMixin, OTPRequiredMixin, ObjectList): list_template = "partials/ad-list.html" model = Ad @@ -123,6 +136,14 @@ class AdList(LoginRequiredMixin, OTPRequiredMixin, ObjectList): "icon": "fa-solid fa-bomb", "confirm": True, }, + { + "url": reverse("ad_dedup"), + "action": "deduplicate", + "method": "get", + "label": "Deduplicate ads", + "icon": "fa-thin fa-copy", + "confirm": True, + }, ] return context