def setUp(self): self.list_content = [] self.import_name = 'example_input.tab' + str(now())[:19] with open('example_input.tab') as data: header = False for d in data: if not header: header = True else: upload_file = UploadFile() line = unicodedata.normalize('NFKD', d).encode('ASCII', 'ignore').decode('utf-8').lower().split('\t') upload_file.unpack_line(tuple(line), self.import_name) self.list_content.append(upload_file)
def setUp(self): self.list_content = [] self.import_name = 'example_input.tab' + str(now())[:19] with open('example_input.tab') as data: header = False for d in data: if not header: header = True else: upload_file = UploadFile() line = unicodedata.normalize('NFKD', d).encode( 'ASCII', 'ignore').decode('utf-8').lower().split('\t') upload_file.unpack_line(tuple(line), self.import_name) self.list_content.append(upload_file)
def save_file(request): form = UploadForm(request.POST, request.FILES) if not form.is_valid(): return render(request, 'upload.html', {'form': UploadForm()}) data = request.FILES['file'] file_name = data.name import_name = file_name + str(now())[:19] data = data.read().decode('utf-8').split('\n') list_content_file = [] for line in data[1:]: reader = csv.DictReader(StringIO(line), delimiter='\t', quoting=csv.QUOTE_NONE) fields = tuple(unicodedata.normalize('NFKD', r) .encode('ASCII', 'ignore') .decode('utf-8').lower() for r in reader.fieldnames) upload_file = UploadFile() upload_file.unpack_line(fields, import_name) list_content_file.append(upload_file) UploadFile.bulk_insert(list_content_file) #calcula receita bruta value_billing = billing_file(import_name) return render(request, 'upload.html', {'form': form, 'receita': value_billing, 'file_name': file_name})
def test_create(self): UploadFile.bulk_insert(self.list_content) self.assertEqual( 4, len(UploadFile.objects.filter(import_name=self.import_name)))
def test_create(self): UploadFile.bulk_insert(self.list_content) self.assertEqual(4, len(UploadFile.objects.filter(import_name=self.import_name)))