import asyncio from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse from django.views import View from mixins.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate from two_factor.views.mixins import OTPRequiredMixin from core.clients.aggregators.nordigen import NordigenClient from core.forms import AggregatorForm from core.models import Aggregator from core.util import logs log = logs.get_logger(__name__) 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" context_object_name_singular = "requisition" context_object_name = "requisitions" list_url_name = "reqs" list_url_args = ["type", "pk"] submit_url_name = "aggregator_countries" submit_url_args = ["type", "pk"] 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"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)) # Redirect to auth url print("AUTH URL", auth_url) # Create a blank response response = HttpResponse() response["HX-Redirect"] = auth_url # return redirect(auth_url) return response 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 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