fix upload csv placeholders

This commit is contained in:
Cayo Puigdefabregas 2022-09-12 10:55:49 +02:00
parent 560c9004ef
commit f3b03a1d46
1 changed files with 10 additions and 14 deletions

View File

@ -1,4 +1,5 @@
import copy import copy
import csv
import datetime import datetime
import json import json
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
@ -1498,24 +1499,18 @@ class UploadPlaceholderForm(FlaskForm):
_file = files[0] _file = files[0]
if _file.content_type == 'text/csv': if _file.content_type == 'text/csv':
self.source = "CSV File: {}".format(_file.filename) self.source = "CSV File: {}".format(_file.filename)
delimiter = ';' data = pd.read_csv(
data = pd.read_csv(_file).fillna('').to_dict() _file, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL
head = list(data.keys())[0].split(delimiter) )
values = [ data = data.fillna('').to_dict()
{k: v.split(delimiter)} for x in data.values() for k, v in x.items() return data
]
data = {}
for i in range(len(head)):
data[head[i]] = {}
for x in values:
for k, v in x.items():
data[head[i]][k] = v[i]
else: else:
self.source = "Excel File: {}".format(_file.filename) self.source = "Excel File: {}".format(_file.filename)
try: try:
data = pd.read_excel(_file).fillna('').to_dict() data = pd.read_excel(_file).fillna('').to_dict()
except ValueError: except ValueError:
self.placeholder_file.errors = ["File don't have a correct format"] txt = ["File don't have a correct format"]
self.placeholder_file.errors = txt
return False return False
return data return data
@ -1545,7 +1540,8 @@ class UploadPlaceholderForm(FlaskForm):
for k in header: for k in header:
if k not in data.keys(): if k not in data.keys():
self.placeholder_file.errors = ["Missing required fields in the file"] txt = ["Missing required fields in the file"]
self.placeholder_file.errors = txt
return False return False
self.placeholders = [] self.placeholders = []