add person form, possibility to set an "employee" relation

This commit is contained in:
maxime 2019-06-21 14:02:07 +02:00
parent 0e03930288
commit 675b1f8ef1
21 changed files with 291 additions and 60 deletions

View File

@ -53,5 +53,6 @@ sed -i "$SEDCMD" ./urls/$NAME.py
cd $PWD_
rsync -av "./$BASE.tmp/" .
rm -r "./$BASE.tmp/"
echo "$NAME created, check files \"TO IMPLEMENT\" to make it work"
exit 0

View File

@ -22,5 +22,4 @@ urlpatterns = [
path('person/', include('port.urls.person', namespace='person')),
path('boat/', include('port.urls.boat', namespace='boat')),
path('admin/', admin.site.urls),
# NEW_ROUTE
]

49
gpp/views.py Normal file
View File

@ -0,0 +1,49 @@
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, world.")
def ports_status(request):
"""
res = ''
ports = Port.objects.all()
li = lambda x: '<li>' + str(x)
res = ''.join(map(li, ports))
res = '<html><body><ul>' + res + '</ul></body></html>'
return HttpResponse(res)
"""
return render(request, 'port/ports_status.html', {'ports': Port.objects})
def add_person(request):
"""
[GET] Renders the view to help people add persons
[POST] Adds person
"""
forms = {'person':None, 'address':None}
if (request.method == 'POST'):
forms.update({'person':PersonForm(request.POST)})
if (forms.get('person').is_valid()):
Person.objects.create(forms.get('person').cleaned_data)
return list_persons(request)
# Address handlinG
forms.update({'person':PersonForm(request.POST)})
if (forms.get('person').is_valid()):
Person.objects.create(forms.get('person').cleaned_data)
return list_persons(request)
else:
forms.update({'person':PersonForm()})
return render(request, 'person/add.html', {'form':forms})
def list_persons(request):
return render(request, 'person/list.html', {'persons': Person.objects})

View File

@ -81,12 +81,15 @@ class PortForm(ModelForm):
'address',
'company']
"""
name = CharField(label='Name', max_length=50)
address = CharField(label='Address', max_length=50)
company = CharField(label='Company', max_length=50)
"""
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = [
'port',
'position'
]
class DockForm(ModelForm):
class Meta:
model = Dock

6
port/gpp_todo Normal file
View File

@ -0,0 +1,6 @@
TODO :
- see if we can add a person in a Mooring (problem if the boat fleets
changes between two moorings)
- how to determine which is the right person if choosing from a list and two
have the same names (rare)

View File

@ -16,9 +16,9 @@ def validate_positive(value):
)
class Address(Model):
address = CharField(max_length=200)
zip_code = CharField(max_length=10)
city = CharField(max_length=200)
address = CharField(max_length=200,default='miss_add')
zip_code = CharField(max_length=10,default='miss_zip')
city = CharField(max_length=200,default='miss_city')
country = CountryField()
# Methods
@ -31,6 +31,9 @@ class Person(Model):
surname = CharField(max_length=50,blank=True,null=True)
nationality = CountryField(blank=True,null=True)
email = EmailField(max_length=254,blank=True,null=True)
# phone number has to be in international format
# i.e : for 0102030405 in france => +33102030405
# (the number is written WITHOUT the leading 0)
phone = PhoneNumberField(blank=True,null=True)
# Foreign keys
@ -167,10 +170,10 @@ class Port(Model):
class Employee(Model):
position = CharField('Job', max_length=20, blank=True,null=True)
position = CharField('Job',max_length=20,blank=True,null=True)
# Foreign keys
port = ForeignKey(Port,on_delete=PROTECT)
port = ForeignKey(Port,on_delete=PROTECT,blank=True)
person = ForeignKey(Person,on_delete=PROTECT)
# Methods

View File

@ -1,32 +0,0 @@
{% extends "base.html" %}
{% block content %}
<form action="{% url 'person:add' %}" method="post">
{% csrf_token %}
<!--
<label for="name">Name : </label>
<input type="text" name="name" id="name"/>
<br />
<label for="surname">Surname : </label>
<input type="text" name="surname" id="surname"/>
<br />
<label for="nationality">Nationality : </label>
<input type="text" name="nationality" id="nationality"/>
<br />
<label for="nationality">Nationality : </label>
<label for="email">e-mail : </label>
<input type="text" name="email" id="email"/>
<label for="phone">Phone number : </label>
<input type="countrycode" value="+33"/>
<input type="text" name="phone" id="phone"/>
-->
<fieldset>
<legend>Person</legend>
{{ form.person }}
</fieldset>
<fieldset>
<legend>Address</legend>
{{ form.address }}
</fieldset>
<input type="submit" value="add"/>
</form>
{% endblock %}

View File

@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block content %}
<form action="{% url 'person:add' %}" method="post">
{% csrf_token %}
<fieldset>
<!-- TO IMPLEMENT -->
<legend>Person</legend>
{{ person_form }}
</fieldset>
<fieldset>
<em>Leave blank if missing</em>
{{ address_form }}
</fieldset>
<fieldset>
<legend>Employee at</legend>
<em>Empty job means that this is no employee</em>
{{ employee_form }}
</fieldset>
<input type="submit" value="add"/>
</form>
{% endblock %}

View File

