diff --git a/gpp/settings.py b/gpp/settings.py
index 7beefbb..fa98995 100644
--- a/gpp/settings.py
+++ b/gpp/settings.py
@@ -68,6 +68,9 @@ TEMPLATES = [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
+ 'libraries': {
+ 'dict': 'port.template_tags.dict'
+ }
},
},
]
diff --git a/gpp/urls.py b/gpp/urls.py
index 7adc9b9..77d3f22 100644
--- a/gpp/urls.py
+++ b/gpp/urls.py
@@ -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),
]
diff --git a/port/forms.py b/port/forms.py
index 1b9c855..53c3f76 100644
--- a/port/forms.py
+++ b/port/forms.py
@@ -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
diff --git a/port/models.py b/port/models.py
index ceed1a3..acaad6f 100644
--- a/port/models.py
+++ b/port/models.py
@@ -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)
diff --git a/port/template_tags/__init__.py b/port/template_tags/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/port/template_tags/dict.py b/port/template_tags/dict.py
new file mode 100644
index 0000000..7c38c89
--- /dev/null
+++ b/port/template_tags/dict.py
@@ -0,0 +1,8 @@
+from django import template
+
+register = template.Library()
+
+@register.filter(name='get')
+def get(val, arg):
+ return val.get(arg)
+
diff --git a/port/templates/base.html b/port/templates/base.html
index 479cdf4..cb93681 100644
--- a/port/templates/base.html
+++ b/port/templates/base.html
@@ -1,3 +1,4 @@
+{% load dict %}
{% load static %}
diff --git a/port/templates/index.html b/port/templates/index.html
index 2f38824..c96b865 100644
--- a/port/templates/index.html
+++ b/port/templates/index.html
@@ -9,4 +9,5 @@
{% block formnew %}
{% endblock %}
+ Reset Session
{% endblock %}
diff --git a/port/templates/insurance/add.html b/port/templates/insurance/add.html
new file mode 100644
index 0000000..30858ef
--- /dev/null
+++ b/port/templates/insurance/add.html
@@ -0,0 +1,12 @@
+{% extends "base.html" %}
+{% block content %}
+
+{% endblock %}
diff --git a/port/templates/insurance/list.html b/port/templates/insurance/list.html
new file mode 100644
index 0000000..ec19d31
--- /dev/null
+++ b/port/templates/insurance/list.html
@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+{% block content %}
+
+
+{% for insurance in insurances.all %}
+ - {{ insurance }}
+{% endfor %}
+
+{% endblock %}
diff --git a/port/templates/new_stay/new_stay-1.html b/port/templates/new_stay/new_stay-1.html
index 7dcc8a0..ec05283 100644
--- a/port/templates/new_stay/new_stay-1.html
+++ b/port/templates/new_stay/new_stay-1.html
@@ -1,6 +1,20 @@
{% extends "index.html" %}
{% block form %}
- {{ new_stay_insurance }}
+ {% if insurance is None %}
+ No insurance yet assigned for this boat
+ {% else %}
+ You're currently insured at
+ {{ insurance.name }}
+
+ If it's still the case, or if you don't have an insurance, please push the next button.
+
+ If not, please type in your insurance's name below.
+
+ {{ insurance_search_form }}
+
+
+
+ {% endif %}
{% endblock %}
{% block formnew %}
+{% endblock %}
diff --git a/port/urls/.py b/port/urls/.py
new file mode 100644
index 0000000..1464165
--- /dev/null
+++ b/port/urls/.py
@@ -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'),
+
+]
+
diff --git a/port/urls/index.py b/port/urls/index.py
index af73cc3..a980658 100644
--- a/port/urls/index.py
+++ b/port/urls/index.py
@@ -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'),
]
diff --git a/port/urls/insurance.py b/port/urls/insurance.py
new file mode 100644
index 0000000..7aeb204
--- /dev/null
+++ b/port/urls/insurance.py
@@ -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'),
+
+]
+
diff --git a/port/views/.py b/port/views/.py
new file mode 100644
index 0000000..119c2ee
--- /dev/null
+++ b/port/views/.py
@@ -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
diff --git a/port/views/index.py b/port/views/index.py
index 18f0571..7aec4be 100644
--- a/port/views/index.py
+++ b/port/views/index.py
@@ -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
diff --git a/port/views/insurance.py b/port/views/insurance.py
new file mode 100644
index 0000000..09a2b6f
--- /dev/null
+++ b/port/views/insurance.py
@@ -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