From fa7ea66c6583b9cc3b838d5e1f99bdd45f1a75c2 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 10 Mar 2023 00:23:05 +0000 Subject: [PATCH] Show trades --- app/urls.py | 6 ++ core/clients/platform.py | 24 ++++-- core/clients/platforms/agora.py | 2 +- core/clients/platforms/api/agoradesk.py | 8 +- core/lib/antifraud.py | 3 +- core/lib/schemas/__init__.py | 1 + core/lib/schemas/agora_s.py | 86 ++++++++++++++++++++ core/models.py | 4 + core/templates/base.html | 6 ++ core/templates/partials/platform-trades.html | 82 +++++++++++++++++++ core/views/platforms.py | 27 +++++- handler/lib/antifraud.py | 3 + 12 files changed, 236 insertions(+), 16 deletions(-) create mode 100644 core/lib/schemas/agora_s.py create mode 100644 core/templates/partials/platform-trades.html diff --git a/app/urls.py b/app/urls.py index 011201d..c3895e5 100644 --- a/app/urls.py +++ b/app/urls.py @@ -143,4 +143,10 @@ urlpatterns = [ platforms.PlatformDelete.as_view(), name="platform_delete", ), + # Trades + path( + "trades//", + platforms.PlatformTrades.as_view(), + name="trades", + ), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/clients/platform.py b/core/clients/platform.py index 06fec2e..a3319d7 100644 --- a/core/clients/platform.py +++ b/core/clients/platform.py @@ -28,6 +28,15 @@ class LocalPlatformClient(ABC): async def connect(self): self.api = AgoraDesk(self.instance.token) + async def call_method(self, method, *args, **kwargs): + """ + Call a method using the self.api object. + """ + if hasattr(self.api, method): + return await getattr(self.api, method)(*args, **kwargs) + else: + raise Exception(f"Method {method} not found in {self.name} API.") + # TODO: do in schedules # def setup_loop(self): # """ @@ -62,19 +71,18 @@ class LocalPlatformClient(ABC): async def wrap_dashboard(self, dash=None): # backwards compatibility with TX if not dash: - dash = await self.api.dashboard() + # dash = await self.api.dashboard() + dash = await self.call("dashboard") + + print("DASH22", dash) # if dash["response"] is None: # return False dash_tmp = {} if not dash: return False - if not dash["response"]: - return False - if "data" not in dash["response"]: - # log.error(f"Data not in dashboard response: {dash}") - return dash_tmp - if dash["response"]["data"]["contact_count"] > 0: - for contact in dash["response"]["data"]["contact_list"]: + + if dash["contact_count"] > 0: + for contact in dash["contact_list"]: contact_id = contact["data"]["contact_id"] dash_tmp[contact_id] = contact return dash_tmp diff --git a/core/clients/platforms/agora.py b/core/clients/platforms/agora.py index f2ca968..d690f6e 100644 --- a/core/clients/platforms/agora.py +++ b/core/clients/platforms/agora.py @@ -6,7 +6,7 @@ from core.clients.platform import LocalPlatformClient from core.lib.money import Money -class AgoraClient(BaseClient, LocalPlatformClient): +class AgoraClient(LocalPlatformClient, BaseClient): """ AgoraDesk API handler. """ diff --git a/core/clients/platforms/api/agoradesk.py b/core/clients/platforms/api/agoradesk.py index f5ac78f..f7e4e21 100644 --- a/core/clients/platforms/api/agoradesk.py +++ b/core/clients/platforms/api/agoradesk.py @@ -87,6 +87,7 @@ class AgoraDesk: async with aiohttp.ClientSession() as session: async with session.post(api_call_url, **cast) as response_raw: response = await response_raw.json() + status_code = response_raw.status else: # response = httpx.get(url=api_call_url, headers=headers, params=query_values) @@ -95,12 +96,15 @@ class AgoraDesk: async with aiohttp.ClientSession() as session: async with session.get(api_call_url, **cast) as response_raw: response = await response_raw.json() + status_code = response_raw.status if response: + print("YES RESPONSE", response) logger.debug(response) - result["status"] = response.code - if response.code == 200: + result["status"] = status_code + if status_code == 200: result["success"] = True result["message"] = "OK" + result["response"] = response else: result["message"] = "API ERROR" diff --git a/core/lib/antifraud.py b/core/lib/antifraud.py index a675bd2..aaaaa5e 100644 --- a/core/lib/antifraud.py +++ b/core/lib/antifraud.py @@ -107,5 +107,4 @@ class AntiFraud(object): # ) -if __name__ == "__main__": - antifraud = AntiFraud() +antifraud = AntiFraud() diff --git a/core/lib/schemas/__init__.py b/core/lib/schemas/__init__.py index 940aae0..26747db 100644 --- a/core/lib/schemas/__init__.py +++ b/core/lib/schemas/__init__.py @@ -1 +1,2 @@ +from core.lib.schemas import agora_s # noqa from core.lib.schemas import nordigen_s # noqa diff --git a/core/lib/schemas/agora_s.py b/core/lib/schemas/agora_s.py new file mode 100644 index 0000000..3d5b73d --- /dev/null +++ b/core/lib/schemas/agora_s.py @@ -0,0 +1,86 @@ +from pydantic import BaseModel + + +class ContactDataBuyerSeller(BaseModel): + username: str + name: str + feedback_score: int + trade_count: str + last_online: str + + +class ContactDataAd(BaseModel): + payment_method: str + trade_type: str + advertiser: ContactDataBuyerSeller + asset: str + id: str + + +class ContactData(BaseModel): + buyer: ContactDataBuyerSeller + seller: ContactDataBuyerSeller + amount: str + amount_xmr: str + fee_xmr: str + advertisement: ContactDataAd + contact_id: str + currency: str + country: str + account_info: str + price_equation: str + is_buying: bool + is_selling: bool + created_at: str + escrowed_at: str + funded_at: str + canceled_at: str | None + closed_at: str | None + msg: str + released_at: str | None + payment_completed_at: str | None + disputed_at: str | None + arbitrated: bool + transfer_to_seller_non_custodial_wallet_transaction_confirmations: str | None + transfer_to_buyer_settlement_wallet_transaction_id: str | None + transfer_to_buyer_settlement_wallet_transaction_key: str | None + buyer_settlement_address: str | None + buyer_settlement_fee_level: str | None + seller_non_custodial_wallet_mnemonic: str | None + transfer_to_seller_non_custodial_wallet_transaction_id: str | None + + +class ContactActions(BaseModel): + advertisement_public_view: str + advertisement_url: str + message_post_url: str + messages_url: str + release_url: str + + +class Contact(BaseModel): + data: ContactData + actions: ContactActions + + +class ResponseData(BaseModel): + contact_count: int + contact_list: list[Contact] + + +class Response(BaseModel): + data: ResponseData + + +class Dashboard(BaseModel): + success: bool + message: str + response: Response + + +DashboardSchema = { + "success": "success", + "message": "message", + "contact_count": "response.data.contact_count", + "contact_list": "response.data.contact_list", +} diff --git a/core/models.py b/core/models.py index 13c4e77..d111065 100644 --- a/core/models.py +++ b/core/models.py @@ -122,6 +122,10 @@ class Platform(models.Model): enabled = models.BooleanField(default=True) + @classmethod + def get_for_user(cls, user): + return cls.objects.filter(user=user, enabled=True) + @property def currencies(self): return Aggregator.get_currencies_for_platform(self) diff --git a/core/templates/base.html b/core/templates/base.html index ffa0857..f789875 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -242,6 +242,12 @@