def form_valid(self, form): tally_id = self.kwargs.get('tally_id', None) self.initial = { 'tally_id': tally_id, } tally = Tally.objects.get(id=tally_id) result_form = ResultForm.objects.create( barcode=form.data['barcode'], form_state=form.data['form_state'], tally=tally) self.request.session['result_form'] = result_form.pk post_data = self.request.POST.copy() post_data['result_form'] = self.request.session['result_form'] pk = session_matches_post_result_form(post_data, self.request) result_form = ResultForm.objects.get(pk=pk) if result_form.center or result_form.station_number\ or result_form.ballot or result_form.office: # We are writing a form we should not be, bail out. del self.request.session['result_form'] return redirect(self.success_url, tally_id=tally_id) form = CreateResultForm(post_data, instance=result_form, initial=self.initial) form.save() self.success_message = _( u"Successfully Created form %(form)s" % {'form': form.data['barcode']}) return super(CreateResultFormView, self).form_valid(form)
def test_create_result_form_valid(self): tally = create_tally() tally.users.add(self.user) code = '12345' barcode = '12345' ballot = create_ballot(tally=tally) sc, _ = SubConstituency.objects.get_or_create(code=1, field_office='1') center = create_center(code, tally=tally, sub_constituency=sc) station = create_station(center) form_data = { 'center': center.pk, 'station_number': station.station_number, 'tally': tally.pk, 'form_state': 9, 'ballot': ballot.pk, 'barcode': barcode, 'created_user': self.request.user.userprofile, 'gender': 1 } form = CreateResultForm(form_data) self.assertTrue(form.is_valid()) self.assertEqual(form.instance.barcode, barcode) form.save() self.assertEqual( ResultForm.objects.get(id=form.instance.id).barcode, barcode)
def test_create_result_form_station_not_active_error(self): tally = create_tally() tally.users.add(self.user) code = '12345' center = create_center(code, tally=tally) ballot = create_ballot(tally=tally) station = create_station(center, active=False) form_data = { 'center': center.pk, 'station_number': station.station_number, 'tally': tally.pk, 'form_state': 9, 'ballot': ballot.pk, 'barcode': 12345, 'created_user': self.request.user.userprofile, 'gender': 1 } form = CreateResultForm(form_data) self.assertIn("Selected station is disabled", form.errors['__all__']) self.assertFalse(form.is_valid())
def test_create_result_form_station_does_not_exist_error(self): tally = create_tally() tally.users.add(self.user) code = '12345' station_number = 1 center = create_center(code, tally=tally) ballot = create_ballot(tally=tally) form_data = { 'center': center.pk, 'station_number': station_number, 'tally': tally.pk, 'form_state': 9, 'ballot': ballot.pk, 'barcode': 12345, 'created_user': self.request.user.userprofile, 'gender': 1 } form = CreateResultForm(form_data) self.assertIn("Station does not exist for the selected center", form.errors['__all__']) self.assertFalse(form.is_valid())
def test_create_result_form_ballot_number_mis_match_error(self): tally = create_tally() tally.users.add(self.user) code = '12345' ballot = create_ballot(tally=tally, number=2) sc, _ = SubConstituency.objects.get_or_create(code=1, field_office='1') center = create_center(code, tally=tally, sub_constituency=sc) station = create_station(center) form_data = { 'center': center.pk, 'station_number': station.station_number, 'tally': tally.pk, 'form_state': 9, 'ballot': ballot.pk, 'barcode': 12345, 'created_user': self.request.user.userprofile, 'gender': 1 } form = CreateResultForm(form_data) self.assertIn("Ballot number do not match for center and station", form.errors['__all__']) self.assertFalse(form.is_valid())
def test_create_result_form_mandatory_fields(self): form_data = {} form = CreateResultForm(form_data) self.assertIn("All fields are mandatory", form.errors['__all__']) self.assertFalse(form.is_valid())