53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
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):
|
|
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)
|