You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.2 KiB
Python

2 years ago
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.urls import reverse
from django.views import View
from mixins.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
2 years ago
from two_factor.views.mixins import OTPRequiredMixin
from core.clients.platforms.agora import AgoraClient
from core.forms import AdForm
from core.models import Ad, LinkGroup
2 years ago
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
2 years ago
class Cheat(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.cheat())
2 years ago
context = {"class": "success", "message": "Cheat run"}
return render(request, self.template_name, context)
class AdNuke(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.nuke_ads())
2 years ago
context = {"class": "success", "message": "Ads nuked"}
return render(request, self.template_name, context)
2 years ago
class AdDist(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:
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))
2 years ago
context = {"class": "success", "message": "Ads distributed"}
return render(request, self.template_name, context)
class AdRedist(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:
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))
2 years ago
context = {"class": "success", "message": "Ads updated"}
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)
2 years ago
class AdList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/ad-list.html"
model = Ad
page_title = "List of ads"
2 years ago
list_url_name = "ads"
list_url_args = ["type"]
submit_url_name = "ad_create"
def get_context_data(self):
context = super().get_context_data()
self.extra_buttons = [
{
"url": reverse("ad_dist"),
"action": "distribute",
"method": "get",
"label": "Distribute ads",
"icon": "fa-solid fa-upload",
},
{
"url": reverse("ad_redist"),
"action": "redistribute",
"method": "get",
"label": "Update ads",
"icon": "fa-solid fa-refresh",
},
2 years ago
{
"url": reverse("cheat"),
"action": "cheat",
"method": "get",
"label": "Run cheat",
"icon": "fa-solid fa-bolt",
},
{
"url": reverse("ad_nuke"),
"action": "nuke",
"method": "get",
"label": "Nuke ads",
"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
2 years ago
class AdCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Ad
form_class = AdForm
submit_url_name = "ad_create"
class AdUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Ad
form_class = AdForm
submit_url_name = "ad_update"
class AdDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Ad