2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-02 03:30:54 +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:
Matthias Mair
2022-07-27 02:42:34 +02:00
committed by GitHub
parent 551f66ff90
commit 794e375009
20 changed files with 242 additions and 403 deletions

View File

@ -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