|
19 | 19 |
|
20 | 20 | from common.util import get_mime_type
|
21 | 21 | from common.util import parse_xml
|
| 22 | +from common.validators import validate_filename |
| 23 | +from common.validators import validate_filepath |
22 | 24 | from importer.chunker import chunk_taric
|
23 | 25 | from importer.management.commands.run_import_batch import run_batch
|
24 | 26 | from importer.models import ImportBatch
|
@@ -74,13 +76,8 @@ def clean_taric_file(self):
|
74 | 76 | if mime_type not in ["text/xml", "application/xml"]:
|
75 | 77 | raise ValidationError("The selected file must be XML")
|
76 | 78 |
|
77 |
| - secure_file_name = secure_filename(uploaded_taric_file.name) |
78 |
| - if uploaded_taric_file.name.replace(" ", "_") == secure_file_name: |
79 |
| - uploaded_taric_file.name == secure_file_name |
80 |
| - else: |
81 |
| - raise ValidationError( |
82 |
| - "File name must only include alphanumeric characters and special characters such as spaces, hyphens and underscores.", |
83 |
| - ) |
| 79 | + validate_filename(uploaded_taric_file.name) |
| 80 | + validate_filepath(uploaded_taric_file) |
84 | 81 |
|
85 | 82 | try:
|
86 | 83 | xml_file = parse_xml(uploaded_taric_file)
|
@@ -154,13 +151,8 @@ def clean_taric_file(self):
|
154 | 151 | if mime_type not in ["text/xml", "application/xml"]:
|
155 | 152 | raise ValidationError("The selected file must be XML")
|
156 | 153 |
|
157 |
| - secure_file_name = secure_filename(uploaded_taric_file.name) |
158 |
| - if uploaded_taric_file.name.replace(" ", "_") == secure_file_name: |
159 |
| - uploaded_taric_file.name == secure_file_name |
160 |
| - else: |
161 |
| - raise ValidationError( |
162 |
| - "File name must only include alphanumeric characters and special characters such as spaces, hyphens and underscores.", |
163 |
| - ) |
| 154 | + validate_filename(uploaded_taric_file.name) |
| 155 | + validate_filepath(uploaded_taric_file) |
164 | 156 |
|
165 | 157 | try:
|
166 | 158 | xml_file = parse_xml(uploaded_taric_file)
|
@@ -352,19 +344,20 @@ def save(self):
|
352 | 344 | make sense to use commit=False. process_file() should be moved into the
|
353 | 345 | view if this (common) behaviour becomes required.
|
354 | 346 | """
|
355 |
| - file_name = os.path.splitext(self.cleaned_data["name"])[0] |
356 |
| - description = f"TARIC {file_name} commodity code changes" |
| 347 | + taric_filename = secure_filename(self.cleaned_data["name"]) |
| 348 | + file_id = os.path.splitext(taric_filename)[0] |
| 349 | + description = f"TARIC {file_id} commodity code changes" |
357 | 350 |
|
358 | 351 | import_batch = ImportBatch(
|
359 | 352 | author=self.request.user,
|
360 |
| - name=self.cleaned_data["name"], |
| 353 | + name=taric_filename, |
361 | 354 | goods_import=True,
|
362 | 355 | )
|
363 | 356 |
|
364 | 357 | self.files["taric_file"].seek(0, os.SEEK_SET)
|
365 | 358 | import_batch.taric_file.save(
|
366 |
| - self.files["taric_file"].name, |
367 |
| - ContentFile(self.files["taric_file"].read()), |
| 359 | + name=taric_filename, |
| 360 | + content=ContentFile(self.files["taric_file"].read()), |
368 | 361 | )
|
369 | 362 |
|
370 | 363 | import_batch.save()
|
|
0 commit comments