add port form, change url

This commit is contained in:
maxime 2019-06-08 13:08:57 +02:00
parent 2e4d50f66e
commit a4e56bf94e
14 changed files with 261 additions and 17 deletions

View File

@ -17,8 +17,8 @@ from django.contrib import admin
from django.urls import include, path from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('', include('port.urls')), #path('', include('port.urls', namespace='index')),
path('port/', include('port.urls', namespace='port')), path('port/', include('port.urls.port', namespace='port')),
path('person/', include('port.urls', namespace='person')), path('person/', include('port.urls.person', namespace='person')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]

View File

@ -1,5 +1,5 @@
from django.forms import Form, CharField, IntegerField, DecimalField, \ from django.forms import Form, CharField, IntegerField, DecimalField, \
BooleanField, EmailField, DateTimeField, ImageField BooleanField, EmailField, DateTimeField, ImageField, formset_factory
from django_countries.fields import CountryField from django_countries.fields import CountryField
from phonenumber_field.formfields import PhoneNumberField from phonenumber_field.formfields import PhoneNumberField
@ -15,3 +15,49 @@ class AddressForm(Form):
zip_code = CharField(label='ZipCode', max_length=10) zip_code = CharField(label='ZipCode', max_length=10)
city = CharField(label='City', max_length=200) city = CharField(label='City', max_length=200)
country = CountryField().formfield(label='Country') country = CountryField().formfield(label='Country')
class DockForm(Form):
name = CharField(label='Name', max_length=10)
length = DecimalField(
label='Length', min_value=0,
max_digits=7,
decimal_places=2)
width = DecimalField(
label='Width', min_value=0,
max_digits=7,
decimal_places=2)
depth_min = DecimalField(
label='Depth (min.)', min_value=0,
max_digits=7,
decimal_places=2)
depth_max = DecimalField(
label='Depth (max.)', min_value=0,
max_digits=7,
decimal_places=2)
DockFormSet = formset_factory(DockForm)
class PortForm(Form):
name = CharField(label='Name', max_length=50)
address = CharField(label='Address', max_length=50)
company = CharField(label='Company', max_length=50)
class PlugForm(Form):
name = CharField(label='Name', max_length=10)
amperage = DecimalField(
label='Amperage',
min_value=0,
max_digits=3,
decimal_places=0)
voltage = DecimalField(
label='Voltage',
min_value=0,
max_digits=3,
decimal_places=0)
PlugFormSet = formset_factory(PlugForm)
class TapForm(Form):
name = CharField(label='Name', max_length=10)
TapFormSet = formset_factory(TapForm)

View File

@ -0,0 +1,65 @@
# Generated by Django 2.2.1 on 2019-06-07 09:29
from django.db import migrations, models
import django_countries.fields
import phonenumber_field.modelfields
class Migration(migrations.Migration):
dependencies = [
('port', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='dock',
old_name='depth',
new_name='depth_max',
),
migrations.RenameField(
model_name='dock',
old_name='num',
new_name='name',
),
migrations.RenameField(
model_name='plug',
old_name='num',
new_name='name',
),
migrations.RenameField(
model_name='tap',
old_name='num',
new_name='name',
),
migrations.AddField(
model_name='billline',
name='value',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=7),
),
migrations.AddField(
model_name='dock',
name='depth_min',
field=models.DecimalField(decimal_places=2, max_digits=7, null=True),
),
migrations.AlterField(
model_name='address',
name='country',
field=django_countries.fields.CountryField(max_length=2),
),
migrations.AlterField(
model_name='company',
name='phone',
field=phonenumber_field.modelfields.PhoneNumberField(max_length=128, null=True, region=None),
),
migrations.AlterField(
model_name='person',
name='nationality',
field=django_countries.fields.CountryField(max_length=2, null=True),
),
migrations.AlterField(
model_name='person',
name='phone',
field=phonenumber_field.modelfields.PhoneNumberField(max_length=128, null=True, region=None),
),
]

View File

