mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-27 10:52:58 +01:00
fix(api): save income categories, tags and entities on account create and update
AccountSerializer assigned the many-to-many fields with setattr, which Django rejects, so any write that included them returned a 500. The default ModelSerializer create/update already handles them.
This commit is contained in:
1 parent
58e2aa3c8a
commit
448e9c8f5c
2 files changed
+38
-10
No files matched your search
@@ -97,15 +97,6 @@ class AccountSerializer(serializers.ModelSerializer):
|
||||
self.fields["group_id"].queryset = AccountGroup.objects.all()
|
||||
self.fields["investment_class_id"].queryset = InvestmentClass.objects.all()
|
||||
|
||||
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
|
||||
|
||||
|
||||
class AccountBalanceSerializer(serializers.Serializer):
|
||||
"""Serializer for account balance response."""
|
||||
|
||||
@@ -8,7 +8,7 @@ from rest_framework.test import APIClient
|
||||
|
||||
from apps.accounts.models import Account, AccountGroup
|
||||
from apps.currencies.models import Currency
|
||||
from apps.transactions.models import Transaction
|
||||
from apps.transactions.models import Transaction, TransactionCategory
|
||||
|
||||
|
||||
@override_settings(
|
||||
@@ -97,3 +97,40 @@ class AccountBalanceAPITests(TestCase):
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
def test_patch_and_create_with_income_categories(self):
|
||||
category = TransactionCategory.objects.create(name="Interest", owner=self.user)
|
||||
self.account.owner = self.user
|
||||
self.account.save()
|
||||
# Writes need Django model permissions
|
||||
self.user.is_superuser = True
|
||||
self.user.save()
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/accounts/{self.account.id}/",
|
||||
{"income_categories": [category.id]},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
|
||||
self.assertEqual(response.data["income_categories"], [category.id])
|
||||
# The join table: the related manager is per-user and empty without one
|
||||
self.assertTrue(
|
||||
Account.income_categories.through.objects.filter(
|
||||
account=self.account, transactioncategory=category
|
||||
).exists()
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
"/api/accounts/",
|
||||
{
|
||||
"name": "Broker",
|
||||
"group_id": None,
|
||||
"currency_id": self.currency.id,
|
||||
"exchange_currency_id": None,
|
||||
"is_investment": True,
|
||||
"income_categories": [category.id],
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
|
||||
self.assertEqual(response.data["income_categories"], [category.id])
|
||||
Reference in new issue
Block a user