Skip to content

Commit

Permalink
Ruff: Fix issues via "target-version"
Browse files Browse the repository at this point in the history
  • Loading branch information
kiblik committed Sep 16, 2024
1 parent d4f097d commit 4f162af
Show file tree
Hide file tree
Showing 47 changed files with 222 additions and 234 deletions.
5 changes: 2 additions & 3 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import re
from datetime import datetime
from typing import List

import six
import tagulous
Expand Down Expand Up @@ -1522,7 +1521,7 @@ def get_engagement(self, obj):
)

def validate(self, data):
def validate_findings_have_same_engagement(finding_objects: List[Finding]):
def validate_findings_have_same_engagement(finding_objects: list[Finding]):
engagements = finding_objects.values_list("test__engagement__id", flat=True).distinct().count()
if engagements > 1:
msg = "You are not permitted to add findings from multiple engagements"
Expand Down Expand Up @@ -2048,7 +2047,7 @@ def get_findings_count(self, obj) -> int:
return obj.findings_count

# TODO: maybe extend_schema_field is needed here?
def get_findings_list(self, obj) -> List[int]:
def get_findings_list(self, obj) -> list[int]:
return obj.open_findings_list


Expand Down
2 changes: 1 addition & 1 deletion dojo/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ def get_model_fields(default_fields, extra_fields=()):
def get_model_default_fields(model):
return tuple(
field.name for field in model._meta.fields if
isinstance(field, (models.CharField, models.TextField))
isinstance(field, models.CharField | models.TextField)
)
30 changes: 14 additions & 16 deletions dojo/engagement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from functools import reduce
from tempfile import NamedTemporaryFile
from time import strftime
from typing import List, Optional, Tuple

