gpp/port/views/boat.py

158 lines
4.7 KiB
Python

from django.shortcuts import render
from django.http import HttpResponse
from django.core.exceptions import ValidationError, \
ObjectDoesNotExist
from pprint import pprint
from ..models import *
from ..forms import *
def index(request):
return HttpResponse("Hello Boat")
def list_boats(request):
return render(request, 'boat/list.html',
{'boats': Boat.objects.all})
def form_boat(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')
stay_form = StayForm(prefix='sta')
mooring_forms = MooringFormSet(prefix='moo')
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,
'stay_form': stay_form,
'mooring_forms': mooring_forms})
def add_boat(request, cb=None):
"""
see if it's not possible to give
an url a "callback" in case of success
or error
"""
try:
new_boat_form = BoatForm(request.POST, prefix='boa')
new_boat = new_boat_form.save()
except ValidationError as err:
pprint(err)
return form(request.POST)
except ValueError as err:
pprint(err)
return form(request.POST)
if cb is not None:
return cb(request)
return list_boats(request)
def add_boat_(request):
if request.method != 'POST':
return form(request.POST)
try:
new_boat_form = BoatForm(request.POST, prefix='boa')
new_boat = new_boat_form.save(commit=False)
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:
new_company = None
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_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)
for mooring in new_moorings:
# Check if mooring is between the date of stay
if new_stay.arrival > mooring.date:
mooring = False
if new_stay.departure < mooring.date:
mooring = False
# Check if dock is available
else:
# Set the moorings later
new_moorings = []
if new_company is not None:
new_boat.company = new_company.save()
new_boat.save()
if new_insurance is not None:
new_insurance.save()
# Set insurance for boat since current date
BoatInsurance.objects.create(
insurance = new_insurance,
boat = new_boat
)
new_person.address = new_address.save()
new_person.save()
# Say that person is sailing on boat
SailsOn.objects.create(
boat = new_boat,
person = new_person,
is_captain = False,
is_crew = False,
is_owner = False,
is_guest = False,
is_pet = True)
new_stay.boat = new_boat
new_stay.save()
for mooring in new_moorings:
morring.stay = new_stay
mooring.save()
new_boat_form.save_m2m()
except ValueError as err:
pprint(err)
return form_boat(request)
except ValidationError as err:
pprint(err)
return form_boat(request)
return list_boats(request)