180 lines
6.4 KiB
Python
180 lines
6.4 KiB
Python
from django.shortcuts import render
|
|
from rest_framework import viewsets, mixins
|
|
from .models import Account, BankAccount, Institution, Transaction, Slice, Rule, SubscriptionPlan
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.decorators import action
|
|
from connection.models import Connection, ConnectionType
|
|
from api.serializers import (AccountReadSerializer, AccountWriteSerializer,
|
|
BankAccountSerializer, BankAccountSerializerPOST,
|
|
InstitutionSerializer,
|
|
TransactionSerializer,
|
|
ConnectionSerializer,
|
|
ConnectionTypeSerializer,
|
|
SliceSerializer, SliceTransactionSerializer,
|
|
RuleSerializer, SubscriptionPlanSerializer)
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
|
from dj_rest_auth.registration.views import SocialLoginView
|
|
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
|
|
from dj_rest_auth.social_serializers import TwitterLoginSerializer
|
|
from api.mixins import ReadWriteSerializerMixin
|
|
|
|
class OwnedAccountsMixin():
|
|
|
|
def accessible_accounts(self):
|
|
usr = self.request.user
|
|
return usr.owned_accounts.all()\
|
|
.union(usr.admin_accounts.all())\
|
|
.union(usr.view_accounts.all())
|
|
|
|
class TwitterLogin(SocialLoginView):
|
|
serializer_class = TwitterLoginSerializer
|
|
adapter_class = TwitterOAuthAdapter
|
|
|
|
|
|
class FacebookLogin(SocialLoginView):
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
|
|
class AccountViewSet(ReadWriteSerializerMixin, viewsets.ModelViewSet, OwnedAccountsMixin):
|
|
"""API endpoint that allows accounts to be viewed or edited
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
queryset = Account.objects.all()
|
|
read_serializer_class = AccountReadSerializer
|
|
write_serializer_class = AccountWriteSerializer
|
|
|
|
def get_queryset(self):
|
|
return self.accessible_accounts()
|
|
|
|
|
|
class BankAccountViewSet(viewsets.ModelViewSet, OwnedAccountsMixin):
|
|
"""API endpoint that allows BankAccounts to be viewed or edited
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
queryset = BankAccount.objects.all()
|
|
# serializer_class = BankAccountSerializer
|
|
|
|
def get_serializer_class(self):
|
|
if self.action == 'create':
|
|
return BankAccountSerializerPOST
|
|
return BankAccountSerializer
|
|
|
|
def get_queryset(self):
|
|
return BankAccount.objects.filter(
|
|
qrtr_account__in=self.accessible_accounts().values_list('id'))
|
|
|
|
|
|
class SliceViewSet(viewsets.ModelViewSet, OwnedAccountsMixin):
|
|
"""API endpoint that allows BankAccounts to be viewed.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
queryset = Slice.objects.all()
|
|
serializer_class = SliceSerializer
|
|
|
|
filterset_fields = {
|
|
'id': ['exact', 'lte', 'gte'],
|
|
'name': ['exact',],
|
|
'balance': ['exact', 'lte', 'gte'],
|
|
'bank_acc': ['exact'],
|
|
# 'slice_of': ['exact']
|
|
}
|
|
|
|
def get_queryset(self):
|
|
return Slice.objects.select_related('bank_acc').filter(
|
|
bank_acc__qrtr_account__in=self.accessible_accounts().values_list('id')
|
|
)
|
|
|
|
class SubscriptionPlanViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = SubscriptionPlan.objects.all()
|
|
serializer_class = SubscriptionPlanSerializer
|
|
|
|
class InstitutionViewSet(viewsets.ReadOnlyModelViewSet):
|
|
"""API endpoint that allows BankAccounts to be viewed.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
queryset = Institution.objects.all()
|
|
serializer_class = InstitutionSerializer
|
|
|
|
|
|
class TransactionViewSet(viewsets.ModelViewSet, OwnedAccountsMixin):
|
|
"""API endpoint that allows BankAccounts to be viewed.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
queryset = Transaction.objects.filter(is_split=False)
|
|
serializer_class = TransactionSerializer
|
|
search_fields = ['name', 'slice__name', 'bank__nickname',
|
|
'bank__official_name']
|
|
|
|
filterset_fields = {
|
|
'slice__id': ['exact',],
|
|
'slice__name': ['exact', ],
|
|
'authorized_date': ['exact', 'lte', 'gte', 'isnull'],
|
|
'updated_at': ['exact', 'lte', 'gte', 'isnull'],
|
|
'created_at': ['exact', 'lte', 'gte', 'isnull'],
|
|
'trans_id': ['exact', 'lte', 'gte'],
|
|
'id': ['exact', 'lte', 'gte'],
|
|
'bank': ['exact']
|
|
}
|
|
|
|
@action(detail=True, methods=['post'], url_path='split')
|
|
def split_transaction(self, request, pk=None):
|
|
parent = self.get_object()
|
|
base_information = {"authorized_date": parent.authorized_date,
|
|
"bank": parent.bank,
|
|
"name": parent.name,
|
|
"details": parent.details,
|
|
"slice": parent.slice,
|
|
"trans_id": parent.trans_id,
|
|
"split_parent": parent
|
|
}
|
|
child1 = Transaction.objects.create(**base_information)
|
|
child1.name = f"{child1.name}.split1"
|
|
child2 = Transaction.objects.create(**base_information)
|
|
child2.name = f"{child1.name}.split2"
|
|
|
|
def get_queryset(self):
|
|
return Transaction.objects.select_related('bank').filter(
|
|
bank__qrtr_account__in=self.accessible_accounts().values_list('id')
|
|
).filter(is_split=False)
|
|
|
|
|
|
class SliceTransactionViewSet(viewsets.ModelViewSet, OwnedAccountsMixin):
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = SliceTransactionSerializer
|
|
queryset = Slice.objects.all()
|
|
|
|
filterset_fields = {
|
|
'id': ['exact', 'lte', 'gte'],
|
|
'name': ['exact',],
|
|
'balance': ['exact', 'lte', 'gte'],
|
|
'bank_acc': ['exact'],
|
|
# 'slice_of': ['exact']
|
|
}
|
|
|
|
def get_queryset(self):
|
|
return Slice.objects.select_related('bank_acc').filter(
|
|
bank_acc__qrtr_account__in=self.accessible_accounts().values_list('id')
|
|
)
|
|
|
|
# def get_queryset(self):
|
|
# return Transaction.objects.filter(slice__pk=self.kwargs.get('slice_pk'))
|
|
|
|
|
|
class RuleViewSet(viewsets.ReadOnlyModelViewSet, OwnedAccountsMixin):
|
|
"""API endpoint that allows BankAccounts to be viewed.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
queryset = Rule.objects.all()
|
|
serializer_class = RuleSerializer
|
|
|
|
def get_queryset(self):
|
|
return Rule.objects.select_related('bank_acc').filter(
|
|
bank_acc__qrtr_account__in=self.accessible_accounts().values_list('id')
|
|
) |