From ae4e5ae96441e39e0548b37126ecf0912525a5e1 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 5 Apr 2023 17:30:35 +0100 Subject: [PATCH] Fix referencing link groups in ad functions --- core/models.py | 8 ++++++++ core/views/ads.py | 48 ++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/core/models.py b/core/models.py index e80e0d7..621be99 100644 --- a/core/models.py +++ b/core/models.py @@ -87,6 +87,14 @@ class LinkGroup(models.Model): return payees + @property + def platforms(self): + return Platform.objects.filter(link_group=self) + + @property + def aggregators(self): + return Aggregator.objects.filter(link_group=self) + class Aggregator(models.Model): """ diff --git a/core/views/ads.py b/core/views/ads.py index 998d766..67a259c 100644 --- a/core/views/ads.py +++ b/core/views/ads.py @@ -7,19 +7,32 @@ from two_factor.views.mixins import OTPRequiredMixin from core.clients.platforms.agora import AgoraClient from core.forms import AdForm -from core.models import Ad +from core.models import Ad, LinkGroup from core.views.helpers import synchronize_async_helper +def get_linkgroups(user): + return LinkGroup.objects.filter(user=user, enabled=True) + + +def get_platforms(user): + groups = get_linkgroups(user) + platforms = [] + for group in groups: + for platform in group.platforms.all(): + platforms.append(platform) + + return platforms + + class Cheat(LoginRequiredMixin, OTPRequiredMixin, View): template_name = "mixins/partials/notify.html" def get(self, request): - ads = Ad.objects.filter(user=request.user, enabled=True) - for ad in ads: - for platform in ad.platforms.all(): - run = synchronize_async_helper(AgoraClient(platform)) - synchronize_async_helper(run.cheat()) + platforms = get_platforms(request.user) + for platform in platforms: + run = synchronize_async_helper(AgoraClient(platform)) + synchronize_async_helper(run.cheat()) context = {"class": "success", "message": "Cheat run"} return render(request, self.template_name, context) @@ -29,11 +42,10 @@ class AdNuke(LoginRequiredMixin, OTPRequiredMixin, View): template_name = "mixins/partials/notify.html" def get(self, request): - ads = Ad.objects.filter(user=request.user, enabled=True) - for ad in ads: - for platform in ad.platforms.all(): - run = synchronize_async_helper(AgoraClient(platform)) - synchronize_async_helper(run.nuke_ads()) + platforms = get_platforms(request.user) + for platform in platforms: + run = synchronize_async_helper(AgoraClient(platform)) + synchronize_async_helper(run.nuke_ads()) context = {"class": "success", "message": "Ads nuked"} return render(request, self.template_name, context) @@ -45,9 +57,10 @@ class AdDist(LoginRequiredMixin, OTPRequiredMixin, View): def get(self, request): ads = Ad.objects.filter(user=request.user, enabled=True) for ad in ads: - for platform in ad.platforms.all(): - run = synchronize_async_helper(AgoraClient(platform)) - synchronize_async_helper(run.dist_countries(ad)) + if ad.link_group is not None: + for platform in ad.link_group.platforms.all(): + run = synchronize_async_helper(AgoraClient(platform)) + synchronize_async_helper(run.dist_countries(ad)) context = {"class": "success", "message": "Ads distributed"} return render(request, self.template_name, context) @@ -59,9 +72,10 @@ class AdRedist(LoginRequiredMixin, OTPRequiredMixin, View): def get(self, request): ads = Ad.objects.filter(user=request.user, enabled=True) for ad in ads: - for platform in ad.platforms.all(): - run = synchronize_async_helper(AgoraClient(platform)) - synchronize_async_helper(run.redist_countries(ad)) + if ad.link_group is not None: + for platform in ad.link_group.platforms.all(): + run = synchronize_async_helper(AgoraClient(platform)) + synchronize_async_helper(run.redist_countries(ad)) context = {"class": "success", "message": "Ads updated"} return render(request, self.template_name, context)