class EmployeeCreate(GenerateMixin, TemplateView): template_name = 'administrator/employee/employee_create_update.html' def get(self, request, *args, **kwargs): self.form_employee = EmployeeForm(auto_id='employee_%s') self.form_employee_relative = BloodRelativeFormSet( prefix='employee_relative') self.form_employee_eps = EpsFormSet( prefix='employee_eps') self.form_employee_experience = ExperienceFormSet( prefix='employee_experience') self.form_employee_training = TrainingFormSet( prefix='employee_training') self.form_employee_police = PoliceFormSet( prefix='employee_police') self.form_employee_criminal = CriminalFormSet( prefix='employee_criminal') self.form_employee_psycho_physical = PsychoPhysicalFormSet( prefix='employee_psycho-physical') self.form_employee_judiciary = JudiciaryFormSet( prefix='employee_judiciary') return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): operating_unit = OperatingUnit.objects.get(pk=1) self.form_employee = EmployeeForm(request.POST, auto_id='employee_%s') self.form_employee_relative = BloodRelativeFormSet( request.POST, prefix='employee_relative') self.form_employee_eps = EpsFormSet( request.POST, prefix='employee_eps') self.form_employee_experience = ExperienceFormSet( request.POST, prefix='employee_experience') self.form_employee_training = TrainingFormSet( request.POST, prefix='employee_training') self.form_employee_police = PoliceFormSet( request.POST, prefix='employee_police') self.form_employee_criminal = CriminalFormSet( request.POST, prefix='employee_criminal') self.form_employee_psycho_physical = PsychoPhysicalFormSet( request.POST, prefix='employee_psycho-physical') self.form_employee_judiciary = JudiciaryFormSet( request.POST, prefix='employee_judiciary') if (self.form_employee.is_valid() and self.form_employee_relative.is_valid() and self.form_employee_eps.is_valid() and self.form_employee_experience.is_valid() and self.form_employee_training.is_valid() and self.form_employee_police.is_valid() and self.form_employee_criminal.is_valid() and self.form_employee_psycho_physical.is_valid() and self.form_employee_judiciary.is_valid()): with transaction.atomic(): new_code = Correlative.genera_correlative( operating_unit, constants.CORRELATIVO_TRABAJADOR_PORTUARIO) employee = self.form_employee.save( operating_unit=operating_unit, code=new_code, commit=False) self.get_formset_employee(self.form_employee_relative, employee) self.get_formset_employee(self.form_employee_eps, employee) self.get_formset_employee(self.form_employee_experience, employee) self.get_formset_employee(self.form_employee_training, employee) self.get_formset_employee(self.form_employee_police, employee) self.get_formset_employee(self.form_employee_criminal, employee) self.get_formset_employee(self.form_employee_psycho_physical, employee) self.get_formset_employee(self.form_employee_judiciary, employee) return self.render_to_response(self.get_context_data()) @staticmethod def get_formset_employee(formset, employee): for form in formset.forms: form.save(employee=employee, commit=False) def get_context_data(self, **kwargs): context = super(EmployeeCreate, self).get_context_data(**kwargs) context['nav'] = ( ('Panel Mando', '/'), ('Trabajadores', reverse_lazy('adm_app:employee_app:employee')), ('Registro', '') ) self.form_employee.fields['tipo'].initial = self.request.GET.get('tipo') self.form_employee.fields['tipo'].widget.attrs.update({'disabled': 'true'}) context['employee'] = self.form_employee context['employee_relative'] = self.form_employee_relative context['employee_eps'] = self.form_employee_eps context['employee_experience'] = self.form_employee_experience context['employee_training'] = self.form_employee_training context['employee_police'] = self.form_employee_police context['employee_criminal'] = self.form_employee_criminal context['employee_psycho_physical'] = self.form_employee_psycho_physical context['employee_judiciary'] = self.form_employee_judiciary return context
class EmployeeUpdate(GenerateMixin, UpdateView): model = Employee template_name = 'administrator/employee/employee_create_update.html' success_url = reverse_lazy('adm_app:employee_app:employee') def get(self, request, *args, **kwargs): self.object = self.get_object() employee = Employee.objects.get(pk=self.object.id) self.form_employee = EmployeeForm( instance=employee, auto_id='employee_%s') self.form_employee_relative = BloodRelativeFormSet( instance=employee, prefix='employee_relative') self.form_employee_eps = EpsFormSet( instance=employee, prefix='employee_eps') self.form_employee_experience = ExperienceFormSet( instance=employee, prefix='employee_experience') self.form_employee_training = TrainingFormSet( instance=employee, prefix='employee_training') self.form_employee_police = PoliceFormSet( instance=employee, prefix='employee_police') self.form_employee_criminal = CriminalFormSet( instance=employee, prefix='employee_criminal') self.form_employee_psycho_physical = PsychoPhysicalFormSet( instance=employee, prefix='employee_psycho-physical') self.form_employee_judiciary = JudiciaryFormSet( instance=employee, prefix='employee_judiciary') return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): self.object = self.get_object() employee = Employee.objects.get(pk=self.object.id) self.form_employee = EmployeeForm( request.POST, auto_id='employee_%s', instance=employee) self.form_employee_relative = BloodRelativeFormSet( request.POST, prefix='employee_relative', instance=employee) self.form_employee_eps = EpsFormSet( request.POST, prefix='employee_eps', instance=employee) self.form_employee_experience = ExperienceFormSet( request.POST, prefix='employee_experience', instance=employee) self.form_employee_training = TrainingFormSet( request.POST, prefix='employee_training', instance=employee) self.form_employee_police = PoliceFormSet( request.POST, prefix='employee_police', instance=employee) self.form_employee_criminal = CriminalFormSet( request.POST, prefix='employee_criminal', instance=employee) self.form_employee_psycho_physical = PsychoPhysicalFormSet( request.POST, prefix='employee_psycho-physical', instance=employee) self.form_employee_judiciary = JudiciaryFormSet( request.POST, prefix='employee_judiciary', instance=employee) if (self.form_employee.is_valid() and self.form_employee_relative.is_valid() and self.form_employee_eps.is_valid() and self.form_employee_experience.is_valid() and self.form_employee_training.is_valid() and self.form_employee_police.is_valid() and self.form_employee_criminal.is_valid() and self.form_employee_psycho_physical.is_valid() and self.form_employee_judiciary.is_valid()): with transaction.atomic(): employee = self.form_employee.save() self.get_formset_employee(self.form_employee_relative, employee) self.get_formset_employee(self.form_employee_eps, employee) self.get_formset_employee(self.form_employee_experience, employee) self.get_formset_employee(self.form_employee_training, employee) self.get_formset_employee(self.form_employee_police, employee) self.get_formset_employee(self.form_employee_criminal, employee) self.get_formset_employee(self.form_employee_psycho_physical, employee) self.get_formset_employee(self.form_employee_judiciary, employee) return self.render_to_response(self.get_context_data()) @staticmethod def get_formset_employee(formset, employee): for form in formset.forms: form.save(employee=employee, commit=False) def get_context_data(self, **kwargs): context = super(EmployeeUpdate, self).get_context_data(**kwargs) context['nav'] = ( ('Panel Mando', '/'), ('Trabajadores', reverse_lazy('adm_app:employee_app:employee')), ('Actualización', '') ) context['employee'] = self.form_employee context['employee_relative'] = self.form_employee_relative context['employee_eps'] = self.form_employee_eps context['employee_experience'] = self.form_employee_experience context['employee_training'] = self.form_employee_training context['employee_police'] = self.form_employee_police context['employee_criminal'] = self.form_employee_criminal context['employee_psycho_physical'] = self.form_employee_psycho_physical context['employee_judiciary'] = self.form_employee_judiciary return context