insurance form, person form, add dict template tags
This commit is contained in:
parent
c5bd563b18
commit
bbc42ed5c5
@ -68,6 +68,9 @@ TEMPLATES = [
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
'libraries': {
|
||||
'dict': 'port.template_tags.dict'
|
||||
}
|
||||
},
|
||||
},
|
||||
]
|
||||
|
@ -21,5 +21,6 @@ urlpatterns = [
|
||||
path('port/', include('port.urls.port', namespace='port')),
|
||||
path('person/', include('port.urls.person', namespace='person')),
|
||||
path('boat/', include('port.urls.boat', namespace='boat')),
|
||||
path('insurance/', include('port.urls.insurance', namespace='insurance')),
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
|
@ -2,6 +2,7 @@ from django.forms import Form, ModelForm, \
|
||||
CharField, IntegerField, DecimalField, ChoiceField,\
|
||||
BooleanField, EmailField, DateTimeField, ImageField, \
|
||||
formset_factory, SelectDateWidget, TextInput, \
|
||||
HiddenInput, \
|
||||
RadioSelect
|
||||
|
||||
from django_countries.fields import CountryField
|
||||
@ -51,6 +52,25 @@ class CompanyForm(ModelForm):
|
||||
'phone',
|
||||
'registration_num']
|
||||
|
||||
class InsuranceSearchForm(Form):
|
||||
search_name = CharField(
|
||||
label='Name',
|
||||
required=False
|
||||
)
|
||||
search_results = ChoiceField(
|
||||
label='Insurances',
|
||||
widget=RadioSelect
|
||||
)
|
||||
|
||||
def __init__(self, name='', choices=[]):
|
||||
super(Form, self).__init__()
|
||||
self.fields['search_name'].initial = name
|
||||
|
||||
if choices == [] and len(name) < 3:
|
||||
self.fields.pop('search_results')
|
||||
else:
|
||||
self.fields['search_results'].choices = choices
|
||||
|
||||
class InsuranceForm(CompanyForm):
|
||||
class Meta:
|
||||
model = Insurance
|
||||
@ -95,6 +115,26 @@ class BoatForm(ModelForm):
|
||||
'company',
|
||||
'picture']
|
||||
|
||||
class SailorsForm(ModelForm):
|
||||
class Meta:
|
||||
model = SailsOn
|
||||
|
||||
fields = [
|
||||
'person',
|
||||
'is_captain',
|
||||
'is_crew',
|
||||
'is_owner',
|
||||
'is_guest',
|
||||
'is_pet',
|
||||
'present'
|
||||
]
|
||||
|
||||
widgets = {
|
||||
'person': HiddenInput()
|
||||
}
|
||||
|
||||
SailorsFormSet = formset_factory(SailorsForm, extra=0)
|
||||
|
||||
class PortForm(ModelForm):
|
||||
class Meta:
|
||||
model = Port
|
||||
|
@ -143,8 +143,23 @@ class Boat(Model):
|
||||
def __str__(self):
|
||||
return '{}'.format(self.name)
|
||||
|
||||
def getInsurance(self, date=None):
|
||||
try:
|
||||
return self.insured \
|
||||
.order_by('-date')[0].insurance \
|
||||
if date is None \
|
||||
else self.insured.filter(date__lte=date) \
|
||||
.order_by('-date')[0].insurance
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
class SailsOn(Model):
|
||||
boat = ForeignKey(Boat,on_delete=PROTECT)
|
||||
boat = ForeignKey(
|
||||
Boat,
|
||||
on_delete=PROTECT,
|
||||
related_name='sailors'
|
||||
)
|
||||
|
||||
person = ForeignKey(Person,on_delete=PROTECT)
|
||||
|
||||
is_captain = BooleanField()
|
||||
@ -161,7 +176,11 @@ class BoatInsurance(Model):
|
||||
|
||||
# Foreign keys
|
||||
insurance = ForeignKey(Insurance,on_delete=PROTECT)
|
||||
boat = ForeignKey(Boat,on_delete=PROTECT)
|
||||
boat = ForeignKey(
|
||||
Boat,
|
||||
on_delete=PROTECT,
|
||||
related_name='insured'
|
||||
)
|
||||
|
||||
class Port(Model):
|
||||
name = CharField(max_length=50)
|
||||
|
0
port/template_tags/__init__.py
Normal file
0
port/template_tags/__init__.py
Normal file
8
port/template_tags/dict.py
Normal file
8
port/template_tags/dict.py
Normal file
@ -0,0 +1,8 @@
|
||||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.filter(name='get')
|
||||
def get(val, arg):
|
||||
return val.get(arg)
|
||||
|
@ -1,3 +1,4 @@
|
||||
{% load dict %}
|
||||
{% load static %}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
@ -9,4 +9,5 @@
|
||||
</form>
|
||||
{% block formnew %}
|
||||
{% endblock %}
|
||||
<a href="{% url 'index:reset_session' %}">Reset Session</a>
|
||||
{% endblock %}
|
||||
|
12
port/templates/insurance/add.html
Normal file
12
port/templates/insurance/add.html
Normal file
@ -0,0 +1,12 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<form action="{% url 'insurance:add' %}" method="post">
|
||||
{% csrf_token %}
|
||||
<fieldset>
|
||||
<!-- TO IMPLEMENT -->
|
||||
<legend>Insurance</legend>
|
||||
{{ form.insurance }}
|
||||
</fieldset>
|
||||
<input type="submit" value="add"/>
|
||||
</form>
|
||||
{% endblock %}
|
9
port/templates/insurance/list.html
Normal file
9
port/templates/insurance/list.html
Normal file
@ -0,0 +1,9 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<ul>
|
||||
<!-- TO IMPLEMENT -->
|
||||
{% for insurance in insurances.all %}
|
||||
<li>{{ insurance }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
@ -1,6 +1,20 @@
|
||||
{% extends "index.html" %}
|
||||
{% block form %}
|
||||
{{ new_stay_insurance }}
|
||||
{% if insurance is None %}
|
||||
<em>No insurance yet assigned for this boat</em>
|
||||
{% else %}
|
||||
You're currently insured at
|
||||
<em>{{ insurance.name }}</em>
|
||||
<br />
|
||||
If it's still the case, or if you don't have an insurance, please push the next button.
|
||||
<br />
|
||||
If not, please type in your insurance's name below.
|
||||
<br />
|
||||
{{ insurance_search_form }}
|
||||
<br />
|
||||
<input type="submit" value="Next" />
|
||||
<br />
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
{% block formnew %}
|
||||
<form action="{% url 'insurance:add' %}" method="post">
|
||||
|
45
port/templates/new_stay/new_stay-2.html
Normal file
45
port/templates/new_stay/new_stay-2.html
Normal file
@ -0,0 +1,45 @@
|
||||
{% extends "index.html" %}
|
||||
{% load dict %}
|
||||
{% block form %}
|
||||
{% if sailors_forms.initial|length <= 0 %}
|
||||
<em>No one is yet assigned for this boat</em>
|
||||
{% else %}
|
||||
|
||||
Previous passengers :
|
||||
<table>
|
||||
<tr>
|
||||
<th>Person</th>
|
||||
<th>Captain</th>
|
||||
<th>Crew</th>
|
||||
<th>Owner</th>
|
||||
<th>Guest</th>
|
||||
<th>Pet</th>
|
||||
<th>On the boat</th>
|
||||
</tr>
|
||||
{% for sailors_form in sailors_forms %}
|
||||
<tr>
|
||||
{% with id=sailors_form.person.value %}
|
||||
<td>{{ persons|get:id }}</td>
|
||||
{% endwith %}
|
||||
<td>{{ sailors_form.is_captain }}</td>
|
||||
<td>{{ sailors_form.is_crew }}</td>
|
||||
<td>{{ sailors_form.is_owner }}</td>
|
||||
<td>{{ sailors_form.is_guest }}</td>
|
||||
<td>{{ sailors_form.is_pet }}</td>
|
||||
<td>{{ sailors_form.present }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<br />
|
||||
<input type="submit" value="Next" />
|
||||
<br />
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
{% block formnew %}
|
||||
<form action="{% url 'person:add' %}" method="post">
|
||||
<div class="new hidden" id="new_person">
|
||||
{{ person_form }}
|
||||
<input type="submit" value="Add person" />
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
13
port/urls/.py
Normal file
13
port/urls/.py
Normal file
@ -0,0 +1,13 @@
|
||||
from django.urls import path
|
||||
|
||||
from ..views. import *
|
||||
|
||||
app_name = ''
|
||||
urlpatterns = [
|
||||
path('', index, name='index'),
|
||||
path('list', list_s, name='list'),
|
||||
path('form', form_, name='form'),
|
||||
path('add', add_, name='add'),
|
||||
|
||||
]
|
||||
|
@ -6,4 +6,5 @@ app_name = 'index'
|
||||
urlpatterns = [
|
||||
path('', index, name='index'),
|
||||
path('new_stay/', new_stay, name='new_stay'),
|
||||
path('reset_session/', reset_session, name='reset_session'),
|
||||
]
|
||||
|
13
port/urls/insurance.py
Normal file
13
port/urls/insurance.py
Normal file
@ -0,0 +1,13 @@
|
||||
from django.urls import path
|
||||
|
||||
from ..views.insurance import *
|
||||
|
||||
app_name = 'insurance'
|
||||
urlpatterns = [
|
||||
path('', index, name='index'),
|
||||
path('list', list_insurances, name='list'),
|
||||
path('form', form_insurance, name='form'),
|
||||
path('add', add_insurance, name='add'),
|
||||
|
||||
]
|
||||
|
22
port/views/.py
Normal file
22
port/views/.py
Normal file
@ -0,0 +1,22 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
from ..models import *
|
||||
from ..forms import *
|
||||
|
||||
def index(request):
|
||||
return HttpResponse("Hello ")
|
||||
|
||||
def list_s(request):
|
||||
# TO IMPLEMENT
|
||||
pass
|
||||
|
||||
def form_(request):
|
||||
# TO IMPLEMENT
|
||||
pass
|
||||
|
||||
def add_(request):
|
||||
# TO IMPLEMENT
|
||||
pass
|
@ -7,62 +7,138 @@ from pprint import pprint
|
||||
|
||||
from ..models import *
|
||||
from ..forms import *
|
||||
#import ..views as Views
|
||||
|
||||
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
|
||||
|
||||
try:
|
||||
boat_id = int(request.POST.get('search_results'))
|
||||
boat = Boat.objects.get(pk=boat_id)
|
||||
if boat is not None:
|
||||
request.session['new_stay_step'] = 1
|
||||
request.session['new_stay_boat'] = boat_id
|
||||
if boat.insurance is not None:
|
||||
request.session['new_stay_insurance'] = boat.insurance.id
|
||||
return new_stay(request)
|
||||
except TypeError:
|
||||
pass
|
||||
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()
|
||||
|
||||
|
||||
return render(request,
|
||||
'new_stay/new_stay-0.html',
|
||||
{
|
||||
'boat_search_form': boat_search_form,
|
||||
'boat_form': boat_form
|
||||
})
|
||||
# Boat form
|
||||
return new_stay_0(request)
|
||||
elif request.session['new_stay_step'] == 1:
|
||||
# Insurance form
|
||||
data = dict(request.session)
|
||||
data['insurance_form'] = InsuranceForm()
|
||||
return render(request, 'new_stay/new_stay-1.html', data)
|
||||
return new_stay_1(request)
|
||||
elif request.session['new_stay_step'] ==2:
|
||||
# Person form
|
||||
return render(request, 'new_stay/new_stay-2.html')
|
||||
return new_stay_2(request)
|
||||
elif request.session['new_stay_step'] ==3:
|
||||
# Stay form
|
||||
return render(request, 'new_stay/new_stay-3.html')
|
||||
return new_stay_3(request)
|
||||
elif request.session['new_stay_step'] == 4:
|
||||
# Save form
|
||||
request.session['new_stay_done'] = True
|
||||
return render(request, 'new_stay/new_stay-4.html')
|
||||
return new_stay_4(request)
|
||||
|
||||
def new_stay_0(request):
|
||||
try:
|
||||
boat_id = int(request.POST.get('search_results'))
|
||||
boat = Boat.objects.get(pk=boat_id)
|
||||
if boat is not None:
|
||||
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:
|
||||
pass
|
||||
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()
|
||||
|
||||
|
||||
return render(request,
|
||||
'new_stay/new_stay-0.html',
|
||||
{
|
||||
'boat_search_form': boat_search_form,
|
||||
'boat_form': boat_form
|
||||
})
|
||||
|
||||
def new_stay_1(request):
|
||||
# Insurance
|
||||
try:
|
||||
if request.POST.get('search_results'):
|
||||
print('Search results')
|
||||
print(request.POST.get('search_results'))
|
||||
else:
|
||||
# User has not select a search result
|
||||
# So this must be the good insurance
|
||||
print('No search')
|
||||
request.session['new_stay_step'] = 2
|
||||
return new_stay(request)
|
||||
|
||||
except IndexError:
|
||||
print('Missing search_results data')
|
||||
|
||||
print('No ')
|
||||
data = dict(request.session)
|
||||
data['insurance_form'] = InsuranceForm()
|
||||
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
|
||||
|
||||
return render(request, 'new_stay/new_stay-1.html', data)
|
||||
|
||||
def new_stay_2(request):
|
||||
# Person
|
||||
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):
|
||||
pass
|
||||
def new_stay_4(request):
|
||||
pass
|
||||
|
22
port/views/insurance.py
Normal file
22
port/views/insurance.py
Normal file
@ -0,0 +1,22 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
from ..models import *
|
||||
from ..forms import *
|
||||
|
||||
def index(request):
|
||||
return HttpResponse("Hello Insurance")
|
||||
|
||||
def list_insurances(request):
|
||||
# TO IMPLEMENT
|
||||
pass
|
||||
|
||||
def form_insurance(request):
|
||||
# TO IMPLEMENT
|
||||
pass
|
||||
|
||||
def add_insurance(request):
|
||||
# TO IMPLEMENT
|
||||
pass
|
Loading…
Reference in New Issue
Block a user