@ -19,7 +19,7 @@ class Address(Model):
class Person(Model): class Person(Model):
name = CharField(max_length=50,null=True) name = CharField(max_length=50,null=True)
surname = CharField(max_length=50,null=True) surname = CharField(max_length=50,null=True)
nationality = CountryField() nationality = CountryField(null=True)
email = EmailField(max_length=254,null=True) email = EmailField(max_length=254,null=True)
phone = PhoneNumberField(null=True) phone = PhoneNumberField(null=True)
@ -116,12 +116,12 @@ class Employee(Model):
return '{}, {} at {}'.format(str(self.person), self.position, return '{}, {} at {}'.format(str(self.person), self.position,
str(self.port)) str(self.port))
class Dock(Model): class Dock(Model):
num = CharField(max_length=10) name = CharField(max_length=10)
length = DecimalField(max_digits=7, decimal_places=2, null=True) length = DecimalField(max_digits=7, decimal_places=2, null=True)
width = DecimalField(max_digits=7, decimal_places=2, null=True) width = DecimalField(max_digits=7, decimal_places=2, null=True)
depth = DecimalField(max_digits=7, decimal_places=2, null=True) depth_min = DecimalField(max_digits=7, decimal_places=2, null=True)
depth_max = DecimalField(max_digits=7, decimal_places=2, null=True)
# Foreign keys # Foreign keys
port = ForeignKey(Port,on_delete=PROTECT) port = ForeignKey(Port,on_delete=PROTECT)
@ -129,7 +129,7 @@ class Dock(Model):
return '{} : {}'.format(str(self.port), self.num) return '{} : {}'.format(str(self.port), self.num)
class Plug(Model): class Plug(Model):
num = CharField(max_length=10) name = CharField(max_length=10)
amperage = DecimalField(max_digits=7, decimal_places=2) amperage = DecimalField(max_digits=7, decimal_places=2)
voltage = DecimalField(max_digits=7, decimal_places=2) voltage = DecimalField(max_digits=7, decimal_places=2)
@ -139,7 +139,7 @@ class Plug(Model):
return '{} : {}'.format(str(self.port), self.num) return '{} : {}'.format(str(self.port), self.num)
class Tap(Model): class Tap(Model):
num = CharField(max_length=10) name = CharField(max_length=10)
# Foreign keys # Foreign keys
port = ForeignKey(Port,on_delete=PROTECT) port = ForeignKey(Port,on_delete=PROTECT)
@ -184,7 +184,7 @@ class Bill(Model):
class BillLine(Model): class BillLine(Model):
quantity = DecimalField(max_digits=7, decimal_places=2) quantity = DecimalField(max_digits=7, decimal_places=2)
value = DecimalField(max_digits=7, decimal_places=2) value = DecimalField(max_digits=7, decimal_places=2, default=0.)
# Foreign keyks # Foreign keyks
service = ForeignKey(Service,on_delete=PROTECT) service = ForeignKey(Service,on_delete=PROTECT)

View File

@ -0,0 +1,36 @@
{% extends "base.html" %}
{% block content %}
<form action="{% url 'port:add' %}" method="post">
{% csrf_token %}
<fieldset>
<legend>Port</legend>
{{ port_form }}
</fieldset>
<fieldset>
<legend>Dock</legend>
<table>
{% for dock_form in dock_forms %}
{{ dock_form.as_table }}
{% endfor %}
</table>
</fieldset>
<fieldset>
<legend>Plug</legend>
<table>
{% for plug_form in plug_forms %}
{{ plug_form.as_table }}
{% endfor %}
</table>
</fieldset>
<fieldset>
<legend>Tap</legend>
<table>
{% for tap_form in tap_forms %}
{{ tap_form.as_table }}
{% endfor %}
</table>
</fieldset>
<input type="submit" value="add"/>
</form>
{% endblock %}

View File

@ -0,0 +1,8 @@
{% extends "base.html" %}
{% block content %}
<ul>
{% for port in ports.all %}
<li>{{ port }}</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -1,11 +1,8 @@
from django.urls import path from django.urls import path
from . import views from .views.index import *
app_name = 'port' app_name = 'index'
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', index, name='index'),
path('', views.ports_status, name='list'),
path('new', views.add_person, name='add'),
path('list', views.list_persons, name='list'),
] ]

0
port/urls/__init__.py Normal file
View File

9
port/urls/person.py Normal file
View File

@ -0,0 +1,9 @@
from django.urls import path
from ..views.person import *
app_name = 'person'
urlpatterns = [
path('', index, name='index'),
]

11
port/urls/port.py Normal file
View File

@ -0,0 +1,11 @@
from django.urls import path
from ..views.port import *
app_name = 'port'
urlpatterns = [
path('', index, name='index'),
path('list', list_ports, name='list'),
path('form', form, name='form'),
path('add', add_port, name='add'),
]

0
port/views/__init__.py Normal file
View File

17
port/views/index.py Normal file
View File

@ -0,0 +1,17 @@
from django.shortcuts import render
from django.http import HttpResponse
from pprint import pprint
from ..models import *
from ..forms import *
import ..views as Views
def index(request):
return HttpResponse("Hello World")
def index_port(request):
return Views.port.index(request)
def index_person(request):
return Views.person.index(request)

10
port/views/person.py Normal file
View File

@ -0,0 +1,10 @@
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")

45
port/views/port.py Normal file
View File

@ -0,0 +1,45 @@
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 Port")
def list_ports(request):
return render(request, 'port/list.html',
{'ports': Port.objects.all})
def add_port(request):
if request.method == 'POST':
port_form = PortForm(request.POST)
if not port_form.is_valid():
return form(request)
dock_forms = DockFormSet(request.POST)
dock_forms_data = dock_forms.save(commit=False)
for dock_data in dock_forms_data:
pass
plug_forms = PlugFormSet(request.POST)
plug_forms_data = plug_forms.save(commit=False)
tap_forms = tapFormSet(request.POST)
tap_forms_data = tap_forms.save(commit=False)
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})