mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-02-25 17:04:51 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10a0ac42a2 | ||
|
|
1b47c12a22 | ||
|
|
091f73bf8d | ||
|
|
73fe17de64 | ||
|
|
52af1b2260 | ||
|
|
8efa087aee |
@@ -1,3 +1,4 @@
|
||||
from .transactions import *
|
||||
from .accounts import *
|
||||
from .currencies import *
|
||||
from .dca import *
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from rest_framework import serializers
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from apps.api.serializers.currencies import CurrencySerializer
|
||||
from apps.accounts.models import AccountGroup, Account
|
||||
@@ -6,6 +7,8 @@ from apps.currencies.models import Currency
|
||||
|
||||
|
||||
class AccountGroupSerializer(serializers.ModelSerializer):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = AccountGroup
|
||||
fields = "__all__"
|
||||
@@ -31,6 +34,8 @@ class AccountSerializer(serializers.ModelSerializer):
|
||||
allow_null=True,
|
||||
)
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = Account
|
||||
fields = [
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
from rest_framework import serializers
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from apps.currencies.models import Currency, ExchangeRate
|
||||
|
||||
|
||||
class CurrencySerializer(serializers.ModelSerializer):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = Currency
|
||||
fields = "__all__"
|
||||
@@ -24,6 +28,8 @@ class ExchangeRateSerializer(serializers.ModelSerializer):
|
||||
queryset=Currency.objects.all(), source="to_currency", write_only=True
|
||||
)
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = ExchangeRate
|
||||
fields = "__all__"
|
||||
|
||||
85
app/apps/api/serializers/dca.py
Normal file
85
app/apps/api/serializers/dca.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from rest_framework import serializers
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from apps.dca.models import DCAEntry, DCAStrategy
|
||||
|
||||
|
||||
class DCAEntrySerializer(serializers.ModelSerializer):
|
||||
profit_loss = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
profit_loss_percentage = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
current_value = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
entry_price = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = DCAEntry
|
||||
fields = [
|
||||
"id",
|
||||
"strategy",
|
||||
"date",
|
||||
"amount_paid",
|
||||
"amount_received",
|
||||
"notes",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"profit_loss",
|
||||
"profit_loss_percentage",
|
||||
"current_value",
|
||||
"entry_price",
|
||||
]
|
||||
read_only_fields = ["created_at", "updated_at"]
|
||||
|
||||
|
||||
class DCAStrategySerializer(serializers.ModelSerializer):
|
||||
entries = DCAEntrySerializer(many=True, read_only=True)
|
||||
total_invested = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
total_received = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
average_entry_price = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
total_entries = serializers.IntegerField(read_only=True)
|
||||
current_total_value = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
total_profit_loss = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
total_profit_loss_percentage = serializers.DecimalField(
|
||||
max_digits=42, decimal_places=30, read_only=True
|
||||
)
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = DCAStrategy
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"target_currency",
|
||||
"payment_currency",
|
||||
"notes",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"entries",
|
||||
"total_invested",
|
||||
"total_received",
|
||||
"average_entry_price",
|
||||
"total_entries",
|
||||
"current_total_value",
|
||||
"total_profit_loss",
|
||||
"total_profit_loss_percentage",
|
||||
]
|
||||
read_only_fields = ["created_at", "updated_at"]
|
||||
@@ -19,6 +19,7 @@ from apps.transactions.models import (
|
||||
TransactionTag,
|
||||
InstallmentPlan,
|
||||
TransactionEntity,
|
||||
RecurringTransaction,
|
||||
)
|
||||
|
||||
|
||||
@@ -47,11 +48,77 @@ class TransactionEntitySerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class InstallmentPlanSerializer(serializers.ModelSerializer):
|
||||
category = TransactionCategoryField(required=False)
|
||||
tags = TransactionTagField(required=False)
|
||||
entities = TransactionEntityField(required=False)
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
class Meta:
|
||||
model = InstallmentPlan
|
||||
fields = "__all__"
|
||||
fields = [
|
||||
"id",
|
||||
"account",
|
||||
"type",
|
||||
"description",
|
||||
"number_of_installments",
|
||||
"installment_start",
|
||||
"installment_total_number",
|
||||
"start_date",
|
||||
"reference_date",
|
||||
"end_date",
|
||||
"recurrence",
|
||||
"installment_amount",
|
||||
"category",
|
||||
"tags",
|
||||
"entities",
|
||||
"notes",
|
||||
]
|
||||
read_only_fields = ["installment_total_number", "end_date"]
|
||||
|
||||
def create(self, validated_data):
|
||||
instance = super().create(validated_data)
|
||||
instance.create_transactions()
|
||||
return instance
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
instance = super().update(instance, validated_data)
|
||||
instance.update_transactions()
|
||||
return instance
|
||||
|
||||
|
||||
class RecurringTransactionSerializer(serializers.ModelSerializer):
|
||||
category = TransactionCategoryField(required=False)
|
||||
tags = TransactionTagField(required=False)
|
||||
entities = TransactionEntityField(required=False)
|
||||
|
||||
class Meta:
|
||||
model = RecurringTransaction
|
||||
fields = [
|
||||
"id",
|
||||
"is_paused",
|
||||
"account",
|
||||
"type",
|
||||
"amount",
|
||||
"description",
|
||||
"category",
|
||||
"tags",
|
||||
"entities",
|
||||
"notes",
|
||||
"reference_date",
|
||||
"start_date",
|
||||
"end_date",
|
||||
"recurrence_type",
|
||||
"recurrence_interval",
|
||||
"last_generated_date",
|
||||
"last_generated_reference_date",
|
||||
]
|
||||
read_only_fields = ["last_generated_date", "last_generated_reference_date"]
|
||||
|
||||
def create(self, validated_data):
|
||||
instance = super().create(validated_data)
|
||||
instance.create_upcoming_transactions()
|
||||
return instance
|
||||
|
||||
|
||||
class TransactionSerializer(serializers.ModelSerializer):
|
||||
|
||||
@@ -9,10 +9,13 @@ router.register(r"categories", views.TransactionCategoryViewSet)
|
||||
router.register(r"tags", views.TransactionTagViewSet)
|
||||
router.register(r"entities", views.TransactionEntityViewSet)
|
||||
router.register(r"installment-plans", views.InstallmentPlanViewSet)
|
||||
router.register(r"recurring-transactions", views.RecurringTransactionViewSet)
|
||||
router.register(r"account-groups", views.AccountGroupViewSet)
|
||||
router.register(r"accounts", views.AccountViewSet)
|
||||
router.register(r"currencies", views.CurrencyViewSet)
|
||||
router.register(r"exchange-rates", views.ExchangeRateViewSet)
|
||||
router.register(r"dca/strategies", views.DCAStrategyViewSet)
|
||||
router.register(r"dca/entries", views.DCAEntryViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
path("", include(router.urls)),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from .transactions import *
|
||||
from .accounts import *
|
||||
from .currencies import *
|
||||
from .dca import *
|
||||
|
||||
41
app/apps/api/views/dca.py
Normal file
41
app/apps/api/views/dca.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from apps.dca.models import DCAStrategy, DCAEntry
|
||||
from apps.api.serializers import DCAStrategySerializer, DCAEntrySerializer
|
||||
|
||||
|
||||
class DCAStrategyViewSet(viewsets.ModelViewSet):
|
||||
queryset = DCAStrategy.objects.all()
|
||||
serializer_class = DCAStrategySerializer
|
||||
|
||||
@action(detail=True, methods=["get"])
|
||||
def investment_frequency(self, request, pk=None):
|
||||
strategy = self.get_object()
|
||||
return Response(strategy.investment_frequency_data())
|
||||
|
||||
@action(detail=True, methods=["get"])
|
||||
def price_comparison(self, request, pk=None):
|
||||
strategy = self.get_object()
|
||||
return Response(strategy.price_comparison_data())
|
||||
|
||||
@action(detail=True, methods=["get"])
|
||||
def current_price(self, request, pk=None):
|
||||
strategy = self.get_object()
|
||||
price_data = strategy.current_price()
|
||||
if price_data:
|
||||
price, date = price_data
|
||||
return Response({"price": price, "date": date})
|
||||
return Response({"price": None, "date": None})
|
||||
|
||||
|
||||
class DCAEntryViewSet(viewsets.ModelViewSet):
|
||||
queryset = DCAEntry.objects.all()
|
||||
serializer_class = DCAEntrySerializer
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = DCAEntry.objects.all()
|
||||
strategy_id = self.request.query_params.get("strategy", None)
|
||||
if strategy_id is not None:
|
||||
queryset = queryset.filter(strategy_id=strategy_id)
|
||||
return queryset
|
||||
@@ -1,4 +1,4 @@
|
||||
from rest_framework import permissions, viewsets
|
||||
from rest_framework import viewsets
|
||||
|
||||
from apps.api.serializers import (
|
||||
TransactionSerializer,
|
||||
@@ -6,6 +6,7 @@ from apps.api.serializers import (
|
||||
TransactionTagSerializer,
|
||||
InstallmentPlanSerializer,
|
||||
TransactionEntitySerializer,
|
||||
RecurringTransactionSerializer,
|
||||
)
|
||||
from apps.transactions.models import (
|
||||
Transaction,
|
||||
@@ -13,6 +14,7 @@ from apps.transactions.models import (
|
||||
TransactionTag,
|
||||
InstallmentPlan,
|
||||
TransactionEntity,
|
||||
RecurringTransaction,
|
||||
)
|
||||
from apps.rules.signals import transaction_updated, transaction_created
|
||||
|
||||
@@ -53,10 +55,7 @@ class InstallmentPlanViewSet(viewsets.ModelViewSet):
|
||||
queryset = InstallmentPlan.objects.all()
|
||||
serializer_class = InstallmentPlanSerializer
|
||||
|
||||
def perform_create(self, serializer):
|
||||
instance = serializer.save()
|
||||
instance.create_transactions()
|
||||
|
||||
def perform_update(self, serializer):
|
||||
instance = serializer.save()
|
||||
instance.create_transactions()
|
||||
class RecurringTransactionViewSet(viewsets.ModelViewSet):
|
||||
queryset = RecurringTransaction.objects.all()
|
||||
serializer_class = RecurringTransactionSerializer
|
||||
|
||||
Reference in New Issue
Block a user