135 lines
3.9 KiB
Python
Executable File
135 lines
3.9 KiB
Python
Executable File
from django.contrib.auth.models import Group
|
|
from django.contrib.auth import get_user_model
|
|
from rest_framework import serializers
|
|
from qrtr_account.models import Account, Bank, Institution, Transaction, Slice, Rule
|
|
from connection.models import Connection, ConnectionType
|
|
|
|
|
|
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = get_user_model()
|
|
fields = ['url', 'username', 'email', 'groups', 'owned_accounts',
|
|
'admin_accounts', 'view_accounts']
|
|
|
|
|
|
class GroupSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Group
|
|
fields = ['url', 'name']
|
|
|
|
|
|
class AccountSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Account
|
|
fields = ['url', 'owner', 'name', 'admin_users', 'view_users']
|
|
|
|
|
|
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
|
|
fields = [
|
|
'url',
|
|
'qrtr_account',
|
|
'connection',
|
|
'institution',
|
|
'nickname',
|
|
'balance',
|
|
'ac_type',
|
|
'ac_subtype',
|
|
]
|
|
extra_kwargs = {
|
|
'balance': {'read_only': True},
|
|
'connection': {'read_only': True},
|
|
'institution': {'read_only': True},
|
|
'ac_type': {'read_only': True},
|
|
'ac_subtype': {'read_only': True}
|
|
}
|
|
|
|
|
|
class BankSerializerPOST(BankSerializer):
|
|
"""Separate Serializer for POST requests to create a new bank. This adds
|
|
a new field called connection_details that is used to create a new
|
|
connection record to go with the new Bank. This field is only allowed on
|
|
POST because we don't want to expose this information to the user, or allow
|
|
them to change it b/c that could lead to an integrity problem, breaking
|
|
their bank functionality.
|
|
"""
|
|
# connection_details = serializers.JSONField(
|
|
# write_only=True,
|
|
# required=True,
|
|
# initial={
|
|
# "type": f"{' OR '.join(ConnectionType.objects.all().values_list('name', flat=True))}",
|
|
# "credentials": {}})
|
|
|
|
class Meta:
|
|
model = Bank
|
|
fields = [
|
|
'url',
|
|
'qrtr_account',
|
|
'connection',
|
|
'institution',
|
|
'nickname',
|
|
'balance',
|
|
'ac_type',
|
|
'ac_subtype',
|
|
# 'connection_details'
|
|
]
|
|
extra_kwargs = {
|
|
'balance': {'read_only': True},
|
|
# 'connection': {'read_only': True},
|
|
'institution': {'read_only': True},
|
|
'ac_type': {'read_only': True},
|
|
'ac_subtype': {'read_only': True}
|
|
}
|
|
|
|
|
|
class InstitutionSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Institution
|
|
fields = ['url', 'name']
|
|
|
|
|
|
class TransactionSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Transaction
|
|
fields = ['url', 'datetime', 'Bank', 'details']
|
|
|
|
|
|
class SliceSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Slice
|
|
fields = ['url', 'name', 'icon', 'budget', 'slice_of']
|
|
|
|
|
|
class RuleSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Rule
|
|
fields = [
|
|
'url',
|
|
'name',
|
|
'kind',
|
|
'when_to_run',
|
|
'amount',
|
|
'source',
|
|
'destination']
|