64 lines
2.5 KiB
Python
64 lines
2.5 KiB
Python
from django.contrib.auth.models import Group
|
|
from django.contrib.auth import get_user_model
|
|
from rest_framework import viewsets
|
|
from api.serializers import UserSerializer, GroupSerializer
|
|
from allauth.account.views import ConfirmEmailView
|
|
|
|
from django.shortcuts import redirect, render
|
|
from django.http import Http404
|
|
from django.views.generic.base import TemplateView
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import action
|
|
|
|
import importlib
|
|
from collections import defaultdict
|
|
|
|
class UserViewSet(viewsets.ModelViewSet):
|
|
"""
|
|
API endpoint that allows users to be viewed or edited.
|
|
"""
|
|
queryset = get_user_model().objects.all().order_by('-date_joined')
|
|
serializer_class = UserSerializer
|
|
|
|
@action(detail=False, methods=['get'], url_path="me")
|
|
def me(self, request, pk=None):
|
|
user = UserSerializer(request.user, context={'request': request})
|
|
return Response(user.data)
|
|
|
|
@action(detail=False, methods=['get'], url_path='me/list-connections')
|
|
def get_accounts(self,request):
|
|
print("GETTING ACCOUNTS!")
|
|
print(request.user)
|
|
avail_conns = defaultdict(list)
|
|
user_qrtr_accounts = request.user.owned_accounts.all() | \
|
|
request.user.admin_accounts.all() | \
|
|
request.user.view_accounts.all()
|
|
for qrtr_account in user_qrtr_accounts.distinct():
|
|
print(f"Getting Connections for {qrtr_account}")
|
|
connections = qrtr_account.connection_set.all()
|
|
print(connections)
|
|
for connection in connections:
|
|
print("Getting items from plaid for given account")
|
|
print(connection)
|
|
conn_name = connection.type.name
|
|
conn_accs = []
|
|
client_lib = importlib.import_module(f"connection.connections.{connection.type.filename}")
|
|
print(type(connection.credentials))
|
|
client = client_lib.Connection(connection.credentials)
|
|
connection.credentials = client.credentials
|
|
connection.save()
|
|
conn_accs.append(client.get_accounts())
|
|
avail_conns[conn_name].extend(conn_accs)
|
|
return Response(avail_conns)
|
|
|
|
|
|
class GroupViewSet(viewsets.ReadOnlyModelViewSet):
|
|
"""
|
|
API endpoint that allows groups to be viewed or edited.
|
|
"""
|
|
queryset = Group.objects.all()
|
|
serializer_class = GroupSerializer
|
|
|
|
|
|
class ConfirmEmailSuccessView(TemplateView):
|
|
template_name = 'confirm_email.html' |