def post(self, *args, **kwargs): questionnaire = Questionnaire.objects.get(id=self.kwargs['questionnaire_id']) form = PublishQuestionnaireForm(initial={'questionnaire': questionnaire}, data=self.request.POST) if form.is_valid(): form.save() message = "The questionnaire has been published to %s" % ", ".join([region.name for region in form.cleaned_data['regions']]) messages.success(self.request, message) return HttpResponseRedirect(reverse('manage_jrf_page')) else: message = "Questionnaire could not be published see errors below" messages.error(self.request, message) context = {'questionnaire': questionnaire, 'publish_form': form, 'btn_label': "Publish", 'cancel_url': reverse('manage_jrf_page')} return render(self.request, self.template_name, context)
def test_creates_copies_for_regions_on_save(self): Questionnaire.objects.create(name="JRF 2013 Core English", status=Questionnaire.PUBLISHED, year=2013, region=self.afro) pacific = Region.objects.create(name="haha", organization=self.who) asia = Region.objects.create(name="hehe", organization=self.who) data = {'questionnaire': self.questionnaire, 'regions': [self.paho.id, pacific.id, asia.id]} publish_questionnaire_form = PublishQuestionnaireForm(initial={'questionnaire': self.questionnaire}, data=data) self.assertTrue(publish_questionnaire_form.is_valid()) publish_questionnaire_form.save() questionnaires = Questionnaire.objects.filter(year=self.questionnaire.year) self.assertEqual(5, questionnaires.count()) [self.assertEqual(1, region.questionnaire.all().count()) for region in [self.paho, pacific, asia]] self.assertEqual(1, self.afro.questionnaire.all().count()) questionnaire = Questionnaire.objects.filter(id=self.questionnaire.id)[0] self.assertEqual(questionnaire.status, Questionnaire.PUBLISHED)