-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(API): Imports: nouvelle class CSVImportApiView générique (qui check le format, la taille max, et si déjà uploadé) #4936
base: raphodn/backend-api-filters-utils
Are you sure you want to change the base?
Changes from all commits
e2c77a8
5cf06e1
4e10319
bb31e03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
import hashlib | ||
import json | ||
import logging | ||
|
||
import chardet | ||
from django.conf import settings | ||
from django.core.exceptions import ValidationError | ||
from djangorestframework_camel_case.render import CamelCaseJSONRenderer | ||
from rest_framework.views import APIView | ||
from simple_history.utils import update_change_reason | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CSVImportApiView(APIView): | ||
def _verify_file_size(self): | ||
if self.file.size > settings.CSV_IMPORT_MAX_SIZE: | ||
raise ValidationError( | ||
f"Ce fichier est trop grand, merci d'utiliser un fichier de moins de {settings.CSV_IMPORT_MAX_SIZE_PRETTY}" | ||
) | ||
|
||
def _verify_file_format(self): | ||
if self.file.content_type not in ["text/csv", "text/tab-separated-values"]: | ||
raise ValidationError( | ||
f"Ce fichier est au format {self.file.content_type}, merci d'exporter votre fichier au format CSV et réessayer." | ||
) | ||
|
||
def _get_file_digest(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. je l'ai mis ici car j'aimerais à terme rajouter la logique "fichier identique ?" pour les imports cantines/diagnostics. enfin je ne suis pas 100% certain de sa faisabilité, en tout cas on ne stock pas le md5 actuellement pour ces imports, mais ca serait utile pour tous les futurs ! |
||
file_hash = hashlib.md5() | ||
for row in self.file: | ||
file_hash.update(row) | ||
return file_hash.hexdigest() | ||
|
||
|
||
def camelize(data): | ||
camel_case_bytes = CamelCaseJSONRenderer().render(data) | ||
return json.loads(camel_case_bytes.decode("utf-8")) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pour info, dans ce commentaire - #4896 (comment) - je me pose la question si c'est une classe DRF qu'on doit utiliser, ou plutôt un simple fichier utils python avec les différentes méthodes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parce que ces appels "super()", et l'utilisation un peu magique de "self.", ce n'est pas très très joli je trouve ^^