Compare commits

...

10 Commits

24 changed files with 545 additions and 38 deletions

View File

@ -68,6 +68,9 @@ TEMPLATES = [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'libraries': {
'dict': 'port.template_tags.dict'
}
},
},
]

View File

@ -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),
]

View File

@ -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
@ -70,9 +90,12 @@ class BoatSearchForm(Form):
def __init__(self, name='', choices=[]):
super(Form, self).__init__()
print(name)
self.fields['search_name'].initial = name
self.fields['search_results'].choices = choices
if choices == [] and len(name) < 3:
self.fields.pop('search_results')
else:
self.fields['search_results'].choices = choices
class BoatForm(ModelForm):
@ -92,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

View File

@ -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()
@ -155,13 +170,25 @@ class SailsOn(Model):
present = BooleanField(default=True)
def update(self, sailson):
self.is_captain = sailson.is_captain
self.is_crew = sailson.is_crew
self.is_owner = sailson.is_owner
self.is_guest = sailson.is_guest
self.is_pet = sailson.is_pet
self.present = sailson.present
class BoatInsurance(Model):
contract = IntegerField(blank=True,null=True)
date = DateTimeField(auto_now_add=True)
# 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)

View File

View File

@ -0,0 +1,8 @@
from django import template
register = template.Library()
@register.filter(name='get')
def get(val, arg):
return val.get(arg)

View File

@ -1,3 +1,4 @@
{% load dict %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
@ -21,8 +22,9 @@
<link rel="stylesheet" href="{% static "css/gpp.css" %}">
<link rel="icon" type="image/png" href="{% static "images/favicon.png" %}">
<!--
<script src="{% static "js/jquery-3.4.1.min.js" %}"></script>
<script src="{% static "js/main.js" %}"></script>
<!--
<link href="{% static "css/bootstrap.css" %}" rel="stylesheet" type="text/css"/>
<script src="{% static "js/bootstrap.js" %}"></script>
-->

View File

@ -7,4 +7,7 @@
<a href="{% url 'index:new_stay' %}">New Stay</a>
{% endblock %}
</form>
{% block formnew %}
{% endblock %}
<a href="{% url 'index:reset_session' %}">Reset Session</a>
{% endblock %}

View 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 %}

View File

@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block content %}
<ul>
<!-- TO IMPLEMENT -->
{% for insurance in insurances.all %}
<li>{{ insurance }}</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -1,10 +1,30 @@
{% extends "index.html" %}
{% block form %}
<div class="search" id="search_boat">
<div class="search" id="search_boat">
{{ boat_search_form }}
</div>
<div class="new hidden" id="new_boat">
<!-- {{ boat_form }} -->
</div>
<input type="submit" value="Submit" />
{% if search_name %}
<label for="id_search_results_none">
<input type="radio"
name="search_results"
value="-1"
id="id_search_results_none"/>
None ( create a new boat )
</label>
{% endif %}
<input type="submit" value="Search boat" />
</div>
{% endblock %}
{% block formnew %}
<form action="{% url 'index:new_boat' %}" method="post" name="boat_add">
{% csrf_token %}
<div class="new hidden" id="new_boat">
<a
id="switch_boat_search"
href="#search_boat">
Switch to boat search
</a>
{{ boat_form }}
<input type="submit" value="Add boat" />
</div>
</form>
{% endblock %}

View File

@ -0,0 +1,31 @@
{% extends "index.html" %}
{% block form %}
{% 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 'index:new_insurance' %}" method="post">
{% csrf_token %}
{% if insurance is None %}
<div class="new" id="new_insurance">
{% else %}
<div class="new hidden" id="new_insurance">
{% endif %}
{{ insurance_form }}
<input type="submit" value="Add insurance" />
</div>
</form>
{% endblock %}

View File

@ -0,0 +1,49 @@
{% extends "index.html" %}
{% load dict %}
{% block form %}
{% if sailors_forms.initial|length <= 0 %}
<em>No one is yet assigned for this boat</em>
{% else %}
<!-- necessary to work with formsets -->
{{ sailors_forms.management_form }}
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 }}
{{ sailors_form.person }}</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">
{% csrf_token %}
<div class="new hidden" id="new_person">
{{ person_form }}
<input type="submit" value="Add person" />
</div>
</form>
{% endblock %}

View File

@ -0,0 +1,8 @@
{% extends "index.html" %}
{% load dict %}
{% block form %}
{{ stay_form }}
<br />
<input type="submit" value="Next" />
<br />
{% endblock %}

13
port/urls/.py Normal file
View 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'),
]

View File

@ -6,4 +6,7 @@ app_name = 'index'
urlpatterns = [
path('', index, name='index'),
path('new_stay/', new_stay, name='new_stay'),
path('new_stay/boat', new_boat, name='new_boat'),
path('new_stay/insurance', new_insurance, name='new_insurance'),
path('reset_session/', reset_session, name='reset_session'),
]

13
port/urls/insurance.py Normal file
View 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
View 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

View File

@ -36,10 +36,31 @@ def form_boat(request):
'stay_form': stay_form,
'mooring_forms': mooring_forms})
def add_boat(request):
if request.method != 'POST':
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)
@ -71,10 +92,9 @@ def add_boat(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)
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)
@ -127,6 +147,9 @@ def add_boat(request):
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)

View File

@ -7,45 +7,204 @@ 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
name = request.POST.get('search_name', '')
boat_existing = [ (b.id, b.name) for b in \
Boat.objects.filter(name__icontains=name) ]
pprint(boat_existing)
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
return render(request, 'new_stay-1.html')
elif request.session['new_stay_step'] ==2:
return new_stay_1(request)
elif request.session['new_stay_step'] == 2:
# Person form
return render(request, 'new_stay-2.html')
elif request.session['new_stay_step'] ==3:
return new_stay_2(request)
elif request.session['new_stay_step'] == 3:
# Stay form
return render(request, '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-4.html')
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)

22
port/views/insurance.py Normal file
View 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

View File

@ -6,6 +6,7 @@ chardet==3.0.4
curtsies==0.3.0
Django==2.2.1
django-countries==5.3.3
django-datetime-widget==0.9.3
django-phonenumber-field==3.0.1
djangodbu==0.0.18
geographiclib==1.49

2
static/js/jquery-3.4.1.min.js vendored Normal file

File diff suppressed because one or more lines are too long

33
static/js/main.js Normal file
View File

@ -0,0 +1,33 @@
$(function () {
// todo
// change this with python normally
$('#id_boa-name').val($('#id_search_name').val());
// Events
$('[name=search_results]').on('change',
function (e) {
new_stay_0($(this));
})
$('#id_search_name').on('keyup',
function (e) {
$('#id_name').val($(this).val())
})
$('#switch_boat_search').on('click',
function (e) {
console.log('lol')
$('#new_boat').hide();
$('#search_boat').show();
})
// New Stay
// new_stay-0
function new_stay_0(elem) {
if (elem.val() < 0) {
$('#new_boat').show();
$('#search_boat').hide();
}
}
})