diff --git a/gen_route.sh b/gen_route.sh index b6ef116..dbdbd4c 100755 --- a/gen_route.sh +++ b/gen_route.sh @@ -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 diff --git a/gpp/urls.py b/gpp/urls.py index 1424c5c..e0b7360 100644 --- a/gpp/urls.py +++ b/gpp/urls.py @@ -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 ] diff --git a/gpp/views.py b/gpp/views.py new file mode 100644 index 0000000..e9fe858 --- /dev/null +++ b/gpp/views.py @@ -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: '
  • ' + str(x) + res = ''.join(map(li, ports)) + res = '' + 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}) diff --git a/port/forms.py b/port/forms.py index 03e9388..e2023fa 100644 --- a/port/forms.py +++ b/port/forms.py @@ -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 diff --git a/port/gpp_todo b/port/gpp_todo new file mode 100644 index 0000000..0ea1236 --- /dev/null +++ b/port/gpp_todo @@ -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) + diff --git a/port/models.py b/port/models.py index 0431921..0fc065d 100644 --- a/port/models.py +++ b/port/models.py @@ -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 diff --git a/port/templates/person/add.html b/port/templates/person/add.html deleted file mode 100644 index 1e5e5ad..0000000 --- a/port/templates/person/add.html +++ /dev/null @@ -1,32 +0,0 @@ -{% extends "base.html" %} -{% block content %} -
    - {% csrf_token %} - -
    - Person - {{ form.person }} -
    -
    - Address - {{ form.address }} -
    - -
    -{% endblock %} diff --git a/port/templates/person/form.html b/port/templates/person/form.html new file mode 100644 index 0000000..458bf65 --- /dev/null +++ b/port/templates/person/form.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} +{% block content %} +
    + {% csrf_token %} +
    + + Person + {{ person_form }} + +
    +
    + Leave blank if missing + {{ address_form }} + +
    +
    + Employee at + Empty job means that this is no employee + {{ employee_form }} +
    + +
    +{% endblock %} diff --git a/port/templates/person/list.html b/port/templates/person/list.html index 8b2defb..a177c2e 100644 --- a/port/templates/person/list.html +++ b/port/templates/person/list.html @@ -1,8 +1,9 @@ {% extends "base.html" %} {% block content %} {% endblock %} diff --git a/port/urls/boat.py b/port/urls/boat.py index 0e15c42..3833ef9 100644 --- a/port/urls/boat.py +++ b/port/urls/boat.py @@ -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'), ] diff --git a/port/urls/person.py b/port/urls/person.py index 5066e07..bebcae3 100644 --- a/port/urls/person.py +++ b/port/urls/person.py @@ -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'), + ] diff --git a/port/urls/port.py b/port/urls/port.py index ee7523f..7d823c7 100644 --- a/port/urls/port.py +++ b/port/urls/port.py @@ -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'), ] diff --git a/port/views/boat.py b/port/views/boat.py index 258226f..4c0a883 100644 --- a/port/views/boat.py +++ b/port/views/boat.py @@ -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) diff --git a/port/views/person.py b/port/views/person.py index c834fd9..39570b6 100644 --- a/port/views/person.py +++ b/port/views/person.py @@ -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) diff --git a/port/views/port.py b/port/views/port.py index ef7208c..0d2d0ce 100644 --- a/port/views/port.py +++ b/port/views/port.py @@ -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}) diff --git a/route_template.tmp/gpp/urls.py b/route_template.tmp/gpp/urls.py new file mode 100644 index 0000000..e0b7360 --- /dev/null +++ b/route_template.tmp/gpp/urls.py @@ -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), +] diff --git a/route_template.tmp/gpp/views.py b/route_template.tmp/gpp/views.py new file mode 100644 index 0000000..e9fe858 --- /dev/null +++ b/route_template.tmp/gpp/views.py @@ -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: '
  • ' + str(x) + res = ''.join(map(li, ports)) + res = '' + 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}) diff --git a/route_template.tmp/port/templates/person/add.html b/route_template.tmp/port/templates/person/add.html new file mode 100644 index 0000000..6a3f59f --- /dev/null +++ b/route_template.tmp/port/templates/person/add.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} +{% block content %} +
    + {% csrf_token %} +
    + + Person + {{ form.person }} +
    + +
    +{% endblock %} diff --git a/route_template.tmp/port/templates/person/list.html b/route_template.tmp/port/templates/person/list.html new file mode 100644 index 0000000..abcb9f3 --- /dev/null +++ b/route_template.tmp/port/templates/person/list.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} +{% block content %} + +{% endblock %} diff --git a/route_template.tmp/port/urls/person.py b/route_template.tmp/port/urls/person.py new file mode 100644 index 0000000..bebcae3 --- /dev/null +++ b/route_template.tmp/port/urls/person.py @@ -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'), + +] + diff --git a/route_template.tmp/port/views/person.py b/route_template.tmp/port/views/person.py new file mode 100644 index 0000000..bc8101e --- /dev/null +++ b/route_template.tmp/port/views/person.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 Person") + +def list_persons(request): +# TO IMPLEMENT + pass + +def form_person(request): +# TO IMPLEMENT + pass + +def add_person(request): +# TO IMPLEMENT + pass