def test_update_or_create_income_and_wealth(self):
     data = list(utils.read_csv(self.correct_file))
     self.assertTrue(len(data) > 0)
     utils.update_or_create_income_and_wealth(data)
     obj_count_on_db = IncomeWealth.objects.count()
     # should be len(data) - 1, because first raw contains column names
     self.assertTrue(obj_count_on_db == len(data) - 1)
 def test_read_csv_success(self):
     seq = utils.read_csv(self.correct_file)
     data = list(seq)
     # assert at least one row exists
     self.assertTrue(len(data) >= 1)
     # assert each row has same column size
     self.assertTrue(
         next(iter(set([len(d) for d in data]))) == len(data[0]))
Example #3
0
def load_initial_data(apps, schema_editor):
    csv_dump_file_path = os.path.join(settings.BASE_DIR,
                                      settings.INIT_CSV_DUMP_FILE_REL_PATH)

    with open(csv_dump_file_path) as f:
        data = read_csv(f)
        update_or_create_income_and_wealth(data)
        logger.info('DONE.')
Example #4
0
    def clean_csv_file(self):
        f = self.cleaned_data['csv_file']

        if f.content_type not in ['text/csv']:
            raise forms.ValidationError('The file type is not accepted.')

        try:
            # try was parsed or not, use deepcopy to keep original stream
            next(read_csv(copy.deepcopy(f)))
        except csv.Error:
            raise forms.ValidationError('The file is broken or not csv.')

        return f
Example #5
0
def upload_income_and_wealth_csv(request):
    """
    example: /api/v1/upload-income-and-wealth-csv/
    and use "csv_file" field while uploading the file
    """
    # necessary validation was made in Form implementation
    form = CsvFileForm(request.POST, request.FILES)
    if not form.is_valid():
        return {'error': form.errors.get('csv_file')[0]}, 400

    f = request.FILES['csv_file']
    # get list of list - it is like row-columns
    data = read_csv(f)
    # update or insert to database
    update_or_create_income_and_wealth(data)

    # 201 should be returned according to REST spec.
    return {'status': True}, 201
 def test_read_csv_error(self):
     data = None
     try:
         data = list(utils.read_csv(self.wrong_file))
     except Exception, e:
         self.assertTrue(isinstance(e, csv.Error))