예제 #1
0
def upload(request):
    upload_form = UploadLocationsForm()
    if request.method == 'POST':
        upload_form = UploadLocationsForm(request.POST, request.FILES)
        if upload_form.is_valid():
            upload_task.delay(upload_form)
            messages.warning(request, "Upload in progress. This could take a while.")
            return HttpResponseRedirect('/locations/upload/')
        messages.error(request, 'Locations not uploaded. %s'%upload_form.errors['__all__'].as_text().replace('*',''))

    context = {'button_label': 'Save', 'id': 'upload-locations-form',
             'country_name': '', 'upload_form': upload_form,'range':range(3)}

    details = LocationTypeDetails.objects.all()
    if not details.exists():
        messages.error(request, "No location hierarchy added yet.")
        return render(request, 'location_hierarchy/upload.html', context)

    country_with_location_details_objects = details[0].country
    context = {'button_label': 'Save', 'id': 'upload-locations-form',
             'country_name': country_with_location_details_objects.name, 'upload_form': upload_form,'range':range(3)}
    location_types = LocationTypeDetails.objects.order_by('order') #get_ordered_types().exclude(name__iexact='country')
    if location_types.exists():
        context.update({'location_types_details':location_types})
    return render(request, 'location_hierarchy/upload.html', context)
    def test_valid(self):
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())
    def test_invalid_if_location_type_details_not_found(self):
        LocationTypeDetails.objects.all().delete()
        data_file={'file': SimpleUploadedFile(self.filename, self.file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn('Location type details for Region not found.', upload_location_form.non_field_errors())
    def test_invalid_if_location_type_details_not_found(self):
        LocationTypeDetails.objects.all().delete()
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())
예제 #5
0
    def test_invalid_if_location_type_details_not_found(self):
        LocationTypeDetails.objects.all().delete()
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn('Location type details for Region not found.',
                      upload_location_form.non_field_errors())
    def test_invalid_if_headers_are_not_in_order(self):
        unordered_data = [['DistrictName', 'CountyName', 'RegionName'],
                          ['district1', 'county1', 'region1'],
                          ['district2', 'county2', 'region2']]
        self.write_to_csv('wb', unordered_data, csvfilename='some_file.csv')
        file = open('some_file.csv', 'rb')
        data_file={'file': SimpleUploadedFile(self.filename, file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn('Location types not in order. Please refer to input file format.', upload_location_form.non_field_errors())
    def test_invalid_if_has_code_is_checked_but_no_type_code_column(self):
        district = LocationTypeDetails.objects.get(location_type=self.district, required=True)
        district.has_code = True
        district.length_of_code = 6
        district.save()
        data_file={'file': SimpleUploadedFile(self.filename, self.file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn('DistrictCode column should be before DistrictName column. Please refer to input file format.',
                      upload_location_form.non_field_errors())
    def test_invalid_if_has_code_is_checked_but_no_type_code_column(self):
        district = LocationTypeDetails.objects.get(location_type=self.district,
                                                   required=True)
        district.has_code = True
        district.length_of_code = 6
        district.save()
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())
    def test_invalid_if_not_has_code_but_code_still_supplied(self):
        data = [['RegionName', 'DistrictCode', 'DistrictName', 'CountyName'],
                     ['region1', '001', 'district1', 'county1'],
                     ['region2', '002','district2', 'county2']]

        self.write_to_csv('wb', data)
        file = open('test.csv', 'rb')
        data_file={'file': SimpleUploadedFile(self.filename, self.file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn('District has no code. The column DistrictCode should be removed. Please refer to input file format.',
                      upload_location_form.non_field_errors())
    def test_invalid_if_headers_are_not_in_order(self):
        unordered_data = [['DistrictName', 'CountyName', 'RegionName'],
                          ['district1', 'county1', 'region1'],
                          ['district2', 'county2', 'region2']]
        self.write_to_csv('wb', unordered_data, csvfilename='some_file.csv')
        file = open('some_file.csv', 'rb')
        data_file = {'file': SimpleUploadedFile(self.filename, file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn(
            'Location types not in order. Please refer to input file format.',
            upload_location_form.non_field_errors())
    def test_invalid_if_not_has_code_but_code_still_supplied(self):
        data = [['RegionName', 'DistrictCode', 'DistrictName', 'CountyName'],
                ['region1', '001', 'district1', 'county1'],
                ['region2', '002', 'district2', 'county2']]

        self.write_to_csv('wb', data)
        file = open('test.csv', 'rb')
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())
예제 #12
0
    def test_invalid_if_has_code_is_checked_but_no_type_code_column(self):
        district = LocationTypeDetails.objects.get(location_type=self.district,
                                                   required=True)
        district.has_code = True
        district.length_of_code = 6
        district.save()
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn(
            'DistrictCode column should be before DistrictName column. Please refer to input file format.',
            upload_location_form.non_field_errors())
    def test_valid_with_has_code(self):
        district = LocationTypeDetails.objects.get(location_type=self.district, required=True)
        district.has_code = True
        district.length_of_code = 3
        district.save()

        data = [['RegionName', 'DistrictCode', 'DistrictName', 'CountyName'],
                     ['region1', '001', 'district1', 'county1'],
                     ['region2', '003','district2', 'county2']]

        self.write_to_csv('wb', data)
        file = open('test.csv', 'rb')
        data_file={'file': SimpleUploadedFile(self.filename, file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())
예제 #14
0
    def test_invalid_if_not_has_code_but_code_still_supplied(self):
        data = [['RegionName', 'DistrictCode', 'DistrictName', 'CountyName'],
                ['region1', '001', 'district1', 'county1'],
                ['region2', '002', 'district2', 'county2']]

        self.write_to_csv('wb', data)
        file = open('test.csv', 'rb')
        data_file = {
            'file': SimpleUploadedFile(self.filename, self.file.read())
        }

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(False, upload_location_form.is_valid())
        self.assertIn(
            'District has no code. The column DistrictCode should be removed. Please refer to input file format.',
            upload_location_form.non_field_errors())
    def test_valid_with_has_code(self):
        district = LocationTypeDetails.objects.get(location_type=self.district,
                                                   required=True)
        district.has_code = True
        district.length_of_code = 3
        district.save()

        data = [['RegionName', 'DistrictCode', 'DistrictName', 'CountyName'],
                ['region1', '001', 'district1', 'county1'],
                ['region2', '003', 'district2', 'county2']]

        self.write_to_csv('wb', data)
        file = open('test.csv', 'rb')
        data_file = {'file': SimpleUploadedFile(self.filename, file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())
예제 #16
0
def upload(request):
    upload_form = UploadLocationsForm()
    if request.method == 'POST':
        upload_form = UploadLocationsForm(request.POST, request.FILES)
        if upload_form.is_valid():
            upload_task.delay(upload_form)
            messages.warning(request,
                             "Upload in progress. This could take a while.")
            return HttpResponseRedirect('/locations/upload/')
        messages.error(
            request, 'Locations not uploaded. %s' %
            upload_form.errors['__all__'].as_text().replace('*', ''))

    context = {
        'button_label': 'Save',
        'id': 'upload-locations-form',
        'country_name': '',
        'upload_form': upload_form,
        'range': range(3)
    }

    details = LocationTypeDetails.objects.all()
    if not details.exists():
        messages.error(request, "No location hierarchy added yet.")
        return render(request, 'location_hierarchy/upload.html', context)

    country_with_location_details_objects = details[0].country
    context = {
        'button_label': 'Save',
        'id': 'upload-locations-form',
        'country_name': country_with_location_details_objects.name,
        'upload_form': upload_form,
        'range': range(3)
    }
    location_types = LocationTypeDetails.objects.order_by(
        'order')  #get_ordered_types().exclude(name__iexact='country')
    if location_types.exists():
        context.update({'location_types_details': location_types})
    return render(request, 'location_hierarchy/upload.html', context)
    def test_valid(self):
        data_file={'file': SimpleUploadedFile(self.filename, self.file.read())}

        upload_location_form = UploadLocationsForm({}, data_file)

        self.assertEqual(True, upload_location_form.is_valid())