[QRTR-88] Updated plaid_client.py to pull and store the item_id; Moved serializers and viewsets to the connection app as well.
This commit is contained in:
parent
1e80dfd7fd
commit
e49a547dae
@ -4,6 +4,7 @@ from rest_framework import serializers
|
|||||||
from qrtr_account.models import Account, Bank, Institution, Transaction, Slice, Rule
|
from qrtr_account.models import Account, Bank, Institution, Transaction, Slice, Rule
|
||||||
from user.models import User
|
from user.models import User
|
||||||
from connection.models import Connection, ConnectionType
|
from connection.models import Connection, ConnectionType
|
||||||
|
from connection.serializers import ConnectionTypeSerializer, ConnectionSerializer
|
||||||
|
|
||||||
|
|
||||||
class UserAccountSerializer(serializers.HyperlinkedModelSerializer):
|
class UserAccountSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
@ -44,26 +45,6 @@ class GroupSerializer(serializers.HyperlinkedModelSerializer):
|
|||||||
fields = ['url', 'name']
|
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 BankSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Bank
|
model = Bank
|
||||||
|
|||||||
@ -47,10 +47,15 @@ class Connection(AbstractConnectionClient):
|
|||||||
auth_token = self.credentials.get('auth_token')
|
auth_token = self.credentials.get('auth_token')
|
||||||
if not auth_token and public_key:
|
if not auth_token and public_key:
|
||||||
print("Getting Auth Token From 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:
|
if "error" in auth_token:
|
||||||
raise ValueError(f"Unable to generate Auth Token, {auth_token}")
|
raise ValueError(f"Unable to generate Auth Token, {auth_token}")
|
||||||
self.credentials['auth_token'] = auth_token
|
self.credentials['auth_token'] = auth_token
|
||||||
|
self.credentials['item_id'] = item_id
|
||||||
print("Plaid Connection successful")
|
print("Plaid Connection successful")
|
||||||
print(self.credentials)
|
print(self.credentials)
|
||||||
|
|
||||||
@ -63,7 +68,8 @@ class Connection(AbstractConnectionClient):
|
|||||||
print(e)
|
print(e)
|
||||||
return format_error(e)
|
return format_error(e)
|
||||||
access_token = exchange_response['access_token']
|
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):
|
def get_accounts(self, auth_token=None):
|
||||||
if not auth_token:
|
if not auth_token:
|
||||||
|
|||||||
@ -1,5 +1,15 @@
|
|||||||
from .models import Connection
|
|
||||||
from rest_framework import serializers
|
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):
|
class ConnectionSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
from .views import ConnectionViewSet
|
from .views import ConnectionViewSet
|
||||||
|
from django.urls import include, path
|
||||||
|
|
||||||
ROUTER = routers.SimpleRouter()
|
ROUTER = routers.SimpleRouter()
|
||||||
ROUTER.register(r'', ConnectionViewSet)
|
ROUTER.register(r'', ConnectionViewSet)
|
||||||
urlpatterns = ROUTER.urls
|
urlpatterns = [
|
||||||
|
path('', include(ROUTER.urls)),
|
||||||
|
]
|
||||||
|
|||||||
@ -9,10 +9,17 @@ from qrtr_account.models import Account
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
import importlib
|
import importlib
|
||||||
import json
|
import json
|
||||||
|
from .serializers import ConnectionSerializer, ConnectionTypeSerializer
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectionTypeViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = ConnectionType.objects.all()
|
||||||
|
serializer_class = ConnectionTypeSerializer
|
||||||
|
|
||||||
|
|
||||||
class ConnectionViewSet(viewsets.ModelViewSet):
|
class ConnectionViewSet(viewsets.ModelViewSet):
|
||||||
"""API endpoint that allows connections to be seen or created
|
"""API endpoint that allows connections to be seen or created
|
||||||
"""
|
"""
|
||||||
@ -66,12 +73,13 @@ class ConnectionViewSet(viewsets.ModelViewSet):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return Response(status=status.HTTP_500,
|
return Response(status=status.HTTP_500,
|
||||||
data="ERROR: Unable to contact Plaid")
|
data="ERROR: Unable to contact Plaid")
|
||||||
conn, created = Connection.objects \
|
with transaction.atomic():
|
||||||
.get_or_create(name=name, type=conn_type,
|
conn, created = Connection.objects \
|
||||||
defaults={
|
.get_or_create(name=name, type=conn_type,
|
||||||
"credentials": request.data,
|
defaults={
|
||||||
"account": account
|
"credentials": request.data,
|
||||||
})
|
"account": account
|
||||||
conn.credentials = plaid_client.credentials
|
})
|
||||||
conn.save()
|
conn.credentials = plaid_client.credentials
|
||||||
|
conn.save()
|
||||||
return Response(plaid_client.get_accounts())
|
return Response(plaid_client.get_accounts())
|
||||||
@ -31,10 +31,10 @@ from qrtr_account.views import (AccountViewSet,
|
|||||||
InstitutionViewSet,
|
InstitutionViewSet,
|
||||||
TransactionViewSet,
|
TransactionViewSet,
|
||||||
SliceViewSet,
|
SliceViewSet,
|
||||||
ConnectionViewSet,
|
|
||||||
ConnectionTypeViewSet,
|
|
||||||
FacebookLogin,
|
FacebookLogin,
|
||||||
TwitterLogin)
|
TwitterLogin)
|
||||||
|
|
||||||
|
from connection.views import ConnectionViewSet, ConnectionTypeViewSet
|
||||||
|
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
|
|||||||
0
qrtr_account/serializers.py
Normal file
0
qrtr_account/serializers.py
Normal file
@ -60,24 +60,6 @@ class TransactionViewSet(viewsets.ReadOnlyModelViewSet):
|
|||||||
serializer_class = TransactionSerializer
|
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):
|
class SliceViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
"""API endpoint that allows Banks to be viewed.
|
"""API endpoint that allows Banks to be viewed.
|
||||||
"""
|
"""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user