mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Switch to pathlib (#3392)
* switch to pathlib * more pathlib * useconvert env to path * fix typo * use resolve instead of absolute * fix gitppod * also allow parents * replace more os operations * fix string replacement feature * make sure root dirs exsist * fix replace function * refactor duplicate code * reduce code * make sure dirs exist * fix typo * also create parent dirs * fix match statement * fix statments expecting string * return getMigrationFileNames to old behaviour * fully resolve config file * make sure comparison works * use pathlib in tasks * fix file count test * reduce code duplication in test + add test for part * fix test * re-add os * Make pathlib usage simpler
This commit is contained in:
		| @@ -3,6 +3,7 @@ | ||||
| import logging | ||||
| import os | ||||
| import shutil | ||||
| from pathlib import Path | ||||
|  | ||||
| from django.apps import AppConfig | ||||
| from django.conf import settings | ||||
| @@ -25,23 +26,21 @@ class ReportConfig(AppConfig): | ||||
|     def create_default_reports(self, model, reports): | ||||
|         """Copy defualt report files across to the media directory.""" | ||||
|         # Source directory for report templates | ||||
|         src_dir = os.path.join( | ||||
|             os.path.dirname(os.path.realpath(__file__)), | ||||
|         src_dir = Path(__file__).parent.joinpath( | ||||
|             'templates', | ||||
|             'report', | ||||
|         ) | ||||
|  | ||||
|         # Destination directory | ||||
|         dst_dir = os.path.join( | ||||
|             settings.MEDIA_ROOT, | ||||
|         dst_dir = settings.MEDIA_ROOT.joinpath( | ||||
|             'report', | ||||
|             'inventree', | ||||
|             model.getSubdir(), | ||||
|         ) | ||||
|  | ||||
|         if not os.path.exists(dst_dir): | ||||
|         if not dst_dir.exists(): | ||||
|             logger.info(f"Creating missing directory: '{dst_dir}'") | ||||
|             os.makedirs(dst_dir, exist_ok=True) | ||||
|             dst_dir.mkdir(parents=True, exist_ok=True) | ||||
|  | ||||
|         # Copy each report template across (if required) | ||||
|         for report in reports: | ||||
| @@ -54,10 +53,10 @@ class ReportConfig(AppConfig): | ||||
|                 report['file'], | ||||
|             ) | ||||
|  | ||||
|             src_file = os.path.join(src_dir, report['file']) | ||||
|             dst_file = os.path.join(settings.MEDIA_ROOT, filename) | ||||
|             src_file = src_dir.joinpath(report['file']) | ||||
|             dst_file = settings.MEDIA_ROOT.joinpath(filename) | ||||
|  | ||||
|             if not os.path.exists(dst_file): | ||||
|             if not dst_file.exists(): | ||||
|                 logger.info(f"Copying test report template '{dst_file}'") | ||||
|                 shutil.copyfile(src_file, dst_file) | ||||
|  | ||||
|   | ||||
| @@ -111,14 +111,13 @@ class ReportBase(models.Model): | ||||
|  | ||||
|         path = os.path.join('report', 'report_template', self.getSubdir(), filename) | ||||
|  | ||||
|         fullpath = os.path.join(settings.MEDIA_ROOT, path) | ||||
|         fullpath = os.path.abspath(fullpath) | ||||
|         fullpath = settings.MEDIA_ROOT.joinpath(path).resolve() | ||||
|  | ||||
|         # If the report file is the *same* filename as the one being uploaded, | ||||
|         # remove the original one from the media directory | ||||
|         if str(filename) == str(self.template): | ||||
|  | ||||
|             if os.path.exists(fullpath): | ||||
|             if fullpath.exists(): | ||||
|                 logger.info(f"Deleting existing report template: '{filename}'") | ||||
|                 os.remove(fullpath) | ||||
|  | ||||
| @@ -139,10 +138,12 @@ class ReportBase(models.Model): | ||||
|         Required for passing the file to an external process | ||||
|         """ | ||||
|         template = self.template.name | ||||
|  | ||||
|         # TODO @matmair change to using new file objects | ||||
|         template = template.replace('/', os.path.sep) | ||||
|         template = template.replace('\\', os.path.sep) | ||||
|  | ||||
|         template = os.path.join(settings.MEDIA_ROOT, template) | ||||
|         template = settings.MEDIA_ROOT.joinpath(template) | ||||
|  | ||||
|         return template | ||||
|  | ||||
| @@ -474,14 +475,13 @@ def rename_snippet(instance, filename): | ||||
|  | ||||
|     path = os.path.join('report', 'snippets', filename) | ||||
|  | ||||
|     fullpath = os.path.join(settings.MEDIA_ROOT, path) | ||||
|     fullpath = os.path.abspath(fullpath) | ||||
|     fullpath = settings.MEDIA_ROOT.joinpath(path).resolve() | ||||
|  | ||||
|     # If the snippet file is the *same* filename as the one being uploaded, | ||||
|     # delete the original one from the media directory | ||||
|     if str(filename) == str(instance.snippet): | ||||
|  | ||||
|         if os.path.exists(fullpath): | ||||
|         if fullpath.exists(): | ||||
|             logger.info(f"Deleting existing snippet file: '{filename}'") | ||||
|             os.remove(fullpath) | ||||
|  | ||||
| @@ -517,10 +517,9 @@ def rename_asset(instance, filename): | ||||
|     # If the asset file is the *same* filename as the one being uploaded, | ||||
|     # delete the original one from the media directory | ||||
|     if str(filename) == str(instance.asset): | ||||
|         fullpath = os.path.join(settings.MEDIA_ROOT, path) | ||||
|         fullpath = os.path.abspath(fullpath) | ||||
|         fullpath = settings.MEDIA_ROOT.joinpath(path).resolve() | ||||
|  | ||||
|         if os.path.exists(fullpath): | ||||
|         if fullpath.exists(): | ||||
|             logger.info(f"Deleting existing asset file: '{filename}'") | ||||
|             os.remove(fullpath) | ||||
|  | ||||
|   | ||||
| @@ -32,9 +32,9 @@ def asset(filename): | ||||
|     debug_mode = InvenTreeSetting.get_setting('REPORT_DEBUG_MODE') | ||||
|  | ||||
|     # Test if the file actually exists | ||||
|     full_path = os.path.join(settings.MEDIA_ROOT, 'report', 'assets', filename) | ||||
|     full_path = settings.MEDIA_ROOT.joinpath('report', 'assets', filename) | ||||
|  | ||||
|     if not os.path.exists(full_path) or not os.path.isfile(full_path): | ||||
|     if not full_path.exists() or not full_path.is_file(): | ||||
|         raise FileNotFoundError(f"Asset file '{filename}' does not exist") | ||||
|  | ||||
|     if debug_mode: | ||||
| @@ -63,9 +63,8 @@ def uploaded_image(filename, replace_missing=True, replacement_file='blank_image | ||||
|         exists = False | ||||
|     else: | ||||
|         try: | ||||
|             full_path = os.path.join(settings.MEDIA_ROOT, filename) | ||||
|             full_path = os.path.abspath(full_path) | ||||
|             exists = os.path.exists(full_path) and os.path.isfile(full_path) | ||||
|             full_path = settings.MEDIA_ROOT.joinpath(filename).resolve() | ||||
|             exists = full_path.exists() and full_path.is_file() | ||||
|         except Exception: | ||||
|             exists = False | ||||
|  | ||||
| @@ -85,11 +84,9 @@ def uploaded_image(filename, replace_missing=True, replacement_file='blank_image | ||||
|     else: | ||||
|         # Return file path | ||||
|         if exists: | ||||
|             path = os.path.join(settings.MEDIA_ROOT, filename) | ||||
|             path = os.path.abspath(path) | ||||
|             path = settings.MEDIA_ROOT.joinpath(filename).resolve() | ||||
|         else: | ||||
|             path = os.path.join(settings.STATIC_ROOT, 'img', replacement_file) | ||||
|             path = os.path.abspath(path) | ||||
|             path = settings.STATIC_ROOT.joinpath('img', replacement_file).resolve() | ||||
|  | ||||
|         return f"file://{path}" | ||||
|  | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
|  | ||||
| import os | ||||
| import shutil | ||||
| from pathlib import Path | ||||
|  | ||||
| from django.conf import settings | ||||
| from django.core.cache import cache | ||||
| @@ -38,12 +39,11 @@ class ReportTagTest(TestCase): | ||||
|                 report_tags.asset("bad_file.txt") | ||||
|  | ||||
|         # Create an asset file | ||||
|         asset_dir = os.path.join(settings.MEDIA_ROOT, 'report', 'assets') | ||||
|         os.makedirs(asset_dir, exist_ok=True) | ||||
|         asset_path = os.path.join(asset_dir, 'test.txt') | ||||
|         asset_dir = settings.MEDIA_ROOT.joinpath('report', 'assets') | ||||
|         asset_dir.mkdir(parents=True, exist_ok=True) | ||||
|         asset_path = asset_dir.joinpath('test.txt') | ||||
|  | ||||
|         with open(asset_path, 'w') as f: | ||||
|             f.write("dummy data") | ||||
|         asset_path.write_text("dummy data") | ||||
|  | ||||
|         self.debug_mode(True) | ||||
|         asset = report_tags.asset('test.txt') | ||||
| @@ -68,13 +68,11 @@ class ReportTagTest(TestCase): | ||||
|  | ||||
|         # Create a dummy image | ||||
|         img_path = 'part/images/' | ||||
|         img_path = os.path.join(settings.MEDIA_ROOT, img_path) | ||||
|         img_file = os.path.join(img_path, 'test.jpg') | ||||
|         img_path = settings.MEDIA_ROOT.joinpath(img_path) | ||||
|         img_file = img_path.joinpath('test.jpg') | ||||
|  | ||||
|         os.makedirs(img_path, exist_ok=True) | ||||
|  | ||||
|         with open(img_file, 'w') as f: | ||||
|             f.write("dummy data") | ||||
|         img_path.mkdir(parents=True, exist_ok=True) | ||||
|         img_file.write_text("dummy data") | ||||
|  | ||||
|         # Test in debug mode. Returns blank image as dummy file is not a valid image | ||||
|         self.debug_mode(True) | ||||
| @@ -91,7 +89,7 @@ class ReportTagTest(TestCase): | ||||
|  | ||||
|         self.debug_mode(False) | ||||
|         img = report_tags.uploaded_image('part/images/test.jpg') | ||||
|         self.assertEqual(img, f'file://{img_path}test.jpg') | ||||
|         self.assertEqual(img, f'file://{img_path.joinpath("test.jpg")}') | ||||
|  | ||||
|     def test_part_image(self): | ||||
|         """Unit tests for the 'part_image' tag""" | ||||
| @@ -178,8 +176,7 @@ class ReportTest(InvenTreeAPITestCase): | ||||
|  | ||||
|     def copyReportTemplate(self, filename, description): | ||||
|         """Copy the provided report template into the required media directory.""" | ||||
|         src_dir = os.path.join( | ||||
|             os.path.dirname(os.path.realpath(__file__)), | ||||
|         src_dir = Path(__file__).parent.joinpath( | ||||
|             'templates', | ||||
|             'report' | ||||
|         ) | ||||
| @@ -190,18 +187,15 @@ class ReportTest(InvenTreeAPITestCase): | ||||
|             self.model.getSubdir(), | ||||
|         ) | ||||
|  | ||||
|         dst_dir = os.path.join( | ||||
|             settings.MEDIA_ROOT, | ||||
|             template_dir | ||||
|         ) | ||||
|         dst_dir = settings.MEDIA_ROOT.joinpath(template_dir) | ||||
|  | ||||
|         if not os.path.exists(dst_dir):  # pragma: no cover | ||||
|             os.makedirs(dst_dir, exist_ok=True) | ||||
|         if not dst_dir.exists():  # pragma: no cover | ||||
|             dst_dir.mkdir(parents=True, exist_ok=True) | ||||
|  | ||||
|         src_file = os.path.join(src_dir, filename) | ||||
|         dst_file = os.path.join(dst_dir, filename) | ||||
|         src_file = src_dir.joinpath(filename) | ||||
|         dst_file = dst_dir.joinpath(filename) | ||||
|  | ||||
|         if not os.path.exists(dst_file):  # pragma: no cover | ||||
|         if not dst_file.exists():  # pragma: no cover | ||||
|             shutil.copyfile(src_file, dst_file) | ||||
|  | ||||
|         # Convert to an "internal" filename | ||||
|   | ||||
		Reference in New Issue
	
	Block a user