Skip to content

Commit cd6f262

Browse files
committed
fix: implement optional expiry date to fix #25
1 parent e0511a2 commit cd6f262

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

myapp/forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from django import forms
88
from .models import *
99
from django.utils.translation import gettext_lazy as _
10+
from datetime import timedelta
11+
from django.utils import timezone
1012

1113
class ItemForm(forms.ModelForm):
1214
file = forms.FileField(required=False)
@@ -29,6 +31,9 @@ def __init__(self, *args, **kwargs):
2931
else:
3032
self.fields['value'].required = True
3133

34+
# Make expiry_date optional
35+
self.fields['expiry_date'].required = False
36+
3237
def clean_file(self):
3338
file = self.cleaned_data.get('file')
3439
if file:
@@ -46,6 +51,11 @@ def clean(self):
4651
item_type = cleaned_data.get('type')
4752
value_type = cleaned_data.get('value_type')
4853
value = cleaned_data.get('value')
54+
expiry_date = cleaned_data.get('expiry_date')
55+
56+
# Set expiry_date to 50 years in the future if not provided
57+
if not expiry_date:
58+
cleaned_data['expiry_date'] = timezone.now() + timedelta(days=50*365) # 50 years in the future
4959

5060
if item_type == 'loyaltycard' and value != 0:
5161
error_msg_value = _('Value must be zero for loyalty cards.')
@@ -60,6 +70,7 @@ def clean(self):
6070
elif item_type != 'loyaltycard' and (value is None or value < 0):
6171
error_message_positive = _('Value must be positive.')
6272
raise forms.ValidationError(error_message_positive)
73+
6374
return cleaned_data
6475

6576
class TransactionForm(forms.ModelForm):

myapp/templates/create-item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ <h5 class="card-title">{% trans "Item Details" %}</h5>
9898
<div class="row mb-3">
9999
<label for="expiry_date" class="col-sm-2 col-form-label">{% trans "Expiry Date" %}</label>
100100
<div class="col-sm-10">
101-
<input type="date" class="form-control" id="expiry_date" name="expiry_date" required>
101+
<input type="date" class="form-control" id="expiry_date" name="expiry_date">
102102
</div>
103103
</div>
104104
<div class="row mb-3">

0 commit comments

Comments
 (0)