def test_edit_post_formation_found(self):
        address = AddressFactory()
        ed = EducationGroupFactory()
        EducationGroupYearFactory(education_group=ed)
        cet = ContinuingEducationTrainingFactory(postal_address=None,
                                                 education_group=ed)
        cet_dict = model_to_dict(cet)
        cet_dict['training_aid'] = not cet.training_aid
        cet_dict['active'] = not cet.active
        cet_dict['postal_address'] = model_to_dict(address)

        form = ContinuingEducationTrainingForm(cet_dict)
        form.is_valid()
        url = reverse('formation_edit', args=[cet.pk])
        response = self.client.post(url, data=form.cleaned_data)

        self.assertRedirects(
            response, reverse('formation_detail',
                              args=[cet.education_group.id]))
        cet.refresh_from_db()

        # verifying that fields are correctly updated
        for key in form.cleaned_data.keys():
            field_value = cet.__getattribute__(key)
            self.assertEqual(field_value, cet_dict[key])
def formation_edit(request, formation_id):
    formation = get_object_or_404(ContinuingEducationTraining, pk=formation_id)
    if _can_edit_formation(request, formation):
        form = ContinuingEducationTrainingForm(request.POST or None,
                                               user=request.user,
                                               instance=formation)
        address_form = AddressForm(request.POST or None,
                                   instance=formation.postal_address)
        if all([form.is_valid(), address_form.is_valid()]):
            address = address_form.save()
            formation = form.save(commit=False)
            formation.postal_address = address
            formation.save()
            return redirect(
                reverse('formation_detail',
                        kwargs={'formation_id': formation.education_group.id}))
        return render(request, "formation_form.html", {
            'formation': formation,
            'form': form,
            'address_form': address_form
        })
    else:
        display_error_messages(
            request, _("You are not authorized to edit this training"))
        return redirect(
            reverse('formation_detail',
                    kwargs={'formation_id': formation.education_group.id}))
 def test_valid_form_for_continuing_education_managers(self):
     self.manager = ContinuingEducationManagerFactory()
     self.client.force_login(self.manager.person.user)
     data = self.formation.__dict__
     data['formation'] = self.formation.pk
     form = ContinuingEducationTrainingForm(data=data, user=self.manager.person.user)
     self.assertTrue(form.is_valid(), form.errors)
예제 #4
0
def formation_edit(request, formation_id):
    formation = get_object_or_404(
        ContinuingEducationTraining.objects.select_related('postal_address', 'education_group'),
        pk=formation_id
    )
    form = ContinuingEducationTrainingForm(request.POST or None, user=request.user, instance=formation)
    address_form = AddressForm(request.POST or None, instance=formation.postal_address)
    if all([form.is_valid(), address_form.is_valid()]):
        address = address_form.save()
        formation = form.save(commit=False)
        formation.postal_address = address
        formation.save()
        return redirect(reverse('formation_detail', kwargs={'formation_id': formation.education_group.id}))
    return render(
        request,
        "formation_form.html",
        {
            'formation': formation,
            'form': form,
            'address_form': address_form
        }
    )