2023-03-08 12:48:05 +00:00
|
|
|
import asyncio
|
|
|
|
|
2023-03-07 16:59:39 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2023-03-08 12:48:05 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.views import View
|
2023-03-08 17:04:47 +00:00
|
|
|
from mixins.views import (
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectDelete,
|
|
|
|
ObjectList,
|
|
|
|
ObjectRead,
|
|
|
|
ObjectUpdate,
|
|
|
|
)
|
2023-03-07 16:59:39 +00:00
|
|
|
from two_factor.views.mixins import OTPRequiredMixin
|
|
|
|
|
2023-03-08 12:48:05 +00:00
|
|
|
from core.clients.aggregators.nordigen import NordigenClient
|
2023-03-07 16:59:39 +00:00
|
|
|
from core.forms import AggregatorForm
|
|
|
|
from core.models import Aggregator
|
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
|
|
|
2023-03-08 12:48:05 +00:00
|
|
|
def synchronize_async_helper(to_await):
|
|
|
|
async_response = []
|
|
|
|
|
|
|
|
async def run_and_capture_result():
|
|
|
|
r = await to_await
|
|
|
|
async_response.append(r)
|
|
|
|
|
|
|
|
try:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
coroutine = run_and_capture_result()
|
|
|
|
loop.run_until_complete(coroutine)
|
|
|
|
return async_response[0]
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
context["pk"] = self.kwargs.get("pk")
|
|
|
|
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:
|
|
|
|
message = "Aggregator does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
self.page_title = (
|
|
|
|
f"Requisitions for {aggregator.name} ({aggregator.get_service_display()})"
|
|
|
|
)
|
|
|
|
|
|
|
|
run = synchronize_async_helper(NordigenClient(aggregator))
|
|
|
|
reqs = synchronize_async_helper(run.get_requisitions())
|
|
|
|
print("REQS", reqs)
|
|
|
|
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:
|
|
|
|
message = "Aggregator does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
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())
|
|
|
|
print("COUNTRIES", 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:
|
|
|
|
message = "Aggregator does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
print("BANKS", banks)
|
|
|
|
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:
|
|
|
|
message = "Aggregator does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
message = "Aggregator does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
message = "Aggregator does not exist"
|
|
|
|
message_class = "danger"
|
|
|
|
context = {
|
|
|
|
"message": message,
|
|
|
|
"message_class": message_class,
|
|
|
|
"window_content": self.window_content,
|
|
|
|
}
|
|
|
|
return self.render_to_response(context)
|
|
|
|
run = synchronize_async_helper(NordigenClient(aggregator))
|
|
|
|
req = synchronize_async_helper(run.get_all_account_info(req_id))
|
|
|
|
print("REQ KEYS", req.keys())
|
|
|
|
self.extra_context = {"pretty": list(req.keys())}
|
|
|
|
return req
|
|
|
|
|
|
|
|
|
2023-03-07 16:59:39 +00:00
|
|
|
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
|