from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse from mixins.views import ( ObjectCreate, ObjectDelete, ObjectList, ObjectRead, ObjectUpdate, ) from rest_framework import status from two_factor.views.mixins import OTPRequiredMixin from core.forms import LinkGroupForm from core.models import Aggregator, LinkGroup, Platform, Requisition class LinkGroupInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead): context_object_name_singular = "linkgroup" context_object_name = "linkgroups" detail_template = "partials/linkgroup-info.html" def get_object(self, **kwargs): pk = self.kwargs.get("pk") linkgroup = LinkGroup.objects.filter( user=self.request.user, id=pk, ).first() if not linkgroup: return HttpResponse(status=status.HTTP_404_NOT_FOUND) return linkgroup def get_context_data(self): context = super().get_context_data() aggregators = Aggregator.objects.filter( user=self.request.user, link_group=self.object, ) platforms = Platform.objects.filter( user=self.request.user, link_group=self.object, ) requisitions = Requisition.objects.filter( user=self.request.user, aggregator__in=aggregators, ) context["aggregators"] = aggregators context["platforms"] = platforms context["requisitions"] = requisitions context["linkgroup"] = self.object payees = self.object.payees() simulation = {} profit = 1000 profit_platform = profit * (self.object.platform_owner_cut_percentage / 100) profit_requisition = profit * ( self.object.requisition_owner_cut_percentage / 100 ) profit_operator = profit * (self.object.operator_cut_percentage / 100) pay_list = {} platform_pay_list = [] if "platform" in payees: for payee in payees["platform"]: cast = { "name": payee.name, "address": payee.address, "amount": profit_platform / len(payees["platform"]), "max": profit_platform, } if payee not in pay_list: pay_list[payee] = {} if "amount" in pay_list[payee]: pay_list[payee]["amount"] += cast["amount"] else: pay_list[payee] = dict(cast) platform_pay_list.append(cast) requisition_pay_list = [] if "requisition" in payees: for payee in payees["requisition"]: cast = { "name": payee.name, "address": payee.address, "amount": profit_requisition / len(payees["requisition"]), "max": profit_requisition, } if payee not in pay_list: pay_list[payee] = {} if "amount" in pay_list[payee]: pay_list[payee]["amount"] += cast["amount"] else: pay_list[payee] = dict(cast) requisition_pay_list.append(cast) simulation[("Platform", profit_platform)] = platform_pay_list simulation[("Requisition", profit_requisition)] = requisition_pay_list simulation[("Operator", profit_operator)] = [] context["pay_list"] = pay_list context["simulation"] = simulation return context class LinkGroupList(LoginRequiredMixin, OTPRequiredMixin, ObjectList): list_template = "partials/linkgroup-list.html" model = LinkGroup page_title = "List of link groups" page_subtitle = "Link groups are used to link aggregators and platforms" list_url_name = "linkgroups" list_url_args = ["type"] submit_url_name = "linkgroup_create" class LinkGroupCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate): model = LinkGroup form_class = LinkGroupForm submit_url_name = "linkgroup_create" class LinkGroupUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate): model = LinkGroup form_class = LinkGroupForm submit_url_name = "linkgroup_update" class LinkGroupDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete): model = LinkGroup