def test_kml_upload_form_with_with_valid_data(self): """ Test the import kml form. This test uses a sample kmz file with one placemark. This code based on stack overflow question at http://stackoverflow.com/questions/7304248/writing-tests-for-forms-in-django :return: """ upload_file = open('west_turkana/fixtures/MLP_test.kmz', 'rb') post_dict = {} file_dict = {'kmlfileUpload': SimpleUploadedFile(upload_file.name, upload_file.read())} upload_file.close() form = UploadKMLForm(post_dict, file_dict) self.assertTrue(form.is_valid()) # get current number of occurrence records in DB and verify count occurrence_starting_record_count = Occurrence.objects.count() # follow redirect to confirmation page response = self.client.post('/projects/west_turkana/upload/', file_dict, follow=True) # test redirect to confirmation page self.assertRedirects(response, '/projects/west_turkana/confirmation/') self.assertEqual(response.status_code, 200) self.assertContains(response, 'upload was successful!') # test message in conf page # test that new occurrence was added to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count+1)
def test_kml_form_with_no_data(self): # get starting count of records in DB occurrence_starting_record_count = Occurrence.objects.count() # create an empty form post_dict = {} file_dict = {} form = UploadKMLForm(post_dict, file_dict) # test that form is not valid with empty data self.assertFalse(form.is_valid()) # get the post response and test page reload and error message response = self.client.post(reverse('projects:west_turkana:west_turkana_upload_kml'), file_dict, follow=True) self.assertEqual(response.status_code, 200) # test reload self.assertContains(response, 'Upload a') self.assertContains(response, 'This field is required') # test nothing is saved to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count)
def import_kml(request): # Handle file upload if request.method == 'POST': kml = Kml(file=request.FILES['file'], visibility=False) kml.save() # Redirect to the document list after POST return HttpResponseRedirect('/VYD/KmlManager/kmls') else: form = UploadKMLForm() # A empty, unbound form # Render list page with the documents and the form return render_to_response( 'import_kml.html', {'form': form}, )
def form(self): return UploadKMLForm(self.request.form)
def upload_kml_form(self): return UploadKMLForm()