Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff: Add and fix PTH122 #11255

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}":
Expand Down Expand Up @@ -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)}":
Expand Down
4 changes: 3 additions & 1 deletion dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions dojo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,8 @@ 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)
extension = path.suffix
# Check if threat folder exist.
if not Path(settings.MEDIA_ROOT + "/threat/").is_dir():
# Create the folder
Expand All @@ -1395,7 +1396,8 @@ def handle_uploaded_threat(f, eng):


def handle_uploaded_selenium(f, cred):
_name, extension = os.path.splitext(f.name)
path = Path(f.name)
extension = path.suffix
with open(settings.MEDIA_ROOT + f"/selenium/{cred.id}{extension}",
"wb+") as destination:
for chunk in f.chunks():
Expand Down Expand Up @@ -2699,7 +2701,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]
Expand Down
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down