kfet -- Fix available checkouts in K-Psul

The checkout validity is checked using the current datetime (when
requesting the kpsul page).
This commit is contained in:
Aurélien Delobelle 2018-01-15 16:56:38 +01:00
parent e23e1bdba6
commit 525bb4d16d
2 changed files with 17 additions and 8 deletions

View file

@ -296,17 +296,17 @@ class KPsulAccountForm(forms.ModelForm):
class KPsulCheckoutForm(forms.Form): class KPsulCheckoutForm(forms.Form):
checkout = forms.ModelChoiceField( checkout = forms.ModelChoiceField(
queryset=( queryset=None,
Checkout.objects
.filter(
is_protected=False,
valid_from__lte=timezone.now(),
valid_to__gte=timezone.now(),
)
),
widget=forms.Select(attrs={'id': 'id_checkout_select'}), 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): class KPsulOperationForm(forms.ModelForm):
article = forms.ModelChoiceField( article = forms.ModelChoiceField(

View file

@ -341,6 +341,13 @@ class AccountNegative(models.Model):
return self.start + kfet_config.overdraft_duration 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): class Checkout(models.Model):
created_by = models.ForeignKey( created_by = models.ForeignKey(
Account, on_delete = models.PROTECT, Account, on_delete = models.PROTECT,
@ -353,6 +360,8 @@ class Checkout(models.Model):
default = 0) default = 0)
is_protected = models.BooleanField(default = False) is_protected = models.BooleanField(default = False)
objects = CheckoutQuerySet.as_manager()
def get_absolute_url(self): def get_absolute_url(self):
return reverse('kfet.checkout.read', kwargs={'pk': self.pk}) return reverse('kfet.checkout.read', kwargs={'pk': self.pk})