Merge pull request #337 from eReuse/bugfix/3749-csv-placeholders

fix upload csv placeholders
This commit is contained in:
cayop 2022-09-12 11:22:08 +02:00 committed by GitHub
commit 39b68fae87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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 = []