Merge branch 'aureplop/fix-184' into 'master'

Fix available checkouts in K-Psul

Closes #184

See merge request cof-geek/gestioCOF!282
This commit is contained in:
Martin Pepin 2018-02-05 22:35:04 +01:00
commit a3295ca6a3
3 changed files with 65 additions and 8 deletions

View file

@ -296,17 +296,17 @@ class KPsulAccountForm(forms.ModelForm):
class KPsulCheckoutForm(forms.Form):
checkout = forms.ModelChoiceField(
queryset=(
Checkout.objects
.filter(
is_protected=False,
valid_from__lte=timezone.now(),
valid_to__gte=timezone.now(),
)
),
queryset=None,
widget=forms.Select(attrs={'id': 'id_checkout_select'}),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Create the queryset on form instanciation to use the current time.
self.fields['checkout'].queryset = (
Checkout.objects.is_valid().filter(is_protected=False))
class KPsulOperationForm(forms.ModelForm):
article = forms.ModelChoiceField(

View file

@ -341,6 +341,13 @@ class AccountNegative(models.Model):
return self.start + kfet_config.overdraft_duration
class CheckoutQuerySet(models.QuerySet):
def is_valid(self):
now = timezone.now()
return self.filter(valid_from__lte=now, valid_to__gte=now)
class Checkout(models.Model):
created_by = models.ForeignKey(
Account, on_delete = models.PROTECT,
@ -353,6 +360,8 @@ class Checkout(models.Model):
default = 0)
is_protected = models.BooleanField(default = False)
objects = CheckoutQuerySet.as_manager()
def get_absolute_url(self):
return reverse('kfet.checkout.read', kwargs={'pk': self.pk})

48
kfet/tests/test_forms.py Normal file
View file

@ -0,0 +1,48 @@
import datetime
from unittest import mock
from django.test import TestCase
from django.utils import timezone
from kfet.forms import KPsulCheckoutForm
from kfet.models import Checkout
from .utils import create_user
class KPsulCheckoutFormTests(TestCase):
def setUp(self):
self.now = timezone.now()
user = create_user()
self.c1 = Checkout.objects.create(
name='C1', balance=10,
created_by=user.profile.account_kfet,
valid_from=self.now,
valid_to=self.now + datetime.timedelta(days=1),
)
self.form = KPsulCheckoutForm()
def test_checkout(self):
checkout_f = self.form.fields['checkout']
self.assertListEqual(list(checkout_f.choices), [
('', '---------'),
(self.c1.pk, 'C1'),
])
@mock.patch('django.utils.timezone.now')
def test_checkout_valid(self, mock_now):
"""
Checkout are filtered using the current datetime.
Regression test for #184.
"""
self.now += datetime.timedelta(days=2)
mock_now.return_value = self.now
form = KPsulCheckoutForm()
checkout_f = form.fields['checkout']
self.assertListEqual(list(checkout_f.choices), [('', '---------')])