def test_copy_organization_from_cohort(self): organization = OrganizationFactory(cohort=self.cohort) speciality = SpecialtyFactory(cohort=self.cohort) copy_cohort_name = 'Copy of {} {}'.format(self.cohort.name, timezone.now()) form_data = { 'name': copy_cohort_name, 'publication_start_date': self.cohort.publication_start_date.strftime('%Y-%m-%d'), 'subscription_start_date': self.cohort.subscription_start_date.strftime('%Y-%m-%d'), 'subscription_end_date': self.cohort.subscription_end_date.strftime('%Y-%m-%d'), 'description': self.cohort.description, 'originated_from': self.cohort.id, } form = CohortForm(data=form_data) self.assertTrue(form.is_valid()) response = self.client.post(reverse('cohort_new'), data=form_data) self.assertRedirects(response, reverse('internship')) copy_cohort = Cohort.objects.filter(name=copy_cohort_name).first() self.assertEqual(copy_cohort.name, copy_cohort_name) copy_organization = Organization.objects.filter(cohort=copy_cohort).first() copy_speciality = InternshipSpeciality.objects.filter(cohort=copy_cohort).first() self.assertIsNotNone(copy_organization) self.assertIsNotNone(copy_speciality) self.assertEqual(organization.name, copy_organization.name) self.assertEqual(speciality.name, copy_speciality.name)
def new(request): form = CohortForm(request.POST or None) errors = [] if form.is_valid(): cohort = form.save() copy_cohort.copy_from_origin(cohort) return redirect(reverse('internship')) else: errors.append(form.errors) display_errors(request, errors) context = {'form': form, 'page_title': _('Add cohort'), 'form_new': True} return render(request, 'cohort/cohort_form.html', context)
def new(request): form = CohortForm(request.POST or None) if form.is_valid(): cohort = form.save() copy_cohort.copy_from_origin(cohort) return redirect(reverse('internship')) context = { 'form': form, 'page_title': _('create_cohort'), 'form_new': True } return render(request, 'cohort/cohort_form.html', context)
def edit(request, cohort_id): cohort = get_object_or_404(Cohort, pk=cohort_id) form = CohortForm(data=request.POST or None, instance=cohort) if form.is_valid(): form.save() return redirect(reverse('internship')) context = { 'form': form, 'page_title': _('edit_cohort'), } return render(request, 'cohort/cohort_form.html', context)
def test_publication_before_subscription_closed(self): data = { 'name': self.cohort.name, 'publication_start_date': self.cohort.subscription_end_date.strftime('%Y-%m-%d'), 'subscription_start_date': self.cohort.subscription_start_date.strftime('%Y-%m-%d'), 'subscription_end_date': self.cohort.publication_start_date.strftime('%Y-%m-%d'), 'description': self.cohort.description, } form = CohortForm(data) self.assertFalse(form.is_valid())
def test_new_port(self): cohort = CohortFactory.build() form_data = { 'name': cohort.name, 'publication_start_date': cohort.publication_start_date.strftime('%Y-%m-%d'), 'subscription_start_date': cohort.subscription_start_date.strftime('%Y-%m-%d'), 'subscription_end_date': cohort.subscription_end_date.strftime('%Y-%m-%d'), 'description': cohort.description, } form = CohortForm(data=form_data) self.assertTrue(form.is_valid()) response = self.client.post(reverse('cohort_new'), data=form_data) self.assertEqual(response.status_code, 302) self.assertRedirects(response, reverse('internship'))
def test_edit_post_cohort_found(self): cohort = CohortFactory() four_weeks = datetime.timedelta(weeks=4) original_cohort = Cohort.objects.get(pk=cohort.id) original_cohort.name = 'Update {}'.format(cohort.name) original_cohort.publication_start_date += four_weeks original_cohort.subscription_start_date += four_weeks original_cohort.subscription_end_date += four_weeks form_data = { 'name': original_cohort.name, 'publication_start_date': original_cohort.publication_start_date.strftime('%Y-%m-%d'), 'subscription_start_date': original_cohort.subscription_start_date.strftime('%Y-%m-%d'), 'subscription_end_date': original_cohort.subscription_end_date.strftime('%Y-%m-%d'), 'free_internships_number': cohort.free_internships_number, 'description': cohort.description, } form = CohortForm(data=form_data) self.assertTrue(form.is_valid()) url = reverse('cohort_edit', kwargs={'cohort_id': cohort.id}) response = self.client.post(url, data=form_data) self.assertRedirects(response, reverse('internship')) cohort.refresh_from_db() self.assertEqual(cohort.name, original_cohort.name) self.assertEqual(cohort.publication_start_date, original_cohort.publication_start_date) self.assertEqual(cohort.subscription_start_date, original_cohort.subscription_start_date) self.assertEqual(cohort.subscription_end_date, original_cohort.subscription_end_date)
def edit(request, cohort_id): cohort = get_object_or_404(Cohort, pk=cohort_id) form = CohortForm(data=request.POST or None, instance=cohort) errors = [] if form.is_valid(): form.save() return redirect(reverse('internship')) else: errors.append(form.errors) display_errors(request, errors) context = { 'form': form, 'page_title': _('Edit cohort'), } return render(request, 'cohort/cohort_form.html', context)