use selectdatewidget for stay dates

This commit is contained in:
maxime 2019-06-10 19:34:46 +02:00
parent 7a1335a423
commit cc0e91ffc6
3 changed files with 38 additions and 15 deletions

View File

@ -1,7 +1,7 @@
from django.forms import Form, ModelForm, \ from django.forms import Form, ModelForm, \
CharField, IntegerField, DecimalField, \ CharField, IntegerField, DecimalField, \
BooleanField, EmailField, DateTimeField, ImageField, \ BooleanField, EmailField, DateTimeField, ImageField, \
formset_factory formset_factory, SelectDateWidget
from django_countries.fields import CountryField from django_countries.fields import CountryField
from phonenumber_field.formfields import PhoneNumberField from phonenumber_field.formfields import PhoneNumberField
@ -171,6 +171,8 @@ class StayForm(ModelForm):
'going_to', 'going_to',
'no_mooring'] 'no_mooring']
widgets = { widgets = {
'arrival': SelectDateWidget(),
'departure': SelectDateWidget()
} }

View File

@ -8,7 +8,19 @@
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Insurance</legend> <legend>Insurance</legend>
{{ insurance_form }} <div>
<h3>Choose in list :</h3>
<select name="boat_insurance" id="select_boat_insurance">
<option value="-1" select="">---</option>
{% for ins in insurances %}
<option value="{{ ins.pk }}">{{ ins.name }}</option>
{% endfor %}
</select>
</div>
<div>
<h3>Or create a new one (will take existing if name already
exists</h3>
{{ insurance_form }}
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Company</legend> <legend>Company</legend>

View File

@ -1,6 +1,7 @@
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError, \
ObjectDoesNotExist
from pprint import pprint from pprint import pprint
@ -18,6 +19,7 @@ def list_boats(request):
def form(request): def form(request):
boat_form = BoatForm(prefix='boa') boat_form = BoatForm(prefix='boa')
company_form = CompanyForm(prefix='com') company_form = CompanyForm(prefix='com')
insurances = Insurance.objects.all()
insurance_form = InsuranceForm(prefix='ins') insurance_form = InsuranceForm(prefix='ins')
person_form = PersonForm(prefix='per') person_form = PersonForm(prefix='per')
address_form = AddressForm(prefix='add') address_form = AddressForm(prefix='add')
@ -27,6 +29,7 @@ def form(request):
return render(request, 'boat/form.html', return render(request, 'boat/form.html',
{'boat_form': boat_form, {'boat_form': boat_form,
'company_form': company_form, 'company_form': company_form,
'insurances': insurances,
'insurance_form': insurance_form, 'insurance_form': insurance_form,
'person_form': person_form, 'person_form': person_form,
'address_form': address_form, 'address_form': address_form,
@ -40,23 +43,29 @@ def add_boat(request):
try: try:
new_boat_form = BoatForm(request.POST, prefix='boa') new_boat_form = BoatForm(request.POST, prefix='boa')
new_boat = new_boat_form.save(commit=False) new_boat = new_boat_form.save(commit=False)
pprint(new_boat)
# TODO : Handle case where insurance is already existing insurance = None
# if not new_boat.boat_insurance: if request.POST.get('boat_insurance'):
# When the boat's insurance is not specified, it means we need to try:
# create a new insurance insurance = Insurance.objects.get(
# new_insurance = InsuranceForm(request.POST).save(commit=False) id=request.POST.get('boat_insurance'))
#else: except ObjectDoesNotExist:
# new_insurance = None insurance = None
new_insurance = InsuranceForm(request.POST, prefix='ins').save(commit=False) if insurance is not None:
new_insurance = InsuranceForm(request.POST, prefix='ins').save(commit=False)
if len(new_insurance.name) <= 0:
insurance = None
else:
insurance = new_insurance
if not new_boat.company: if not new_boat.company:
# When the boat's company is not specified, it means we need to # When the boat's company is not specified, it means we need to
# create a new company # create a new company
try: try:
new_company = CompanyForm(request.POST, prefix='com').save(commit=False) new_company = CompanyForm(request.POST, prefix='com').save(commit=False)
if len(new_company.name) <= 0:
new_company = None
except: except:
return form(request) return form(request)
else: else:
@ -64,6 +73,8 @@ def add_boat(request):
new_person = PersonForm(request.POST, prefix='per').save(commit=False) new_person = PersonForm(request.POST, prefix='per').save(commit=False)
new_address = AddressForm(request.POST, prefix='add').save(commit=False) new_address = AddressForm(request.POST, prefix='add').save(commit=False)
pprint(request.POST.get('sta-arrival'))
pprint(request.POST.get('sta-departure'))
new_stay = StayForm(request.POST, prefix='sta').save(commit=False) new_stay = StayForm(request.POST, prefix='sta').save(commit=False)
if new_stay.no_mooring is not True: if new_stay.no_mooring is not True:
new_moorings = MooringFormSet(request.POST, prefix='moo').save(commit=False) new_moorings = MooringFormSet(request.POST, prefix='moo').save(commit=False)
@ -71,9 +82,7 @@ def add_boat(request):
# Check if mooring is between the date of stay # Check if mooring is between the date of stay
if new_stay.arrival > mooring.date: if new_stay.arrival > mooring.date:
mooring = False mooring = False
elif new_stay.departure is not None and\ if new_stay.departure < mooring.date:
new_stay.departure < mooring.date:
mooring = False mooring = False
# Check if dock is available # Check if dock is available