working boat form
This commit is contained in:
parent
7e6fb59f71
commit
7a1335a423
|
@ -7,6 +7,8 @@ admin.site.register(Person)
|
|||
admin.site.register(Company)
|
||||
admin.site.register(Employee)
|
||||
admin.site.register(Boat)
|
||||
admin.site.register(BoatInsurance)
|
||||
admin.site.register(Insurance)
|
||||
admin.site.register(Port)
|
||||
admin.site.register(Dock)
|
||||
admin.site.register(Plug)
|
||||
|
@ -15,4 +17,5 @@ admin.site.register(Payment)
|
|||
admin.site.register(Service)
|
||||
admin.site.register(Bill)
|
||||
admin.site.register(Stay)
|
||||
admin.site.register(SailsOn)
|
||||
admin.site.register(Mooring)
|
||||
|
|
101
port/forms.py
101
port/forms.py
|
@ -7,6 +7,17 @@ from phonenumber_field.formfields import PhoneNumberField
|
|||
|
||||
from .models import *
|
||||
|
||||
class AddressForm(ModelForm):
|
||||
class Meta:
|
||||
model = Address
|
||||
|
||||
fields = [
|
||||
'address',
|
||||
'zip_code',
|
||||
'city',
|
||||
'country'
|
||||
]
|
||||
|
||||
class PersonForm(ModelForm):
|
||||
class Meta:
|
||||
model = Person
|
||||
|
@ -26,17 +37,55 @@ class PersonForm(ModelForm):
|
|||
phone = PhoneNumberField(label='Phone Number')
|
||||
"""
|
||||
|
||||
class AddressForm(ModelForm):
|
||||
class CompanyForm(ModelForm):
|
||||
class Meta:
|
||||
model = Address
|
||||
model = Company
|
||||
|
||||
fields = [
|
||||
'address',
|
||||
'zip_code',
|
||||
'city',
|
||||
'country'
|
||||
]
|
||||
'name',
|
||||
'email',
|
||||
'phone',
|
||||
'registration_num']
|
||||
|
||||
class InsuranceForm(CompanyForm):
|
||||
class Meta:
|
||||
model = Insurance
|
||||
|
||||
fields = [
|
||||
'name']
|
||||
|
||||
|
||||
class BoatForm(ModelForm):
|
||||
class Meta:
|
||||
model = Boat
|
||||
|
||||
fields = ['name',
|
||||
'registration_num',
|
||||
'length',
|
||||
'beam',
|
||||
'water_draught',
|
||||
'tonnage',
|
||||
'water_tank',
|
||||
'model',
|
||||
'heating',
|
||||
'passenger_capacity',
|
||||
'company',
|
||||
'picture']
|
||||
|
||||
class PortForm(ModelForm):
|
||||
class Meta:
|
||||
model = Port
|
||||
|
||||
fields = [
|
||||
'name',
|
||||
'address',
|
||||
'company']
|
||||
|
||||
"""
|
||||
name = CharField(label='Name', max_length=50)
|
||||
address = CharField(label='Address', max_length=50)
|
||||
company = CharField(label='Company', max_length=50)
|
||||
"""
|
||||
|
||||
class DockForm(ModelForm):
|
||||
class Meta:
|
||||
|
@ -71,21 +120,6 @@ class DockForm(ModelForm):
|
|||
|
||||
DockFormSet = formset_factory(DockForm)
|
||||
|
||||
class PortForm(ModelForm):
|
||||
class Meta:
|
||||
model = Port
|
||||
|
||||
fields = [
|
||||
'name',
|
||||
'address',
|
||||
'company']
|
||||
|
||||
"""
|
||||
name = CharField(label='Name', max_length=50)
|
||||
address = CharField(label='Address', max_length=50)
|
||||
company = CharField(label='Company', max_length=50)
|
||||
"""
|
||||
|
||||
class PlugForm(ModelForm):
|
||||
class Meta:
|
||||
model = Plug
|
||||
|
@ -125,42 +159,25 @@ class TapForm(ModelForm):
|
|||
|
||||
TapFormSet = formset_factory(TapForm)
|
||||
|
||||
class BoatForm(ModelForm):
|
||||
class Meta:
|
||||
model = Boat
|
||||
fields = ['name',
|
||||
'registration_num',
|
||||
'length',
|
||||
'beam',
|
||||
'water_draught',
|
||||
'tonnage',
|
||||
'water_tank',
|
||||
'model',
|
||||
'heating',
|
||||
'passenger_capacity',
|
||||
'picture']
|
||||
|
||||
|
||||
class StayForm(ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = Stay
|
||||
|
||||
fields = ['arrival',
|
||||
'departure',
|
||||
'coming_from',
|
||||
'going_to',
|
||||
'no_mooring']
|
||||
widgets = {
|
||||
#Use localization and bootstrap 3
|
||||
'datetime': DateTimeWidget(
|
||||
attrs={'id':"yourdatetimeid"},
|
||||
usel10n = True)
|
||||
}
|
||||
|
||||
|
||||
class MooringForm(ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = Mooring
|
||||
|
||||
fields = ['date',
|
||||
'dock',
|
||||
'tap',
|
||||
|
|
138
port/models.py
138
port/models.py
|
@ -21,6 +21,7 @@ class Address(Model):
|
|||
city = CharField(max_length=200)
|
||||
country = CountryField()
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}, {} - {}, {}'.format(self.address, self.zip_code,
|
||||
self.city, self.country)
|
||||
|
@ -34,18 +35,22 @@ class Person(Model):
|
|||
|
||||
# Foreign keys
|
||||
address = ForeignKey(Address,on_delete=SET_NULL,blank=True,null=True)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} {}'.format(self.name, self.surname)
|
||||
|
||||
|
||||
class Company(Model):
|
||||
name = CharField(max_length=50)
|
||||
name = CharField(max_length=50,blank=True)
|
||||
email = EmailField(max_length=200,blank=True,null=True)
|
||||
phone = PhoneNumberField(blank=True,null=True)
|
||||
registration_num = CharField(max_length=50,blank=True,null=True)
|
||||
|
||||
# Foreign keys
|
||||
address = ForeignKey(Address,on_delete=SET_NULL,blank=True,null=True)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}'.format(self.name)
|
||||
|
||||
|
@ -54,32 +59,76 @@ class Insurance(Company):
|
|||
|
||||
class Boat(Model):
|
||||
name = CharField(max_length=50)
|
||||
registration_num = CharField('Registration number', max_length=20,blank=True,null=True)
|
||||
length = DecimalField('Length', max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
beam = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
water_draught = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
tonnage = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
water_tank = DecimalField('Water tank capacity', max_digits=7, decimal_places=2,
|
||||
blank=True,null=True, validators=[validate_positive])
|
||||
registration_num = CharField('Registration number',
|
||||
max_length=20,
|
||||
blank=True,
|
||||
null=True)
|
||||
|
||||
length = DecimalField('Length',
|
||||
max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
beam = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
water_draught = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
tonnage = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
water_tank = DecimalField('Water tank capacity',
|
||||
max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
model = CharField(max_length=50,blank=True,null=True)
|
||||
|
||||
heating = CharField(max_length=50,blank=True,null=True)
|
||||
|
||||
passenger_capacity = IntegerField(blank=True,null=True)
|
||||
picture = ImageField(upload_to='uploads/', height_field=None,
|
||||
width_field=None, max_length=100, blank=True,null=True)
|
||||
|
||||
picture = ImageField(upload_to='uploads/',
|
||||
height_field=None,
|
||||
width_field=None,
|
||||
max_length=100,
|
||||
blank=True,
|
||||
null=True)
|
||||
|
||||
# Foreign keys
|
||||
company = ForeignKey(Company,on_delete=SET_NULL,blank=True,null=True,related_name='+')
|
||||
company = ForeignKey(Company,
|
||||
on_delete=SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name='+')
|
||||
|
||||
boat_insurance = ManyToManyField(
|
||||
Insurance,
|
||||
through='BoatInsurance',
|
||||
through_fields=('boat', 'insurance')
|
||||
through_fields=('boat', 'insurance'),
|
||||
blank=True
|
||||
)
|
||||
|
||||
persons = ManyToManyField(
|
||||
Person,
|
||||
through='SailsOn',
|
||||
through_fields=('boat', 'person')
|
||||
)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}'.format(self.name)
|
||||
|
||||
|
@ -110,8 +159,9 @@ class Port(Model):
|
|||
employees = ManyToManyField(
|
||||
Person,
|
||||
through='Employee',
|
||||
through_fields=('port', 'person')
|
||||
)
|
||||
through_fields=('port', 'person'))
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}'.format(self.name)
|
||||
|
||||
|
@ -122,29 +172,57 @@ class Employee(Model):
|
|||
# Foreign keys
|
||||
port = ForeignKey(Port,on_delete=PROTECT)
|
||||
person = ForeignKey(Person,on_delete=PROTECT)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}, {} at {}'.format(str(self.person), self.position,
|
||||
str(self.port))
|
||||
|
||||
class Dock(Model):
|
||||
name = CharField(max_length=10)
|
||||
length = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
width = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
depth_min = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
depth_max = DecimalField(max_digits=7, decimal_places=2, blank=True,null=True, validators=[validate_positive])
|
||||
|
||||
length = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
width = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
depth_min = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
depth_max = DecimalField(max_digits=7,
|
||||
decimal_places=2,
|
||||
blank=True,
|
||||
null=True,
|
||||
validators=[validate_positive])
|
||||
|
||||
# Foreign keys
|
||||
port = ForeignKey(Port,on_delete=PROTECT)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} : {}'.format(str(self.port), self.num)
|
||||
|
||||
class Plug(Model):
|
||||
name = CharField(max_length=10)
|
||||
amperage = DecimalField(max_digits=7, decimal_places=2, validators=[validate_positive])
|
||||
voltage = DecimalField(max_digits=7, decimal_places=2, validators=[validate_positive])
|
||||
amperage = DecimalField(max_digits=7, decimal_places=2,
|
||||
validators=[validate_positive])
|
||||
voltage = DecimalField(max_digits=7, decimal_places=2,
|
||||
validators=[validate_positive])
|
||||
|
||||
# Foreign keys
|
||||
port = ForeignKey(Port,on_delete=PROTECT)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} : {}'.format(str(self.port), self.num)
|
||||
|
||||
|
@ -153,6 +231,8 @@ class Tap(Model):
|
|||
|
||||
# Foreign keys
|
||||
port = ForeignKey(Port,on_delete=PROTECT)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} : {}'.format(str(self.port), self.num)
|
||||
|
||||
|
@ -170,11 +250,15 @@ class Payment(Model):
|
|||
],
|
||||
default='CSH'
|
||||
)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} at {}'.format(self.num, str(self.date))
|
||||
|
||||
class Service(Model):
|
||||
name = CharField(max_length=50)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}'.format(self.name)
|
||||
|
||||
|
@ -189,6 +273,8 @@ class Bill(Model):
|
|||
through='BillPayment',
|
||||
through_fields=('bill', 'payment')
|
||||
)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} at {}'.format(self.num, str(self.date))
|
||||
|
||||
|
@ -199,6 +285,8 @@ class BillLine(Model):
|
|||
# Foreign keyks
|
||||
service = ForeignKey(Service,on_delete=PROTECT)
|
||||
bill = ForeignKey(Bill,on_delete=PROTECT)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{}x {} - {}'.format(self.quantity, str(self.service),
|
||||
str(self.value))
|
||||
|
@ -220,8 +308,12 @@ class Stay(Model):
|
|||
# Foreign keys
|
||||
boat = ForeignKey(Boat,on_delete=PROTECT)
|
||||
bill = ForeignKey(Bill,on_delete=PROTECT,blank=True,null=True)
|
||||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} - {} : {}'.format(self.arrival, self.departure, str(self.boat))
|
||||
return '{} - {} : {}'.format(self.arrival,
|
||||
self.departure,
|
||||
str(self.boat))
|
||||
|
||||
class Mooring(Model):
|
||||
date = DateTimeField()
|
||||
|
@ -234,4 +326,6 @@ class Mooring(Model):
|
|||
|
||||
# Methods
|
||||
def __str__(self):
|
||||
return '{} - {} at {}'.format(str(self.date), str(self.stay.boat), str(self.dock))
|
||||
return '{} - {} at {}'.format(str(self.date),
|
||||
str(self.stay.boat),
|
||||
str(self.dock))
|
||||
|
|
|
@ -6,9 +6,18 @@
|
|||
<legend>Boat</legend>
|
||||
{{ boat_form }}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>Insurance</legend>
|
||||
{{ insurance_form }}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>Company</legend>
|
||||
{{ company_form }}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>Person</legend>
|
||||
{{ person_form }}
|
||||
{{ address_form }}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>Stay</legend>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
|
@ -12,38 +13,113 @@ def index(request):
|
|||
|
||||
def list_boats(request):
|
||||
return render(request, 'boat/list.html',
|
||||
{'boats': boat.objects.all})
|
||||
{'boats': Boat.objects.all})
|
||||
|
||||
def form(request):
|
||||
boat_form = BoatForm()
|
||||
person_form = PersonForm()
|
||||
stay_form = StayForm()
|
||||
mooring_forms = MooringFormSet()
|
||||
boat_form = BoatForm(prefix='boa')
|
||||
company_form = CompanyForm(prefix='com')
|
||||
insurance_form = InsuranceForm(prefix='ins')
|
||||
person_form = PersonForm(prefix='per')
|
||||
address_form = AddressForm(prefix='add')
|
||||
stay_form = StayForm(prefix='sta')
|
||||
mooring_forms = MooringFormSet(prefix='moo')
|
||||
|
||||
return render(request, 'boat/form.html',
|
||||
{'boat_form': boat_form,
|
||||
'company_form': company_form,
|
||||
'insurance_form': insurance_form,
|
||||
'person_form': person_form,
|
||||
'address_form': address_form,
|
||||
'stay_form': stay_form,
|
||||
'mooring_forms': mooring_forms})
|
||||
|
||||
def add_boat(request):
|
||||
if request.method == 'POST':
|
||||
boat_form = BoatForm(request.POST)
|
||||
if not boat_form.is_valid():
|
||||
return form(request)
|
||||
if request.method != 'POST':
|
||||
return form(request.POST)
|
||||
|
||||
person_form = PersonForm(request.POST)
|
||||
if not person_form.is_valid():
|
||||
return form(request)
|
||||
try:
|
||||
new_boat_form = BoatForm(request.POST, prefix='boa')
|
||||
new_boat = new_boat_form.save(commit=False)
|
||||
pprint(new_boat)
|
||||
|
||||
stay_form = StayForm(request.POST)
|
||||
if not stay_form.is_valid():
|
||||
return form(request)
|
||||
# TODO : Handle case where insurance is already existing
|
||||
# if not new_boat.boat_insurance:
|
||||
# When the boat's insurance is not specified, it means we need to
|
||||
# create a new insurance
|
||||
# new_insurance = InsuranceForm(request.POST).save(commit=False)
|
||||
#else:
|
||||
# new_insurance = None
|
||||
|
||||
if stay_form.no_mooring is not True:
|
||||
mooring_forms = MooringFormSet(request.POST)
|
||||
mooring_forms_data = mooring_forms.save(commit=False)
|
||||
for mooring_data in mooring_forms_data:
|
||||
pass
|
||||
new_insurance = InsuranceForm(request.POST, prefix='ins').save(commit=False)
|
||||
|
||||
return form(request)
|
||||
if not new_boat.company:
|
||||
# When the boat's company is not specified, it means we need to
|
||||
# create a new company
|
||||
try:
|
||||
new_company = CompanyForm(request.POST, prefix='com').save(commit=False)
|
||||
except:
|
||||
return form(request)
|
||||
else:
|
||||
new_company = None
|
||||
|
||||
new_person = PersonForm(request.POST, prefix='per').save(commit=False)
|
||||
new_address = AddressForm(request.POST, prefix='add').save(commit=False)
|
||||
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)
|
||||
for mooring in new_moorings:
|
||||
# Check if mooring is between the date of stay
|
||||
if new_stay.arrival > mooring.date:
|
||||
mooring = False
|
||||
elif new_stay.departure is not None and\
|
||||
new_stay.departure < mooring.date:
|
||||
|
||||
mooring = False
|
||||
|
||||
# Check if dock is available
|
||||
else:
|
||||
# Set the moorings later
|
||||
new_moorings = []
|
||||
|
||||
if new_company is not None:
|
||||
new_boat.company = new_company.save()
|
||||
|
||||
new_boat.save()
|
||||
|
||||
if new_insurance is not None:
|
||||
new_insurance.save()
|
||||
|
||||
# Set insurance for boat since current date
|
||||
BoatInsurance.objects.create(
|
||||
insurance = new_insurance,
|
||||
boat = new_boat
|
||||
)
|
||||
|
||||
|
||||
new_person.address = new_address.save()
|
||||
new_person.save()
|
||||
|
||||
# Say that person is sailing on boat
|
||||
SailsOn.objects.create(
|
||||
boat = new_boat,
|
||||
person = new_person,
|
||||
is_captain = False,
|
||||
is_crew = False,
|
||||
is_owner = False,
|
||||
is_guest = False,
|
||||
is_pet = True)
|
||||
|
||||
new_stay.boat = new_boat
|
||||
new_stay.save()
|
||||
|
||||
for mooring in new_moorings:
|
||||
morring.stay = new_stay
|
||||
mooring.save()
|
||||
|
||||
new_boat_form.save_m2m()
|
||||
|
||||
except ValidationError as err:
|
||||
pprint(err)
|
||||
return form(request)
|
||||
|
||||
return list_boats(request)
|
||||
|
|
Loading…
Reference in New Issue