diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-26 17:27:41 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-06-13 18:08:06 +0200 |
commit | 08542121281f492511a2724e8f2c98b6c0c77d8f (patch) | |
tree | 02c8c796a2dfabaf9ee018470198ee9255a4fe56 | |
parent | 510b9de8fc74807723d73985a84d494218886db9 (diff) | |
download | Ishtar-08542121281f492511a2724e8f2c98b6c0c77d8f.tar.bz2 Ishtar-08542121281f492511a2724e8f2c98b6c0c77d8f.zip |
🐛 fix unclosed file
-rw-r--r-- | archaeological_context_records/tests.py | 20 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 78 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 73 | ||||
-rw-r--r-- | ishtar_common/data_importer.py | 18 | ||||
-rw-r--r-- | ishtar_common/forms_common.py | 2 | ||||
-rw-r--r-- | ishtar_common/models_imports.py | 10 | ||||
-rw-r--r-- | ishtar_common/tasks.py | 14 | ||||
-rw-r--r-- | ishtar_common/tests.py | 142 | ||||
-rw-r--r-- | ishtar_common/utils.py | 3 | ||||
-rw-r--r-- | ishtar_common/utils_migrations.py | 14 |
10 files changed, 192 insertions, 182 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 1dec09ca4..d1b377e76 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -87,10 +87,10 @@ class ImportContextRecordTest(ImportTest, TestCase): filename = root + "importer-GIS-UE.zip" self.restore_serialized(filename) imp_type = ImporterType.objects.get(name="GIS - UE") - imp_file = open(root + data_name, "rb") - file_dict = { - "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) - } + with open(root + data_name, "rb") as imp_file: + file_dict = { + "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) + } post_dict = { "importer_type": imp_type.pk, "name": "cr_geo_import", @@ -1888,7 +1888,8 @@ class GraphGenerationTest(ContextRecordInit, TestCase): self.assertIsNotNone(cr_2.relation_image) self.assertIsNotNone(cr_2.relation_bitmap_image) self.assertIsNotNone(cr_2.relation_dot) - content = open(cr_2.relation_dot.path).read() + with open(cr_2.relation_dot.path) as f: + content = f.read() self.assertIn('"CR 1"', content) self.assertIn('"CR 1B"', content) self.assertIn('"CR 2B"', content) @@ -1906,7 +1907,8 @@ class GraphGenerationTest(ContextRecordInit, TestCase): self.assertIsNotNone(operation.relation_image) self.assertIsNotNone(operation.relation_bitmap_image) self.assertIsNotNone(operation.relation_dot) - content = open(operation.relation_dot.path).read() + with open(operation.relation_dot.path) as f: + content = f.read() self.assertIn('"CR 1"', content) self.assertIn('"CR 1B"', content) self.assertIn('"CR 2B"', content) @@ -1923,7 +1925,8 @@ class GraphGenerationTest(ContextRecordInit, TestCase): self.assertIsNotNone(cr_2.relation_image_above) self.assertIsNotNone(cr_2.relation_bitmap_image_above) self.assertIsNotNone(cr_2.relation_dot_above) - content = open(cr_2.relation_dot_above.path).read() + with open(cr_2.relation_dot_above.path) as f: + content = f.read() self.assertNotIn('"CR 1"', content) self.assertNotIn('"CR 1B"', content) self.assertIn('"CR 2B"', content) @@ -1940,7 +1943,8 @@ class GraphGenerationTest(ContextRecordInit, TestCase): self.assertIsNotNone(cr_2.relation_image_below) self.assertIsNotNone(cr_2.relation_bitmap_image_below) self.assertIsNotNone(cr_2.relation_dot_below) - content = open(cr_2.relation_dot_below.path).read() + with open(cr_2.relation_dot_below.path) as f: + content = f.read() self.assertIn('"CR 1"', content) self.assertIn('"CR 1B"', content) self.assertIn('"CR 2B"', content) diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index c36327bae..b121bd04c 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -655,10 +655,10 @@ class ImportFindTest(BaseImportFindTest): filename = root + data_name + ".zip" self.restore_serialized(filename) imp_type = ImporterType.objects.get(slug="topographie-mobilier") - imp_file = open(root + data_name + ".csv", "rb") - file_dict = { - "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) - } + with open(root + data_name + ".csv", "rb") as imp_file: + file_dict = { + "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) + } post_dict = { "importer_type": imp_type.pk, "name": "find_geo_import", @@ -710,7 +710,7 @@ class ImportFindTest(BaseImportFindTest): def test_group_import(self): imp_group, imp_file, imp_media = self.get_group_import() file_dict = { - "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) + "imported_file": imp_file } post_dict = { "importer_type": imp_group.pk, @@ -723,8 +723,9 @@ class ImportFindTest(BaseImportFindTest): data=post_dict, files=file_dict, user=self.user ) self.assertFalse(form.is_valid()) - self.assertIn(str(_("This importer need a document archive.")), form.errors["__all__"]) - file_dict["imported_images"] = SimpleUploadedFile(imp_media.name, imp_media.read()) + self.assertIn(str(_("This importer need a document archive.")), + form.errors["__all__"]) + file_dict["imported_images"] = imp_media form = forms_common.NewImportGroupForm( data=post_dict, files=file_dict, user=self.user ) @@ -736,7 +737,6 @@ class ImportFindTest(BaseImportFindTest): nb_find = models.Find.objects.count() nb_container = Container.objects.count() nb_docs = Document.objects.count() - impt.importation() self.assertEqual(models.BaseFind.objects.count(), nb_base_find + 1) @@ -757,17 +757,19 @@ class ImportFindTest(BaseImportFindTest): ImportTarget.objects.create( target="documents__image", formater_type_id=formater.pk, column_id=col.pk ) - mcc_file = open( - settings.LIB_BASE_PATH + "archaeological_finds/tests/MCC-finds-example.csv", - "rb", - ) - mcc_images = open( - settings.LIB_BASE_PATH + "archaeological_finds/tests/images.zip", "rb" - ) - file_dict = { - "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()), - "imported_images": SimpleUploadedFile(mcc_images.name, mcc_images.read()), - } + file_dict = {} + with open( + settings.LIB_BASE_PATH + + "archaeological_finds/tests/MCC-finds-example.csv", "rb") as mcc_file: + file_dict["imported_file"] = SimpleUploadedFile( + mcc_file.name, mcc_file.read() + ) + with open( + settings.LIB_BASE_PATH + + "archaeological_finds/tests/images.zip", "rb") as mcc_file: + file_dict["imported_images"] = SimpleUploadedFile( + mcc_file.name, mcc_file.read() + ) post_dict = { "importer_type": MCC.pk, "skip_lines": 1, @@ -904,10 +906,10 @@ class ImportFindTest(BaseImportFindTest): ] ) - imp_file = open(imp_filename, "rb") - file_dict = { - "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) - } + with open(imp_filename, "rb") as imp_file: + file_dict = { + "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) + } post_dict = { "importer_type": importer_type.pk, "skip_lines": 1, @@ -1063,13 +1065,13 @@ class ImportFindLiveServerTest(LiveServerTestCase, BaseImportFindTest): ImportTarget.objects.create( target="documents__image", formater_type_id=formater.pk, column_id=col.pk ) - mcc_file = open( - settings.LIB_BASE_PATH + "archaeological_finds/tests/MCC-finds-example.csv", - "rb", - ) - file_dict = { - "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()), - } + with open( + settings.LIB_BASE_PATH + + "archaeological_finds/tests/MCC-finds-example.csv", + "rb") as mcc_file: + file_dict = { + "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()), + } post_dict = { "importer_type": MCC.pk, "skip_lines": 1, @@ -1754,12 +1756,13 @@ class FindSearchTest(FindInit, TestCase, SearchText): image_path = ( settings.LIB_BASE_PATH + "ishtar_common/static/media/images/ishtar-bg.jpg" ) - document.image = SimpleUploadedFile( - name="ishtar-bg.jpg", - content=open(image_path, "rb").read(), - content_type="image/jpeg", - ) - document.save() + with open(image_path, "rb") as content: + document.image = SimpleUploadedFile( + name="ishtar-bg.jpg", + content=content.read(), + content_type="image/jpeg", + ) + document.save() self.finds[0].documents.add(document) self.finds[0].save() @@ -2791,7 +2794,8 @@ class TreatmentTest(FindInit, TestCase): self.create_finds(data_base={"label": "Find 1"}, force=True) self.create_finds(data_base={"label": "Find 2"}, force=True) image = Document.objects.create(title="Image!") - image.image.save("ishtar-bg.jpg", File(open(img, "rb"))) + with open(img, "rb") as cimg: + image.image.save("ishtar-bg.jpg", File(cimg)) self.finds[0].documents.add(image) self.finds[0].save() diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 425a762f7..8ba63a94b 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -177,14 +177,14 @@ class ImportTest(BaseImportTest): def init_ope_import(self, filename="MCC-operations-example.csv", sep=","): mcc_operation = ImporterType.objects.get(name="MCC - Opérations") - mcc_operation_file = open( - settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, "rb" - ) - file_dict = { - "imported_file": SimpleUploadedFile( - mcc_operation_file.name, mcc_operation_file.read() - ) - } + with open( + settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, + "rb") as mcc_operation_file: + file_dict = { + "imported_file": SimpleUploadedFile( + mcc_operation_file.name, mcc_operation_file.read() + ) + } group, c = TargetKeyGroup.objects.get_or_create(name="My group") post_dict = { "importer_type": mcc_operation.pk, @@ -285,14 +285,13 @@ class ImportTest(BaseImportTest): def init_parcel_import(self): self.init_ope() mcc_parcel = ImporterType.objects.get(name="MCC - Parcelles") - mcc_file = open( - settings.LIB_BASE_PATH + - "archaeological_operations/tests/MCC-parcelles-example.csv", - "rb", - ) - file_dict = { - "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) - } + with open( + settings.LIB_BASE_PATH + + "archaeological_operations/tests/MCC-parcelles-example.csv", + "rb") as mcc_file: + file_dict = { + "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) + } post_dict = { "importer_type": mcc_parcel.pk, "skip_lines": 1, @@ -315,14 +314,12 @@ class ImportTest(BaseImportTest): def init_context_record_import(self): self.init_parcel() mcc = ImporterType.objects.get(name="MCC - UE") - mcc_file = open( - settings.LIB_BASE_PATH + "archaeological_context_records/tests/" - "MCC-context-records-example.csv", - "rb", - ) - file_dict = { - "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) - } + with open( + settings.LIB_BASE_PATH + "archaeological_context_records/tests/" + "MCC-context-records-example.csv", "rb") as mcc_file: + file_dict = { + "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) + } post_dict = { "importer_type": mcc.pk, "skip_lines": 1, @@ -812,15 +809,14 @@ class ImportDocumentTest(ImportTest, TestCase): target="title", formater_type_id=formater.pk, column_id=col.pk ) - doc_import_file = open( - settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, "rb" - ) - - file_dict = { - "imported_file": SimpleUploadedFile( - doc_import_file.name, doc_import_file.read() - ) - } + with open( + settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, + "rb") as doc_import_file: + file_dict = { + "imported_file": SimpleUploadedFile( + doc_import_file.name, doc_import_file.read() + ) + } group, c = TargetKeyGroup.objects.get_or_create(name="My group") post_dict = { @@ -4067,12 +4063,11 @@ class RegisterTest(TestCase, OperationInitTest): self.assertEqual(json.loads(response.content.decode())["recordsTotal"], 1) def test_document_generation(self): - tpl = open( - settings.LIB_BASE_PATH + - "archaeological_operations/tests/document_reference.odt", - "rb", - ) - template = SimpleUploadedFile(tpl.name, tpl.read()) + with open( + settings.LIB_BASE_PATH + + "archaeological_operations/tests/document_reference.odt", + "rb") as tpl: + template = SimpleUploadedFile(tpl.name, tpl.read()) model, __ = ImporterModel.objects.get_or_create( klass="archaeological_operations.models.AdministrativeAct" ) diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index cf54544fe..941ccc4af 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -647,7 +647,6 @@ class FileFormater(Formater): return my_file def _format_zip(self, value, archive): - zp = zipfile.ZipFile(archive) value = value.strip().replace("\\", "/") items = value.replace("/", "_").split(".") base_dir = settings.MEDIA_ROOT + "imported" @@ -656,14 +655,15 @@ class FileFormater(Formater): filename = base_dir + os.sep + ".".join(items[:-1]) + "." + items[-1] try: - with open(filename, "wb") as f: - with zp.open(value) as z: - f.write(z.read()) - f = open(filename, "rb") - my_file = File(f) - # manually set the file size because of an issue with TempFile - my_file.size = os.stat(filename).st_size - return my_file + with zipfile.ZipFile(archive) as zp: + with open(filename, "wb") as f: + with zp.open(value) as z: + f.write(z.read()) + f = open(filename, "rb") + my_file = File(f) + # manually set the file size because of an issue with TempFile + my_file.size = os.stat(filename).st_size + return my_file except KeyError: raise ValueError( _('"%(value)s" is not a valid path for the given archive') diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 2f8af87ab..ae4d925b1 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -443,6 +443,8 @@ class NewImportForm(BaseImportForm): # media is downloaded - clean the link item.imported_media_link = None item.save() + if hasattr(temp_file, "close"): + temp_file.close() return item diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 4639c79ef..0b87350d0 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -1890,11 +1890,13 @@ class ImportGroup(BaseImport): imports = [] imported_file, imported_images = None, None if self.imported_file: - imported_file = ContentFile(self.imported_file.read()) - imported_file.name = self.imported_file.name.split(os.sep)[-1] + with open(self.imported_file.path, "rb") as imp: + imported_file = ContentFile(imp.read()) + imported_file.name = self.imported_file.name.split(os.sep)[-1] if self.imported_images: - imported_images = ContentFile(self.imported_images.read()) - imported_images.name = self.imported_images.name.split(os.sep)[-1] + with open(self.imported_images.path, "rb") as imp: + imported_images = ContentFile(imp.read()) + imported_images.name = self.imported_images.name.split(os.sep)[-1] for import_type_relation in self.importer_type.importer_types.all(): importer_type = import_type_relation.importer_type diff --git a/ishtar_common/tasks.py b/ishtar_common/tasks.py index 55c704ab1..24ae02dcc 100644 --- a/ishtar_common/tasks.py +++ b/ishtar_common/tasks.py @@ -173,13 +173,13 @@ def launch_export(export_task_id): if not export_task.geo: kwargs["no_geo"] = True archive_name = full_serialization(**kwargs) - result = open(archive_name, "rb") - export_task.result.save(archive_name.split(os.sep)[-1], File(result)) - os.remove(archive_name) - export_task.finished_date = datetime.datetime.now() - export_task.state = "F" - export_task.result_info = str(_("Export finished")) - export_task.save() + with open(archive_name, "rb") as result: + export_task.result.save(archive_name.split(os.sep)[-1], File(result)) + os.remove(archive_name) + export_task.finished_date = datetime.datetime.now() + export_task.state = "F" + export_task.result_info = str(_("Export finished")) + export_task.save() def update_towns(): diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 7d60deab8..b5e228852 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -721,17 +721,18 @@ class GenericSerializationTest: ) self.documents = [] for idx in range(12): - self.documents.append( - models.Document.objects.create( - title="Test{}".format(idx), - associated_file=SimpleUploadedFile("test.txt", b"no real content"), - image=SimpleUploadedFile( - name="test.png", - content=open(image_path, "rb").read(), - content_type="image/png", - ), + with open(image_path, "rb") as img: + self.documents.append( + models.Document.objects.create( + title="Test{}".format(idx), + associated_file=SimpleUploadedFile("test.txt", b"no real content"), + image=SimpleUploadedFile( + name="test.png", + content=img.read(), + content_type="image/png", + ), + ) ) - ) def generic_serialization_test(self, serialize, no_test=False, kwargs=None): if not kwargs: @@ -2707,8 +2708,10 @@ class BaseImportTest(TestCase): importer_filename = os.path.join(root, "importer-group.zip") restore_serialized(importer_filename) imp_group = models.ImporterGroup.objects.get(slug="chantier-des-depots") - imp_file = open(os.path.join(root, "importer-group.csv"), "rb") - imp_media = open(os.path.join(root, "importer-group-media.zip"), "rb") + with open(os.path.join(root, "importer-group.csv"), "rb") as imp: + imp_file = SimpleUploadedFile(imp.name, imp.read()) + with open(os.path.join(root, "importer-group-media.zip"), "rb") as imp: + imp_media = SimpleUploadedFile(imp.name, imp.read()) return imp_group, imp_file, imp_media def create_group_import(self, init=True): @@ -2716,8 +2719,8 @@ class BaseImportTest(TestCase): create_user() ishtar_user = models.IshtarUser.objects.all()[0] file_dict = { - "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()), - "imported_images": SimpleUploadedFile(imp_media.name, imp_media.read()) + "imported_file": imp_file, + "imported_images": imp_media } post_dict = { "importer_type": imp_group.pk, @@ -2879,13 +2882,12 @@ class ImportTestInterface(BaseImportTest): def _test_create_import_get_data(self): csv_path = os.path.join(LIB_BASE_PATH, "ishtar_common", "tests", "insee-test.csv") + with open(csv_path, "rb") as f: + imported_file = SimpleUploadedFile(name="insee-test.csv", content=f.read()) return { "name": "Test Name", "importer_type": self.importer_type.pk, - "imported_file": SimpleUploadedFile( - name="insee-test.csv", - content=open(csv_path, "rb").read(), - ), + "imported_file": imported_file, "encoding": "utf-8", "csv_sep": '|', "skip_lines": 1, @@ -3030,17 +3032,16 @@ class ImportTestInterface(BaseImportTest): def test_validation_zip_import_image(self): # init image_path = os.path.join(LIB_BASE_PATH, "ishtar_common", "tests", "test.png") + with open(image_path, "rb") as f: + imported_images = SimpleUploadedFile(name="test.png", content=f.read(), + content_type="image/png") data = { "name": "Import Zip Not Valid Must Fail", "importer_type": self.importer_type.pk, "encoding": "utf-8", "csv_sep": "|", "skip_lines": 1, - "imported_images": SimpleUploadedFile( - name="test.png", - content=open(image_path, "rb").read(), - content_type="image/png", - ), + "imported_images": imported_images, } # superuser @@ -3101,9 +3102,15 @@ class ImportTestInterface(BaseImportTest): path = os.path.join( LIB_BASE_PATH, "ishtar_common", "tests", "error-file.csv" ) - imprt.error_file = SimpleUploadedFile(name="error-file.csv", content=open(path, "rb").read(), content_type="text/csv") + with open(path, "rb") as f: + error_file = SimpleUploadedFile(name="error-file.csv", content=f.read(), + content_type="text/csv") + imprt.error_file = error_file imprt.save() - imprt2.error_file = SimpleUploadedFile(name="error-file.csv", content=open(path, "rb").read(), content_type="text/csv") + with open(path, "rb") as f: + error_file = SimpleUploadedFile(name="error-file.csv", content=f.read(), + content_type="text/csv") + imprt2.error_file = error_file imprt2.save() q = models.ImportLineError.objects.filter(import_item=imprt.pk) @@ -3249,27 +3256,27 @@ class ImportTest(BaseImportTest): SUB_IMPORT_MATCH_IDX = [1, 2] with tempfile.TemporaryDirectory() as tmpdir: - current_zip = zipfile.ZipFile(group_import.archive_file.path, "r") - name_list = current_zip.namelist() - self.assertIn("content.json", name_list) - current_zip.extract("content.json", tmpdir) - content_name = os.path.join(tmpdir, "content.json") - with open(content_name, "r") as content: - files = json.loads(content.read()) - self.assertIn("imported_file", files.keys()) - self.assertIn(files["imported_file"], name_list) - for idx in range(4): - self.assertIn(f"sub-{idx}-result_file", files.keys()) - self.assertIn(files[f"sub-{idx}-result_file"], name_list) - for idx in SUB_IMPORT_MATCH_IDX: - self.assertIn(f"sub-{idx}-match_file", files.keys()) - self.assertIn(files[f"sub-{idx}-match_file"], name_list) - for name in name_list: - current_zip.extract(name, tmpdir) - if name == files["imported_file"]: - with open(os.path.join(tmpdir, name), "r") as f: - result = f.read() - self.assertEqual(result, csv_content) + with zipfile.ZipFile(group_import.archive_file.path, "r") as current_zip: + name_list = current_zip.namelist() + self.assertIn("content.json", name_list) + current_zip.extract("content.json", tmpdir) + content_name = os.path.join(tmpdir, "content.json") + with open(content_name, "r") as content: + files = json.loads(content.read()) + self.assertIn("imported_file", files.keys()) + self.assertIn(files["imported_file"], name_list) + for idx in range(4): + self.assertIn(f"sub-{idx}-result_file", files.keys()) + self.assertIn(files[f"sub-{idx}-result_file"], name_list) + for idx in SUB_IMPORT_MATCH_IDX: + self.assertIn(f"sub-{idx}-match_file", files.keys()) + self.assertIn(files[f"sub-{idx}-match_file"], name_list) + for name in name_list: + current_zip.extract(name, tmpdir) + if name == files["imported_file"]: + with open(os.path.join(tmpdir, name), "r") as f: + result = f.read() + self.assertEqual(result, csv_content) group_import.unarchive("F") group_import = models.ImportGroup.objects.get(pk=group_import.pk) @@ -4627,14 +4634,13 @@ class StorageTest(TestCase): image_path = os.path.join( LIB_BASE_PATH, "ishtar_common", "tests", "test.png" ) + with open(image_path, "rb") as f: + image = SimpleUploadedFile(name="test.png", content=f.read(), + content_type="image/png") doc = models.Document.objects.create( source_type=self.st1, title="Operation report", - image=SimpleUploadedFile( - name="test.png", - content=open(image_path, "rb").read(), - content_type="image/png", - ), + image=image, ) p = doc.image.path.split(os.sep) # current save path @@ -4644,25 +4650,17 @@ class StorageTest(TestCase): if f.startswith("test"): os.remove(os.path.join(base_path, f)) doc = models.Document.objects.get(pk=doc.pk) - doc.image.save( - "test.png", - SimpleUploadedFile( - name="test.png", - content=open(image_path, "rb").read(), - content_type="image/png", - ), - ) + with open(image_path, "rb") as f: + image = SimpleUploadedFile(name="test.png", content=f.read(), + content_type="image/png") + doc.image.save("test.png", image) doc = models.Document.objects.get(pk=doc.pk) os.remove(doc.image.path) os.symlink("/tmp/ZZZZZZZZZZZZZZZ", doc.image.path) # bad link - doc.image.save( - "test.png", - SimpleUploadedFile( - name="test.png", - content=open(image_path, "rb").read(), - content_type="image/png", - ), - ) + with open(image_path, "rb") as f: + image = SimpleUploadedFile(name="test.png", content=f.read(), + content_type="image/png") + doc.image.save("test.png", image) doc.save() @@ -4851,13 +4849,15 @@ class DocumentTest(TestCase): pdf_path = os.path.join( LIB_BASE_PATH, "ishtar_common", "tests", "simple.pdf" ) - doc = models.Document.objects.create( - title="Document", - associated_file=SimpleUploadedFile( + with open(pdf_path, "rb") as f: + associated_file = SimpleUploadedFile( name="simple.pdf", - content=open(pdf_path, "rb").read(), + content=f.read(), content_type="application/pdf", ) + doc = models.Document.objects.create( + title="Document", + associated_file=associated_file ) doc.operations.add(self.ope1) doc = models.Document.objects.get(id=doc.pk) diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 7f0808602..11e107c01 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -2243,7 +2243,8 @@ def generate_relation_graph( png_name = tempdir + os.path.sep + "relations.png" with open(png_name, "wb") as png_file: - svg2png(open(svg_tmp_name, "rb").read(), write_to=png_file) + with open(svg_tmp_name, "rb") as svg_tmp: + svg2png(svg_tmp.read(), write_to=png_file) with open(png_name, "rb") as png_file: django_file = File(png_file) attr = "relation_bitmap_image" + suffix diff --git a/ishtar_common/utils_migrations.py b/ishtar_common/utils_migrations.py index f11428e3c..5654a8897 100644 --- a/ishtar_common/utils_migrations.py +++ b/ishtar_common/utils_migrations.py @@ -28,12 +28,14 @@ def migrate_simple_image_to_m2m(base_model, image_model, rel_model, verbose=Fals image_instance = image_model.objects.create() try: - image_instance.image.save( - os.path.basename(item.image.path), File(open(item.image.path)) - ) - image_instance.thumbnail.save( - os.path.basename(item.thumbnail.path), File(open(item.thumbnail.path)) - ) + with open(item.image.path) as fle: + image_instance.image.save( + os.path.basename(item.image.path), File(fle) + ) + with open(item.thumbnail.path) as fle: + image_instance.thumbnail.save( + os.path.basename(item.thumbnail.path), File(fle) + ) except IOError: # image not on hard-drive item.image = None |