from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -433,7 +432,7 @@ def get_risks_accepted(self, eng):
def get_filtered_tests(
self,
request: HttpRequest,
queryset: List[Test],
queryset: list[Test],
engagement: Engagement,
):
filter_string_matching = get_system_setting("filter_string_matching", False)
Expand Down Expand Up @@ -718,9 +717,9 @@ def get_development_environment(
def get_engagement_or_product(
self,
user: Dojo_User,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
) -> Tuple[Engagement, Product, Product | Engagement]:
engagement_id: int | None = None,
product_id: int | None = None,
) -> tuple[Engagement, Product, Product | Engagement]:
"""
Using the path parameters, either fetch the product or engagement
"""
Expand Down Expand Up @@ -783,7 +782,7 @@ def get_jira_form(
self,
request: HttpRequest,
engagement_or_product: Engagement | Product,
) -> Tuple[JIRAImportScanForm | None, bool]:
) -> tuple[JIRAImportScanForm | None, bool]:
"""
Returns a JiraImportScanForm if jira is enabled
"""
Expand All @@ -810,7 +809,7 @@ def get_product_tab(
self,
product: Product,
engagement: Engagement,
) -> Tuple[Product_Tab, dict]:
) -> tuple[Product_Tab, dict]:
"""
Determine how the product tab will be rendered, and what tab will be selected
as currently active
Expand All @@ -827,9 +826,9 @@ def get_product_tab(
def handle_request(
self,
request: HttpRequest,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
) -> Tuple[HttpRequest, dict]:
engagement_id: int | None = None,
product_id: int | None = None,
) -> tuple[HttpRequest, dict]:
"""
Process the common behaviors between request types, and then return
the request and context dict back to be rendered
Expand Down Expand Up @@ -1074,8 +1073,8 @@ def failure_redirect(
def get(
self,
request: HttpRequest,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
engagement_id: int | None = None,
product_id: int | None = None,
) -> HttpResponse:
"""
Process GET requests for the Import View
Expand All @@ -1092,8 +1091,8 @@ def get(
def post(
self,
request: HttpRequest,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
engagement_id: int | None = None,
product_id: int | None = None,
) -> HttpResponse:
"""
Process POST requests for the Import View
Expand Down Expand Up @@ -1590,8 +1589,7 @@ def get_engagements(request):
msg = "Please use the export button when exporting engagements"
raise ValidationError(msg)
else:
if url.startswith("url="):
url = url[4:]
url = url.removeprefix("url=")

path_items = list(filter(None, re.split(r"/|\?", url)))

Expand Down
23 changes: 11 additions & 12 deletions dojo/finding/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import mimetypes
from collections import OrderedDict, defaultdict
from itertools import chain
from typing import Optional

from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -265,9 +264,9 @@ class BaseListFindings:
def __init__(
self,
filter_name: str = "All",
product_id: Optional[int] = None,
engagement_id: Optional[int] = None,
test_id: Optional[int] = None,
product_id: int | None = None,
engagement_id: int | None = None,
test_id: int | None = None,
order_by: str = "numerical_severity",
prefetch_type: str = "all",
):
Expand Down Expand Up @@ -424,7 +423,7 @@ def add_breadcrumbs(self, request: HttpRequest, context: dict):

return request, context

def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
# Store the product and engagement ids
self.product_id = product_id
self.engagement_id = engagement_id
Expand All @@ -450,43 +449,43 @@ def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement


class ListOpenFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Open"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListVerifiedFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Verified"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListOutOfScopeFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Out of Scope"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListFalsePositiveFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "False Positive"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListInactiveFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Inactive"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListAcceptedFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Accepted"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListClosedFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Closed"
self.order_by = "-mitigated"
return super().get(request, product_id=product_id, engagement_id=engagement_id)
Expand Down
3 changes: 1 addition & 2 deletions dojo/finding_group/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def view_finding_group(request, fgid):
if jira_issue:
# See if the submitted issue was a issue key or the full URL
jira_instance = jira_helper.get_jira_project(finding_group).jira_instance
if jira_issue.startswith(jira_instance.url + "/browse/"):
jira_issue = jira_issue[len(jira_instance.url + "/browse/"):]
jira_issue = jira_issue.removeprefix(jira_instance.url + "/browse/")

if finding_group.has_jira_issue and not jira_issue == jira_helper.get_jira_key(finding_group):
jira_helper.unlink_jira(request, finding_group)
Expand Down
3 changes: 1 addition & 2 deletions dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2385,8 +2385,7 @@ def get_jira_issue_template_dir_choices():
# template_list.append((os.path.join(base_dir, filename), filename))

for dirname in dirnames:
if base_dir.startswith(settings.TEMPLATE_DIR_PREFIX):
base_dir = base_dir[len(settings.TEMPLATE_DIR_PREFIX):]
base_dir = base_dir.removeprefix(settings.TEMPLATE_DIR_PREFIX)
template_dir_list.append((os.path.join(base_dir, dirname), dirname))

logger.debug("templates: %s", template_dir_list)
Expand Down
3 changes: 1 addition & 2 deletions dojo/home/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import defaultdict
from datetime import timedelta
from typing import Dict

from dateutil.relativedelta import relativedelta
from django.db.models import Count, Q
Expand Down Expand Up @@ -75,7 +74,7 @@ def support(request: HttpRequest) -> HttpResponse:
return render(request, "dojo/support.html", {})


def get_severities_all(findings) -> Dict[str, int]:
def get_severities_all(findings) -> dict[str, int]:
severities_all = findings.values("severity").annotate(count=Count("severity")).order_by()
return defaultdict(lambda: 0, {s["severity"]: s["count"] for s in severities_all})

Expand Down
30 changes: 15 additions & 15 deletions dojo/importers/auto_create_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Optional
from typing import Any

from crum import get_current_user
from django.db import transaction
Expand Down Expand Up @@ -111,7 +111,7 @@ def process_import_meta_data_from_dict(
"""
def get_target_product_type_if_exists(
self,
product_type_name: Optional[str] = None,
product_type_name: str | None = None,
**kwargs: dict,
) -> Product_Type | None:
"""
Expand All @@ -126,8 +126,8 @@ def get_target_product_type_if_exists(

def get_target_product_if_exists(
self,
product_name: Optional[str] = None,
product_type_name: Optional[str] = None,
product_name: str | None = None,
product_type_name: str | None = None,
**kwargs: dict,
) -> Product | None:
"""
Expand Down Expand Up @@ -166,7 +166,7 @@ def get_target_product_by_id_if_exists(
def get_target_engagement_if_exists(
self,
engagement_id: int = 0,
engagement_name: Optional[str] = None,
engagement_name: str | None = None,
product: Product = None,
**kwargs: dict,
) -> Engagement | None:
Expand All @@ -189,8 +189,8 @@ def get_target_engagement_if_exists(
def get_target_test_if_exists(
self,
test_id: int = 0,
test_title: Optional[str] = None,
scan_type: Optional[str] = None,
test_title: str | None = None,
scan_type: str | None = None,
engagement: Engagement = None,
**kwargs: dict,
) -> Test | None:
Expand Down Expand Up @@ -218,7 +218,7 @@ def get_target_test_if_exists(
"""
def get_or_create_product_type(
self,
product_type_name: Optional[str] = None,
product_type_name: str | None = None,
**kwargs: dict,
) -> Product_Type:
"""
Expand All @@ -242,8 +242,8 @@ def get_or_create_product_type(

def get_or_create_product(
self,
product_name: Optional[str] = None,
product_type_name: Optional[str] = None,
product_name: str | None = None,
product_type_name: str | None = None,
*,
auto_create_context: bool = False,
**kwargs: dict,
Expand Down Expand Up @@ -277,14 +277,14 @@ def get_or_create_product(
def get_or_create_engagement(
self,
engagement_id: int = 0,
engagement_name: Optional[str] = None,
product_name: Optional[str] = None,
product_type_name: Optional[str] = None,
engagement_name: str | None = None,
product_name: str | None = None,
product_type_name: str | None = None,
*,
auto_create_context: bool = False,
deduplication_on_engagement: bool = False,
source_code_management_uri: Optional[str] = None,
target_end: Optional[datetime] = None,
source_code_management_uri: str | None = None,
target_end: datetime | None = None,
**kwargs: dict,
) -> Engagement:
"""
Expand Down
Loading

0 comments on commit 4f162af

Please sign in to comment.