kfet -- Create initial statement on checkout save
- Why? Because it should be the actual behavior. - To allow using arithmetic operations with values of DecimalField when object are not retrieved from DB, some strings are replaced by Decimal or int. If you wonder why it's not automatically done, see: https://code.djangoproject.com/ticket/27825
This commit is contained in:
parent
44eee9be38
commit
478f56d94b
|
@ -362,6 +362,22 @@ class Checkout(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
created = self.pk is None
|
||||||
|
|
||||||
|
ret = super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
if created:
|
||||||
|
self.statements.create(
|
||||||
|
amount_taken=0,
|
||||||
|
balance_old=self.balance,
|
||||||
|
balance_new=self.balance,
|
||||||
|
by=self.created_by,
|
||||||
|
)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class CheckoutTransfer(models.Model):
|
class CheckoutTransfer(models.Model):
|
||||||
from_checkout = models.ForeignKey(
|
from_checkout = models.ForeignKey(
|
||||||
Checkout, on_delete = models.PROTECT,
|
Checkout, on_delete = models.PROTECT,
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from kfet.models import Account
|
from kfet.models import Account, Checkout
|
||||||
|
|
||||||
|
from .utils import create_user
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
@ -23,3 +28,33 @@ class AccountTests(TestCase):
|
||||||
|
|
||||||
with self.assertRaises(Account.DoesNotExist):
|
with self.assertRaises(Account.DoesNotExist):
|
||||||
Account.objects.get_by_password('bernard')
|
Account.objects.get_by_password('bernard')
|
||||||
|
|
||||||
|
|
||||||
|
class CheckoutTests(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.now = timezone.now()
|
||||||
|
|
||||||
|
self.u = create_user()
|
||||||
|
self.u_acc = self.u.profile.account_kfet
|
||||||
|
|
||||||
|
self.c = Checkout(
|
||||||
|
created_by=self.u_acc,
|
||||||
|
valid_from=self.now,
|
||||||
|
valid_to=self.now + datetime.timedelta(days=1),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_initial_statement(self):
|
||||||
|
"""A statement is added with initial balance on creation."""
|
||||||
|
self.c.balance = 10
|
||||||
|
self.c.save()
|
||||||
|
|
||||||
|
st = self.c.statements.get()
|
||||||
|
self.assertEqual(st.balance_new, 10)
|
||||||
|
self.assertEqual(st.amount_taken, 0)
|
||||||
|
self.assertEqual(st.amount_error, 0)
|
||||||
|
|
||||||
|
# Saving again doesn't create a new statement.
|
||||||
|
self.c.save()
|
||||||
|
|
||||||
|
self.assertEqual(self.c.statements.count(), 1)
|
||||||
|
|
|
@ -746,11 +746,15 @@ class CheckoutReadViewTests(ViewTestCaseMixin, TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
|
with mock.patch('django.utils.timezone.now') as mock_now:
|
||||||
|
mock_now.return_value = self.now
|
||||||
|
|
||||||
self.checkout = Checkout.objects.create(
|
self.checkout = Checkout.objects.create(
|
||||||
name='Checkout',
|
name='Checkout', balance=Decimal('10'),
|
||||||
created_by=self.accounts['team'],
|
created_by=self.accounts['team'],
|
||||||
valid_from=self.now,
|
valid_from=self.now,
|
||||||
valid_to=self.now + timedelta(days=5),
|
valid_to=self.now + timedelta(days=1),
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_ok(self):
|
def test_ok(self):
|
||||||
|
@ -794,7 +798,7 @@ class CheckoutUpdateViewTests(ViewTestCaseMixin, TestCase):
|
||||||
name='Checkout',
|
name='Checkout',
|
||||||
valid_from=self.now,
|
valid_from=self.now,
|
||||||
valid_to=self.now + timedelta(days=5),
|
valid_to=self.now + timedelta(days=5),
|
||||||
balance='3.14',
|
balance=Decimal('3.14'),
|
||||||
is_protected=False,
|
is_protected=False,
|
||||||
created_by=self.accounts['team'],
|
created_by=self.accounts['team'],
|
||||||
)
|
)
|
||||||
|
@ -864,6 +868,7 @@ class CheckoutStatementListViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
r.context['checkoutstatements'],
|
r.context['checkoutstatements'],
|
||||||
map(repr, expected_statements),
|
map(repr, expected_statements),
|
||||||
|
ordered=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -528,15 +528,7 @@ class CheckoutCreate(SuccessMessageMixin, CreateView):
|
||||||
|
|
||||||
# Creating
|
# Creating
|
||||||
form.instance.created_by = self.request.user.profile.account_kfet
|
form.instance.created_by = self.request.user.profile.account_kfet
|
||||||
checkout = form.save()
|
form.save()
|
||||||
|
|
||||||
# Création d'un relevé avec balance initiale
|
|
||||||
CheckoutStatement.objects.create(
|
|
||||||
checkout = checkout,
|
|
||||||
by = self.request.user.profile.account_kfet,
|
|
||||||
balance_old = checkout.balance,
|
|
||||||
balance_new = checkout.balance,
|
|
||||||
amount_taken = 0)
|
|
||||||
|
|
||||||
return super(CheckoutCreate, self).form_valid(form)
|
return super(CheckoutCreate, self).form_valid(form)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue