diff --git a/api/serializers.py b/api/serializers.py index 0d0e8c3..7abbb74 100755 --- a/api/serializers.py +++ b/api/serializers.py @@ -4,6 +4,7 @@ from rest_framework import serializers from qrtr_account.models import Account, Bank, Institution, Transaction, Slice, Rule from user.models import User from connection.models import Connection, ConnectionType +from connection.serializers import ConnectionTypeSerializer, ConnectionSerializer class UserAccountSerializer(serializers.HyperlinkedModelSerializer): @@ -44,26 +45,6 @@ class GroupSerializer(serializers.HyperlinkedModelSerializer): fields = ['url', 'name'] -class ConnectionTypeSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ConnectionType - fields = ['url', 'name', 'filename'] - extra_kwargs = { - 'name': {'read_only': True}, - 'filename': {'read_only': True} - } - - -class ConnectionSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = Connection - fields = ['url', 'name', 'type', 'credentials'] - extra_kwargs = { - 'type': {'write_only': True}, - 'credentials': {'write_only': True} - } - - class BankSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Bank diff --git a/connection/connections/plaid_client.py b/connection/connections/plaid_client.py index 1f56167..1da02d3 100755 --- a/connection/connections/plaid_client.py +++ b/connection/connections/plaid_client.py @@ -47,10 +47,15 @@ class Connection(AbstractConnectionClient): auth_token = self.credentials.get('auth_token') if not auth_token and public_key: print("Getting Auth Token From Public Key") - auth_token = self.get_auth_token(public_key) + authorization = self.get_auth_token(public_key) + print("AUTHORIZATION:") + print(authorization) + auth_token = authorization.get("access_token") + item_id = authorization.get("item_id") if "error" in auth_token: raise ValueError(f"Unable to generate Auth Token, {auth_token}") self.credentials['auth_token'] = auth_token + self.credentials['item_id'] = item_id print("Plaid Connection successful") print(self.credentials) @@ -63,7 +68,8 @@ class Connection(AbstractConnectionClient): print(e) return format_error(e) access_token = exchange_response['access_token'] - return access_token + item_id = exchange_response['item_id'] + return {"access_token":access_token, "item_id":item_id} def get_accounts(self, auth_token=None): if not auth_token: diff --git a/connection/serializers.py b/connection/serializers.py index d8115c8..dc05c06 100755 --- a/connection/serializers.py +++ b/connection/serializers.py @@ -1,5 +1,15 @@ -from .models import Connection from rest_framework import serializers +from .models import ConnectionType, Connection + + +class ConnectionTypeSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = ConnectionType + fields = ['url', 'name', 'filename'] + extra_kwargs = { + 'name': {'read_only': True}, + 'filename': {'read_only': True} + } class ConnectionSerializer(serializers.HyperlinkedModelSerializer): diff --git a/connection/urls.py b/connection/urls.py index 37f0a59..d8ee82c 100755 --- a/connection/urls.py +++ b/connection/urls.py @@ -1,6 +1,9 @@ from rest_framework import routers from .views import ConnectionViewSet +from django.urls import include, path ROUTER = routers.SimpleRouter() ROUTER.register(r'', ConnectionViewSet) -urlpatterns = ROUTER.urls +urlpatterns = [ + path('', include(ROUTER.urls)), +] diff --git a/connection/views.py b/connection/views.py index 027b9cc..63a03bc 100644 --- a/connection/views.py +++ b/connection/views.py @@ -9,10 +9,17 @@ from qrtr_account.models import Account from rest_framework.permissions import IsAuthenticated import importlib import json +from .serializers import ConnectionSerializer, ConnectionTypeSerializer +from django.db import transaction # Create your views here. +class ConnectionTypeViewSet(viewsets.ModelViewSet): + queryset = ConnectionType.objects.all() + serializer_class = ConnectionTypeSerializer + + class ConnectionViewSet(viewsets.ModelViewSet): """API endpoint that allows connections to be seen or created """ @@ -66,12 +73,13 @@ class ConnectionViewSet(viewsets.ModelViewSet): except Exception: return Response(status=status.HTTP_500, data="ERROR: Unable to contact Plaid") - conn, created = Connection.objects \ - .get_or_create(name=name, type=conn_type, - defaults={ - "credentials": request.data, - "account": account - }) - conn.credentials = plaid_client.credentials - conn.save() + with transaction.atomic(): + conn, created = Connection.objects \ + .get_or_create(name=name, type=conn_type, + defaults={ + "credentials": request.data, + "account": account + }) + conn.credentials = plaid_client.credentials + conn.save() return Response(plaid_client.get_accounts()) \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 435911d..798196c 100644 --- a/core/urls.py +++ b/core/urls.py @@ -31,10 +31,10 @@ from qrtr_account.views import (AccountViewSet, InstitutionViewSet, TransactionViewSet, SliceViewSet, - ConnectionViewSet, - ConnectionTypeViewSet, FacebookLogin, TwitterLogin) + +from connection.views import ConnectionViewSet, ConnectionTypeViewSet router = routers.DefaultRouter() diff --git a/qrtr_account/serializers.py b/qrtr_account/serializers.py new file mode 100644 index 0000000..e69de29 diff --git a/qrtr_account/views.py b/qrtr_account/views.py index 4001520..c6f6322 100644 --- a/qrtr_account/views.py +++ b/qrtr_account/views.py @@ -60,24 +60,6 @@ class TransactionViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = TransactionSerializer -class ConnectionTypeViewSet(viewsets.ModelViewSet): - queryset = ConnectionType.objects.all() - serializer_class = ConnectionTypeSerializer - - -class ConnectionViewSet(viewsets.ModelViewSet): - """API endpoint that allows connections to be seen or created - """ - queryset = Connection.objects.all() - serializer_class = ConnectionSerializer - # Make connections somewhat immutable from the users perspective - http_method_names = [ - 'get', - 'post', - 'delete', - 'options'] - - class SliceViewSet(viewsets.ReadOnlyModelViewSet): """API endpoint that allows Banks to be viewed. """