@ -1,8 +1,9 @@
{% extends "base.html" %}
{% block content %}
<ul>
<!-- TO IMPLEMENT -->
{% for person in persons.all %}
<li>{{ person }}</li>
<li>{{ person }} - {{ person.address.city }} ({{ person.address.country }})</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -6,6 +6,6 @@ app_name = 'boat'
urlpatterns = [
path('', index, name='index'),
path('list', list_boats, name='list'),
path('form', form, name='form'),
path('form', form_boat, name='form'),
path('add', add_boat, name='add'),
]

View File

@ -5,5 +5,9 @@ from ..views.person import *
app_name = 'person'
urlpatterns = [
path('', index, name='index'),
path('list', list_persons, name='list'),
path('form', form_person, name='form'),
path('add', add_person, name='add'),
]

View File

@ -6,6 +6,6 @@ app_name = 'port'
urlpatterns = [
path('', index, name='index'),
path('list', list_ports, name='list'),
path('form', form, name='form'),
path('form', form_port, name='form'),
path('add', add_port, name='add'),
]

View File

@ -16,7 +16,7 @@ def list_boats(request):
return render(request, 'boat/list.html',
{'boats': Boat.objects.all})
def form(request):
def form_boat(request):
boat_form = BoatForm(prefix='boa')
company_form = CompanyForm(prefix='com')
insurances = Insurance.objects.all()
@ -129,6 +129,6 @@ def add_boat(request):
except ValidationError as err:
pprint(err)
return form(request)
return form_boat(request)
return list_boats(request)

View File

@ -8,3 +8,45 @@ from ..forms import *
def index(request):
return HttpResponse("Hello Person")
def list_persons(request):
return render(request, 'person/list.html',
{'persons': Person.objects.all})
def form_person(request):
person_form = PersonForm(prefix='per')
address_form = AddressForm(prefix='add')
employee_form = EmployeeForm(prefix='emp')
return render(request, 'person/form.html',
{'person_form': person_form,
'address_form': address_form,
'employee_form': employee_form})
def add_person(request):
if request.method != 'POST':
return form(request.POST)
try:
new_person_form = PersonForm(request.POST, prefix='per')
new_person = new_person_form.save(commit=False)
new_address_form = AddressForm(request.POST, prefix='add')
new_person.address = new_address_form.save()
new_employee_form = EmployeeForm(request.POST, prefix='emp')
new_employee = new_employee_form.save(commit=False)
new_person.save()
if (new_employee.position is not None):
new_employee.person = new_person
new_employee.save()
except ValueError as err:
pprint(err)
return form_person(request)
except ValidationError as err:
pprint(err)
return form_person(request)
return list_persons(request)

View File

@ -14,6 +14,18 @@ def list_ports(request):
return render(request, 'port/list.html',
{'ports': Port.objects.all})
def form_port(request):
port_form = PortForm(prefix='por')
dock_forms = DockFormSet(prefix='doc')
plug_forms = PlugFormSet(prefix='plu')
tap_forms = TapFormSet(prefix='tap')
return render(request, 'port/form.html',
{'port_form': port_form,
'dock_forms': dock_forms,
'plug_forms': plug_forms,
'tap_forms': tap_forms,
'employees': Employee.objects.all()})
def add_port(request):
if request.method == 'POST':
port_form = PortForm(request.POST)
@ -33,13 +45,3 @@ def add_port(request):
return form(request)
def form(request):
port_form = PortForm()
dock_forms = DockFormSet()
plug_forms = PlugFormSet()
tap_forms = TapFormSet()
return render(request, 'port/form.html',
{'port_form': port_form,
'dock_forms': dock_forms,
'plug_forms': plug_forms,
'tap_forms': tap_forms})

View File

@ -0,0 +1,25 @@
"""gpp URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
#path('', include('port.urls', namespace='index')),
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('admin/', admin.site.urls),
]

View File

@ -0,0 +1,49 @@
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, world.")
def ports_status(request):
"""
res = ''
ports = Port.objects.all()
li = lambda x: '<li>' + str(x)
res = ''.join(map(li, ports))
res = '<html><body><ul>' + res + '</ul></body></html>'
return HttpResponse(res)
"""
return render(request, 'port/ports_status.html', {'ports': Port.objects})
def add_person(request):
"""
[GET] Renders the view to help people add persons
[POST] Adds person
"""
forms = {'person':None, 'address':None}
if (request.method == 'POST'):
forms.update({'person':PersonForm(request.POST)})
if (forms.get('person').is_valid()):
Person.objects.create(forms.get('person').cleaned_data)
return list_persons(request)
# Address handlinG
forms.update({'person':PersonForm(request.POST)})
if (forms.get('person').is_valid()):
Person.objects.create(forms.get('person').cleaned_data)
return list_persons(request)
else:
forms.update({'person':PersonForm()})
return render(request, 'person/add.html', {'form':forms})
def list_persons(request):
return render(request, 'person/list.html', {'persons': Person.objects})

View File

@ -0,0 +1,12 @@
{% extends "base.html" %}
{% block content %}
<form action="{% url 'person:add' %}" method="post">
{% csrf_token %}
<fieldset>
<!-- TO IMPLEMENT -->
<legend>Person</legend>
{{ form.person }}
</fieldset>
<input type="submit" value="add"/>
</form>
{% endblock %}

View File

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

View File

@ -0,0 +1,13 @@
from django.urls import path
from ..views.person import *
app_name = 'person'
urlpatterns = [
path('', index, name='index'),
path('list', list_persons, name='list'),
path('form', form_person, name='form'),
path('add', add_person, name='add'),
]

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 Person")
def list_persons(request):
# TO IMPLEMENT
pass
def form_person(request):
# TO IMPLEMENT
pass
def add_person(request):
# TO IMPLEMENT
pass