mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-23 09:08:39 +02:00
feat: add api
This commit is contained in:
3
app/apps/api/serializers/__init__.py
Normal file
3
app/apps/api/serializers/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .transactions import *
|
||||
from .accounts import *
|
||||
from .currencies import *
|
||||
55
app/apps/api/serializers/accounts.py
Normal file
55
app/apps/api/serializers/accounts.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.api.serializers.currencies import CurrencySerializer
|
||||
from apps.accounts.models import AccountGroup, Account
|
||||
from apps.currencies.models import Currency
|
||||
|
||||
|
||||
class AccountGroupSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = AccountGroup
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class AccountSerializer(serializers.ModelSerializer):
|
||||
group = AccountGroupSerializer(read_only=True)
|
||||
group_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=AccountGroup.objects.all(),
|
||||
source="group",
|
||||
write_only=True,
|
||||
allow_null=True,
|
||||
)
|
||||
currency = CurrencySerializer(read_only=True)
|
||||
currency_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Currency.objects.all(), source="currency", write_only=True
|
||||
)
|
||||
exchange_currency = CurrencySerializer(read_only=True)
|
||||
exchange_currency_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Currency.objects.all(),
|
||||
source="exchange_currency",
|
||||
write_only=True,
|
||||
allow_null=True,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Account
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"group",
|
||||
"group_id",
|
||||
"currency",
|
||||
"currency_id",
|
||||
"exchange_currency",
|
||||
"exchange_currency_id",
|
||||
"is_asset",
|
||||
]
|
||||
|
||||
def create(self, validated_data):
|
||||
return Account.objects.create(**validated_data)
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
for attr, value in validated_data.items():
|
||||
setattr(instance, attr, value)
|
||||
instance.save()
|
||||
return instance
|
||||
29
app/apps/api/serializers/currencies.py
Normal file
29
app/apps/api/serializers/currencies.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from rest_framework import serializers
|
||||
from apps.currencies.models import Currency, ExchangeRate
|
||||
|
||||
|
||||
class CurrencySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Currency
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class ExchangeRateSerializer(serializers.ModelSerializer):
|
||||
# For read operations (GET)
|
||||
from_currency = CurrencySerializer(read_only=True)
|
||||
|
||||
# For write operations (POST, PUT, PATCH)
|
||||
from_currency_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Currency.objects.all(), source="from_currency", write_only=True
|
||||
)
|
||||
|
||||
to_currency = CurrencySerializer(read_only=True)
|
||||
|
||||
# For write operations (POST, PUT, PATCH)
|
||||
to_currency_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Currency.objects.all(), source="from_currency", write_only=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = ExchangeRate
|
||||
fields = "__all__"
|
||||
60
app/apps/api/serializers/transactions.py
Normal file
60
app/apps/api/serializers/transactions.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from rest_framework import serializers
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from apps.accounts.models import Account
|
||||
from apps.api.serializers.accounts import AccountSerializer
|
||||
from apps.transactions.models import (
|
||||
Transaction,
|
||||
TransactionCategory,
|
||||
TransactionTag,
|
||||
InstallmentPlan,
|
||||
)
|
||||
|
||||
|
||||
# Create serializers for other related models as needed
|
||||
class TransactionCategorySerializer(serializers.ModelSerializer):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = TransactionCategory
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class TransactionTagSerializer(serializers.ModelSerializer):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = TransactionTag
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class InstallmentPlanSerializer(serializers.ModelSerializer):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = InstallmentPlan
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class TransactionSerializer(serializers.ModelSerializer):
|
||||
category_name = serializers.CharField(source="category.name", read_only=True)
|
||||
tags = TransactionTagSerializer(many=True, read_only=True)
|
||||
exchanged_amount = serializers.SerializerMethodField()
|
||||
|
||||
# For read operations (GET)
|
||||
account = AccountSerializer(read_only=True)
|
||||
|
||||
# For write operations (POST, PUT, PATCH)
|
||||
account_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Account.objects.all(), source="account", write_only=True
|
||||
)
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = Transaction
|
||||
fields = "__all__"
|
||||
|
||||
@staticmethod
|
||||
def get_exchanged_amount(obj):
|
||||
return obj.exchanged_amount()
|
||||
Reference in New Issue
Block a user