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

View File

@ -8,6 +8,18 @@
</fieldset>
<fieldset>
<legend>Insurance</legend>
<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>

View File

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