pluto/core/views/aggregators.py

267 lines
8.4 KiB
Python
Raw Normal View History

from django.contrib.auth.mixins import LoginRequiredMixin
2023-03-08 12:48:05 +00:00
from django.http import HttpResponse
2023-03-09 16:44:16 +00:00
from django.shortcuts import render
from django.urls import reverse
2023-03-08 12:48:05 +00:00
from django.views import View
2023-03-08 17:04:47 +00:00
from mixins.views import (
ObjectCreate,
ObjectDelete,
ObjectList,
ObjectRead,
ObjectUpdate,
)
from two_factor.views.mixins import OTPRequiredMixin
2023-03-08 12:48:05 +00:00
from core.clients.aggregators.nordigen import NordigenClient
from core.forms import AggregatorForm
from core.models import Aggregator
from core.util import logs
2023-03-09 16:44:16 +00:00
from core.views.helpers import synchronize_async_helper
log = logs.get_logger(__name__)
2023-03-09 16:44:16 +00:00
class RequestBankFetch(LoginRequiredMixin, OTPRequiredMixin, View):
template_name = "mixins/partials/notify.html"
def get(self, request, pk=None):
if pk:
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
aggregators = [aggregator]
2023-03-08 12:48:05 +00:00
2023-03-09 16:44:16 +00:00
except Aggregator.DoesNotExist:
message = "Aggregator does not exist"
context = {
"message": message,
"class": "danger",
}
return self.render_to_response(context)
else:
aggregators = Aggregator.objects.filter(user=self.request.user)
2023-03-08 12:48:05 +00:00
2023-03-09 16:44:16 +00:00
for agg in aggregators:
agg.fetch_accounts = True
agg.save()
context = {"class": "success", "message": "Fetch requested"}
return render(request, self.template_name, context)
2023-03-08 12:48:05 +00:00
class ReqsList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/aggregator-info.html"
page_title = "Aggregator Info"
2023-03-08 15:44:21 +00:00
context_object_name_singular = "requisition"
context_object_name = "requisitions"
2023-03-08 12:48:05 +00:00
list_url_name = "reqs"
list_url_args = ["type", "pk"]
submit_url_name = "aggregator_countries"
submit_url_args = ["type", "pk"]
2023-03-08 15:44:21 +00:00
def get_context_data(self):
context = super().get_context_data()
2023-03-09 16:44:16 +00:00
pk = self.kwargs.get("pk")
context["pk"] = pk
self.extra_buttons = [
{
"url": reverse("bank_fetch", kwargs={"pk": pk}),
"action": "refresh",
"method": "get",
"label": "Fetch account details",
"icon": "fa-solid fa-refresh",
},
]
2023-03-08 15:44:21 +00:00
return context
2023-03-08 12:48:05 +00:00
def get_queryset(self, **kwargs):
pk = kwargs.get("pk")
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
except Aggregator.DoesNotExist:
context = {
2023-03-09 16:44:16 +00:00
"message": "Aggregator does not exist",
"class": "danger",
2023-03-08 12:48:05 +00:00
}
return self.render_to_response(context)
self.page_title = (
f"Requisitions for {aggregator.name} ({aggregator.get_service_display()})"
)
2023-03-09 16:44:16 +00:00
self.page_subtitle = f"Stored account details: {len(aggregator.currencies)}"
2023-03-08 12:48:05 +00:00
run = synchronize_async_helper(NordigenClient(aggregator))
reqs = synchronize_async_helper(run.get_requisitions())
return reqs
class AggregatorCountriesList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/aggregator-countries.html"
page_title = "List of countries"
list_url_name = "aggregator_countries"
list_url_args = ["type", "pk"]
context_object_name_singular = "country"
context_object_name = "countries"
def get_context_data(self):
context = super().get_context_data()
context["pk"] = self.kwargs.get("pk")
return context
def get_queryset(self, **kwargs):
pk = kwargs.get("pk")
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
except Aggregator.DoesNotExist:
context = {
2023-03-09 16:44:16 +00:00
"message": "Aggregator does not exist",
"class": "danger",
2023-03-08 12:48:05 +00:00
}
return self.render_to_response(context)
self.page_title = (
f"Countries for {aggregator.name} ({aggregator.get_service_display()})"
)
run = synchronize_async_helper(NordigenClient(aggregator))
countries = synchronize_async_helper(run.get_countries())
self.extra_args = {"pk": pk}
return countries
class AggregatorCountryBanksList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/aggregator-country-banks.html"
page_title = "List of banks"
list_url_name = "aggregator_country_banks"
list_url_args = ["type", "pk", "country"]
context_object_name_singular = "bank"
context_object_name = "banks"
def get_context_data(self):
context = super().get_context_data()
context["pk"] = self.kwargs.get("pk")
context["country"] = self.kwargs.get("country")
return context
def get_queryset(self, **kwargs):
pk = kwargs.get("pk")
country = kwargs.get("country")
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
except Aggregator.DoesNotExist:
context = {
2023-03-09 16:44:16 +00:00
"message": "Aggregator does not exist",
"class": "danger",
2023-03-08 12:48:05 +00:00
}
return self.render_to_response(context)
self.page_title = (
f"Banks for {aggregator.name} in {country} "
f"({aggregator.get_service_display()})"
)
run = synchronize_async_helper(NordigenClient(aggregator))
banks = synchronize_async_helper(run.get_banks(country))
return banks
class AggregatorLinkBank(LoginRequiredMixin, OTPRequiredMixin, View):
def get(self, request, *args, **kwargs):
pk = kwargs.get("pk")
bank = kwargs.get("bank")
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
except Aggregator.DoesNotExist:
context = {
2023-03-09 16:44:16 +00:00
"message": "Aggregator does not exist",
"class": "danger",
2023-03-08 12:48:05 +00:00
}
return self.render_to_response(context)
run = synchronize_async_helper(NordigenClient(aggregator))
auth_url = synchronize_async_helper(run.build_link(bank))
# Create a blank response
response = HttpResponse()
response["HX-Redirect"] = auth_url
return response
2023-03-08 15:44:21 +00:00
class ReqDelete(LoginRequiredMixin, OTPRequiredMixin, View):
def delete(self, request, *args, **kwargs):
pk = kwargs.get("pk")
req_id = kwargs.get("req_id")
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
except Aggregator.DoesNotExist:
context = {
2023-03-09 16:44:16 +00:00
"message": "Aggregator does not exist",
"class": "danger",
2023-03-08 15:44:21 +00:00
}
return self.render_to_response(context)
run = synchronize_async_helper(NordigenClient(aggregator))
synchronize_async_helper(run.delete_requisition(req_id))
response = HttpResponse(status=204)
response["HX-Trigger"] = "requisitionEvent"
return response
2023-03-08 17:04:47 +00:00
class ReqInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
context_object_name_singular = "requisition"
context_object_name = "requisitions"
detail_template = "partials/aggregator-req-info.html"
def get_object(self, **kwargs):
pk = kwargs.get("pk")
req_id = kwargs.get("req_id")
try:
aggregator = Aggregator.get_by_id(pk, self.request.user)
except Aggregator.DoesNotExist:
context = {
2023-03-09 16:44:16 +00:00
"message": "Aggregator does not exist",
"class": "danger",
2023-03-08 17:04:47 +00:00
}
return self.render_to_response(context)
run = synchronize_async_helper(NordigenClient(aggregator))
req = synchronize_async_helper(run.get_all_account_info(req_id))
self.extra_context = {"pretty": list(req.keys())}
return req
class AggregatorList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/aggregator-list.html"
model = Aggregator
page_title = "List of aggregator connections"
list_url_name = "aggregators"
list_url_args = ["type"]
submit_url_name = "aggregator_create"
class AggregatorCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Aggregator
form_class = AggregatorForm
submit_url_name = "aggregator_create"
class AggregatorUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Aggregator
form_class = AggregatorForm
submit_url_name = "aggregator_update"
class AggregatorDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Aggregator