Skip to content

Commit

Permalink
Plus besoin de gérer le header
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jan 30, 2025
1 parent 9a82b53 commit 17f5f79
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions api/views/purchaseimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def post(self, request):
logger.info("Purchase bulk import started")
try:
self.file = request.data["file"]

# Step 1: Format validation
file_import.validate_file_size(self.file)
file_import.validate_file_format(self.file)

Expand All @@ -63,10 +65,13 @@ def post(self, request):
self.dialect = file_import.get_csv_file_dialect(self.file)
file_import.verify_first_line_is_header(self.file, self.dialect, self.expected_header)

# Step 2: Schema validation (Validata)
report = validate_file_against_schema(self.file, self.schema_url)
self.errors = process_errors(report)
print(self.errors)
if len(self.errors):
return self._get_success_response()

# Step 3: ma-cantine validation (permissions, last checks...) + import
with transaction.atomic():
self._process_file()

Expand Down Expand Up @@ -112,28 +117,16 @@ def _log_error(self, message, level="warning"):

def _process_file(self):
chunk = []
read_header = True
row_count = 1
row_count = 0
for row in self.file:
# Sniffing 1st line
if read_header:
# decode header, discarding encoding result that might not be accurate without more data
(decoded_row, _) = file_import.decode_bytes(row)
csvreader = csv.reader(io.StringIO("".join(decoded_row)), self.dialect)
for header in csvreader:
if header != self.expected_header:
raise ValidationError("La première ligne du fichier doit contenir les bon noms de colonnes")
read_header = False
# Split into chunks
chunk.append(row)

# Process full chunk
if row_count == settings.CSV_PURCHASE_CHUNK_LINES:
self._process_chunk(chunk)
chunk = []
row_count = 0
else:
# Split into chunks
chunk.append(row)

# Process full chunk
if row_count == settings.CSV_PURCHASE_CHUNK_LINES:
self._process_chunk(chunk)
chunk = []
row_count = 0
row_count += 1

# Process the last chunk
Expand Down

0 comments on commit 17f5f79

Please sign in to comment.