From cc0e91ffc6c1d26840a58396d1d4fef25299865e Mon Sep 17 00:00:00 2001 From: maxime Date: Mon, 10 Jun 2019 19:34:46 +0200 Subject: [PATCH] use selectdatewidget for stay dates --- port/forms.py | 4 +++- port/templates/boat/form.html | 14 +++++++++++++- port/views/boat.py | 35 ++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/port/forms.py b/port/forms.py index b362b3d..03e9388 100644 --- a/port/forms.py +++ b/port/forms.py @@ -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() } diff --git a/port/templates/boat/form.html b/port/templates/boat/form.html index f14fccf..f073857 100644 --- a/port/templates/boat/form.html +++ b/port/templates/boat/form.html @@ -8,7 +8,19 @@
Insurance - {{ insurance_form }} +
+

Choose in list :

+ +
+
+

Or create a new one (will take existing if name already + exists

+ {{ insurance_form }}
Company diff --git a/port/views/boat.py b/port/views/boat.py index 56f836d..258226f 100644 --- a/port/views/boat.py +++ b/port/views/boat.py @@ -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 - 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: # 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