Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jan 23, 2025
1 parent bb31e03 commit 16396a7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
27 changes: 18 additions & 9 deletions api/views/purchaseimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ def __init__(self, **kwargs):
self.is_duplicate_file = False
self.duplicate_purchases = []
self.duplicate_purchase_count = 0
self.data_schema = json.load(open("data/schemas/imports/achats.json"))
self.expected_header = [field["name"] for field in self.data_schema["fields"]]
self.schema_url = (
"https://raw.githubusercontent.com/betagouv/ma-cantine/refs/heads/staging/data/schemas/imports/achats.json"
)
self.schema_json = json.load(open("data/schemas/imports/achats.json"))
self.expected_header = [field["name"] for field in self.schema_json["fields"]]
super().__init__(**kwargs)

def post(self, request):
Expand All @@ -54,16 +57,22 @@ def post(self, request):
self.file_digest = super()._get_file_digest()
self._check_duplication()

self.dialect = super()._get_file_dialect()
print(self.dialect)
super()._verify_first_line_is_header()

with transaction.atomic():
self._process_file()
res = super()._send_to_validata()
print(res)
# self._process_file()

# If at least an error has been detected, we raise an error to interrupt the
# transaction and rollback the insertion of any data
if self.errors:
raise IntegrityError()
# # If at least an error has been detected, we raise an error to interrupt the
# # transaction and rollback the insertion of any data
# if self.errors:
# raise IntegrityError()

# Update all purchases's import source with file digest
Purchase.objects.filter(import_source=self.tmp_id).update(import_source=self.file_digest)
# # Update all purchases's import source with file digest
# Purchase.objects.filter(import_source=self.tmp_id).update(import_source=self.file_digest)

return self._get_success_response()

Expand Down
30 changes: 30 additions & 0 deletions api/views/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import csv
import hashlib
import io
import json
import logging

import chardet
import requests
from django.conf import settings
from django.core.exceptions import ValidationError
from djangorestframework_camel_case.render import CamelCaseJSONRenderer
Expand Down Expand Up @@ -31,6 +34,33 @@ def _get_file_digest(self):
file_hash.update(row)
return file_hash.hexdigest()

def _get_file_dialect(self):
self.file.seek(0)
row_1 = self.file.readline()
(decoded_row, _) = decode_bytes(row_1)
self.dialect = csv.Sniffer().sniff(decoded_row)

def _verify_first_line_is_header(self):
self.file.seek(0)
row_1 = self.file.readline()
(decoded_row, _) = decode_bytes(row_1)
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")

def _send_to_validata(self):
# Reset the file pointer to the beginning
self.file.seek(0)
response = requests.post(
"https://api.validata.etalab.studio/validate",
files={
"file": ("file.csv", self.file.read(), self.file.content_type),
},
data={"schema": self.schema_url, "header_case": True},
)
return response.json()["report"]


def camelize(data):
camel_case_bytes = CamelCaseJSONRenderer().render(data)
Expand Down

0 comments on commit 16396a7

Please sign in to comment.