Beispiel #1
0
def edit(request, pk):
    student = get_object_or_404(Student, pk=pk)
    if request.method == "POST":
        form_student = StudentForm(request.POST, instance=student)
        if form_student.is_valid():
            student = form_student.save(commit=False)
            student.save()
            return redirect('students:detail', student.pk)
    else:
        form_student = StudentForm(instance=student)
    return render(request, 'management/students/student_edit.html', {'form_student': form_student})
Beispiel #2
0
def create(request):
    form_student = StudentForm(request.POST)
    form_address = AddressForm(request.POST)
    form_contact = ContactForm(request.POST)
    form_matriculation = MatriculationForm(request.POST)
    form_responsible = ResponsibleForm(request.POST)
    form_responsible_student = ResponsibleStudentForm(request.POST)
    form_tuition_fee = TuitionFeeForm(request.POST)

    if not form_student.is_valid() or not form_address.is_valid() or not form_contact.is_valid() \
            or not form_matriculation.is_valid() or not form_responsible.is_valid() \
            or not form_responsible_student.is_valid() or not form_tuition_fee.is_valid():
        return render(request, 'management/students/student_edit.html',
                      {'form_student': form_student,
                       'form_address': form_address,
                       'form_contact': form_contact,
                       'form_matriculation': form_matriculation,
                       'form_responsible': form_responsible,
                       'form_responsible_student': form_responsible_student,
                       'form_tuition_fee': form_tuition_fee})

    form_student.cleaned_data['cpf'] = _remove_mask_field(form_student.cleaned_data['cpf'])
    form_contact.cleaned_data['cell_phone'] = _remove_mask_field(form_contact.cleaned_data['cell_phone'])
    form_address.cleaned_data['cep'] = _remove_mask_field(form_address.cleaned_data['cep'])

    address = Address.objects.create(**form_address.cleaned_data)
    contact = Contact.objects.create(**form_contact.cleaned_data)
    student = Student.objects.create(**form_student.cleaned_data)
    responsible = Responsible.objects.create(**form_responsible.cleaned_data)
    student.address = address
    student.contact = contact
    student.save()

    matriculation = Matriculation.objects.create(**form_matriculation.cleaned_data)
    tuition_fee = TuitionFee.objects.create(**form_tuition_fee.cleaned_data)
    matriculation.student = student
    matriculation.tuition_fee = tuition_fee
    matriculation.save()

    responsible_student = ResponsibleStudent.objects.create(**form_responsible.cleaned_data)
    responsible_student.student = student
    responsible_student.responsible = responsible
    responsible_student.save()

    return redirect('students:detail', student.pk)