From af03b1d7d8a83461a2c3eb3f638471d081d19130 Mon Sep 17 00:00:00 2001 From: Manuel Sommer Date: Wed, 13 Nov 2024 15:47:41 +0100 Subject: [PATCH 1/3] Ruff: Add and fix PTH122 --- dojo/forms.py | 7 +++++-- dojo/models.py | 4 +++- dojo/utils.py | 12 +++++++++--- ruff.toml | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/dojo/forms.py b/dojo/forms.py index 3577a9ff2ab..d56cd1ebad2 100644 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -4,6 +4,7 @@ import re import warnings from datetime import date, datetime +from pathlib import Path import tagulous from crispy_forms.bootstrap import InlineCheckboxes, InlineRadios @@ -754,7 +755,8 @@ class UploadThreatForm(forms.Form): def clean(self): if (file := self.cleaned_data.get("file", None)) is not None: - ext = os.path.splitext(file.name)[1] # [0] returns path+filename + path = Path(file.name) + ext = path.suffix valid_extensions = [".jpg", ".png", ".pdf"] if ext.lower() not in valid_extensions: if accepted_extensions := f"{', '.join(valid_extensions)}": @@ -872,7 +874,8 @@ def clean(self): for form in self.forms: file = form.cleaned_data.get("file", None) if file: - ext = os.path.splitext(file.name)[1] # [0] returns path+filename + path = Path(file.name) + ext = path.suffix valid_extensions = settings.FILE_UPLOAD_TYPES if ext.lower() not in valid_extensions: if accepted_extensions := f"{', '.join(valid_extensions)}": diff --git a/dojo/models.py b/dojo/models.py index 037b22b919f..61bd6622fc6 100644 --- a/dojo/models.py +++ b/dojo/models.py @@ -141,7 +141,9 @@ def __init__(self, directory=None, keep_basename=False, keep_ext=True): self.keep_ext = keep_ext def __call__(self, model_instance, filename): - base, ext = os.path.splitext(filename) + path = Path(filename) + base = path.parent / path.stem + ext = path.suffix filename = f"{base}_{uuid4()}" if self.keep_basename else str(uuid4()) if self.keep_ext: filename += ext diff --git a/dojo/utils.py b/dojo/utils.py index d30ef1ea63c..eba7b0bf291 100644 --- a/dojo/utils.py +++ b/dojo/utils.py @@ -1381,7 +1381,9 @@ def get_page_items_and_count(request, items, page_size, prefix="", do_count=True def handle_uploaded_threat(f, eng): - _name, extension = os.path.splitext(f.name) + path = Path(f.name) + _name = path.parent / path.stem + extension = path.suffix # Check if threat folder exist. if not Path(settings.MEDIA_ROOT + "/threat/").is_dir(): # Create the folder @@ -1395,7 +1397,9 @@ def handle_uploaded_threat(f, eng): def handle_uploaded_selenium(f, cred): - _name, extension = os.path.splitext(f.name) + path = Path(f.name) + _name = path.parent / path.stem + extension = path.suffix with open(settings.MEDIA_ROOT + f"/selenium/{cred.id}{extension}", "wb+") as destination: for chunk in f.chunks(): @@ -2699,7 +2703,9 @@ def generate_file_response_from_file_path( ) -> FileResponse: """Serve an local file in a uniformed way.""" # Determine the file path - file_path_without_extension, file_extension = os.path.splitext(file_path) + path = Path(file_path) + file_path_without_extension = path.parent / path.stem + file_extension = path.suffix # Determine the file name if not supplied if file_name is None: file_name = file_path_without_extension.rsplit("/")[-1] diff --git a/ruff.toml b/ruff.toml index c78256a9d2e..cface3f73fa 100644 --- a/ruff.toml +++ b/ruff.toml @@ -65,7 +65,7 @@ select = [ "TCH", "INT", "ARG003", "ARG004", "ARG005", - "PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH124", + "PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH122", "PTH124", "TD001", "TD004", "TD005", "PD", "PGH", From b28d75cf81bdb879062a258c04782bc474a55a8b Mon Sep 17 00:00:00 2001 From: manuelsommer <47991713+manuel-sommer@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:10:50 +0100 Subject: [PATCH 2/3] Update dojo/utils.py Co-authored-by: Charles Neill <1749665+cneill@users.noreply.github.com> --- dojo/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dojo/utils.py b/dojo/utils.py index eba7b0bf291..e99d40989b1 100644 --- a/dojo/utils.py +++ b/dojo/utils.py @@ -1398,7 +1398,6 @@ def handle_uploaded_threat(f, eng): def handle_uploaded_selenium(f, cred): path = Path(f.name) - _name = path.parent / path.stem extension = path.suffix with open(settings.MEDIA_ROOT + f"/selenium/{cred.id}{extension}", "wb+") as destination: From 5334988e8525d3bcc2c0bb209104992934430641 Mon Sep 17 00:00:00 2001 From: manuelsommer <47991713+manuel-sommer@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:11:07 +0100 Subject: [PATCH 3/3] Update dojo/utils.py Co-authored-by: Charles Neill <1749665+cneill@users.noreply.github.com> --- dojo/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dojo/utils.py b/dojo/utils.py index e99d40989b1..c19da8945be 100644 --- a/dojo/utils.py +++ b/dojo/utils.py @@ -1382,7 +1382,6 @@ def get_page_items_and_count(request, items, page_size, prefix="", do_count=True def handle_uploaded_threat(f, eng): path = Path(f.name) - _name = path.parent / path.stem extension = path.suffix # Check if threat folder exist. if not Path(settings.MEDIA_ROOT + "/threat/").is_dir():