gpp/port/views/index.py

211 lines
6.3 KiB
Python

from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from django.forms import TextInput, RadioSelect
from pprint import pprint
from ..models import *
from ..forms import *
def index(request):
return render(request, 'index.html')
def reset_session(request):
request.session.clear()
return render(request, 'index.html')
def new_stay(request):
try:
print('Current step : ' + str(request.session.get('new_stay_step')))
except IndexError:
print('New stay is empty')
if not request.session.get('new_stay_step', False) \
or request.session.get('new_stay_done', False) :
# This is a new stay, we initialize the session
request.session['new_stay_step'] = 0
request.session['new_stay_done'] = False
# Boat form
return new_stay_0(request)
elif request.session['new_stay_step'] == 1:
# Insurance form
return new_stay_1(request)
elif request.session['new_stay_step'] == 2:
# Person form
return new_stay_2(request)
elif request.session['new_stay_step'] == 3:
# Stay form
return new_stay_3(request)
elif request.session['new_stay_step'] == 4:
# Save form
request.session['new_stay_done'] = True
return new_stay_4(request)
def new_stay_0(request):
new_boat_search = True
try:
boat_id = int(request.POST.get('search_results'))
boat = Boat.objects.get(pk=boat_id)
if boat is not None:
request.method = 'GET'
request.session['new_stay_step'] = 1
request.session['new_stay_boat'] = boat_id
insurance = boat.getInsurance()
request.session['new_stay_insurance'] = \
insurance.id \
if insurance is not None \
else None
return new_stay(request)
except TypeError as e:
new_boat_search = False
except Exception as e:
pprint(e)
name = request.POST.get('search_name', '')
boat_existing = [ (b.id, b.name) for b in \
Boat.objects.filter(name__icontains=name) ] \
if len(name) > 2 else []
boat_search_form = BoatSearchForm(
name=name,
choices=boat_existing)
boat_form = BoatForm(request.POST, prefix='boa')
return render(request,
'new_stay/new_stay-0.html',
{
'boat_search_form': boat_search_form,
'boat_form': boat_form,
'search_name': name
})
def new_stay_1(request):
# Insurance
try:
if request.POST.get('search_results'):
print('Search results')
print(request.POST.get('search_results'))
elif request.method == 'POST':
# User has not selected a search result
# So this must be the good insurance
print('No search')
request.method = 'GET'
request.session['new_stay_step'] = 2
return new_stay(request)
except IndexError:
print('Missing search_results data')
data = dict(request.session)
data['insurance_form'] = InsuranceForm(prefix='ins')
data['insurance_search_form'] = InsuranceSearchForm()
data['insurance'] = None
try:
id_ins = int(request.session['new_stay_insurance'])
data['insurance'] = Insurance.objects.get( \
pk=id_ins)
except KeyError:
data['insurance'] = None
except TypeError:
data['insurance'] = None
return render(request, 'new_stay/new_stay-1.html', data)
def new_stay_2(request):
# Person
if request.method == 'POST':
sailors_forms = SailorsFormSet(data=request.POST,
prefix='sai')
data = {'sailors_forms': sailors_forms}
for sailor_form in sailors_forms:
if not sailor_form.has_changed():
request.session['new_stay_step'] = 3
return new_stay(request)
if not sailor_form.is_valid():
return render(request, 'new_stay/new_stay-2.html')
else:
sailor_tmp = sailor_form.save(commit=False)
sailor = SailsOn.objects.get(person=sailor_tmp.person)
sailor.boat_id = request.session['new_stay_boat']
sailor.update(sailor_tmp)
sailor.save()
request.method = 'GET'
request.session['new_stay_step'] = 3
return new_stay(request)
boat = Boat.objects.get(
pk=request.session.get('new_stay_boat'))
data = dict(request.session)
sailors = []
persons = {}
for s in boat.sailors.filter(present=True):
s_ = {}
s_['person'] = s.person
s_['is_captain'] = s.is_captain
s_['is_crew'] = s.is_crew
s_['is_owner'] = s.is_owner
s_['is_guest'] = s.is_guest
s_['is_pet'] = s.is_pet
s_['present'] = s.present
persons[s.person.id] = str(s.person)
sailors.append(s_)
data['persons'] = persons
data['sailors_forms'] = SailorsFormSet(
prefix='sai',
initial=sailors)
return render(request, 'new_stay/new_stay-2.html', data)
def new_stay_3(request):
if request.method == 'POST':
stay_form = StayForm(request.POST)
data = {'stay_form': stay_form}
stay = stay_form.save(commit=False)
stay.boat_id = request.session['new_stay_boat']
pprint(stay)
if (stay.departure is not None
and stay.arrival is not None
and stay.departure < stay.arrival):
return render(request, 'new_stay/new_stay-3.html', data)
data = {}
data['stay_form'] = StayForm()
return render(request, 'new_stay/new_stay-3.html', data)
def new_stay_4(request):
pass
def new_boat(request):
boat_form = BoatForm(request.POST, prefix='boa')
boat = boat_form.save()
request.method = 'GET'
request.session['new_stay_step'] = 1
request.session['new_stay_boat'] = boat.id
return new_stay(request)
def new_insurance(request):
ins_form = InsuranceForm(request.POST, prefix='ins')
new_insurance = ins_form.save(commit=False)
insurance = Insurance.objects.get(name=new_insurance.name)
if insurance is None:
insurance = ins_form.save()
request.method = 'GET'
request.session['new_stay_step'] = 2
request.session['new_stay_insurance'] = insurance.id
return new_stay(request)