Skip to content

Commit 79ea71d

Browse files
authored
Ruff: Add PLR5 and fix PLR5501 (#11714)
1 parent 41b3bfb commit 79ea71d

File tree

27 files changed

+657
-700
lines changed

27 files changed

+657
-700
lines changed

dojo/api_v2/exception_handler.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,33 @@ def custom_exception_handler(exc, context):
3636
response.status_code = HTTP_400_BAD_REQUEST
3737
response.data = {}
3838
response.data["message"] = str(exc)
39-
else:
40-
if response is None:
41-
if System_Settings.objects.get().api_expose_error_details:
42-
exception_message = str(exc.args[0])
43-
else:
44-
exception_message = "Internal server error, check logs for details"
45-
# There is no standard error response, so we assume an unexpected
46-
# exception. It is logged but no details are given to the user,
47-
# to avoid leaking internal technical information.
48-
logger.exception(exc)
49-
response = Response()
50-
response.status_code = HTTP_500_INTERNAL_SERVER_ERROR
51-
response.data = {}
52-
response.data[
53-
"message"
54-
] = exception_message
39+
elif response is None:
40+
if System_Settings.objects.get().api_expose_error_details:
41+
exception_message = str(exc.args[0])
5542
else:
56-
if response.status_code < 500:
57-
# HTTP status codes lower than 500 are no technical errors.
58-
# They need not to be logged and we provide the exception
59-
# message, if it is different from the detail that is already
60-
# in the response.
61-
if isinstance(response.data, dict) and str(
62-
exc,
63-
) != response.data.get("detail", ""):
64-
response.data["message"] = str(exc)
65-
else:
66-
# HTTP status code 500 or higher are technical errors.
67-
# They get logged and we don't change the response.
68-
logger.exception(exc)
43+
exception_message = "Internal server error, check logs for details"
44+
# There is no standard error response, so we assume an unexpected
45+
# exception. It is logged but no details are given to the user,
46+
# to avoid leaking internal technical information.
47+
logger.exception(exc)
48+
response = Response()
49+
response.status_code = HTTP_500_INTERNAL_SERVER_ERROR
50+
response.data = {}
51+
response.data[
52+
"message"
53+
] = exception_message
54+
elif response.status_code < 500:
55+
# HTTP status codes lower than 500 are no technical errors.
56+
# They need not to be logged and we provide the exception
57+
# message, if it is different from the detail that is already
58+
# in the response.
59+
if isinstance(response.data, dict) and str(
60+
exc,
61+
) != response.data.get("detail", ""):
62+
response.data["message"] = str(exc)
63+
else:
64+
# HTTP status code 500 or higher are technical errors.
65+
# They get logged and we don't change the response.
66+
logger.exception(exc)
6967

7068
return response

dojo/endpoint/utils.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ def endpoint_filter(**kwargs):
3333
qs = qs.filter(Q(port__isnull=True) | Q(port__exact=SCHEME_PORT_MAP[kwargs["protocol"].lower()]))
3434
else:
3535
qs = qs.filter(port__exact=kwargs["port"])
36+
elif (kwargs.get("protocol")) and (kwargs["protocol"].lower() in SCHEME_PORT_MAP):
37+
qs = qs.filter(Q(port__isnull=True) | Q(port__exact=SCHEME_PORT_MAP[kwargs["protocol"].lower()]))
3638
else:
37-
if (kwargs.get("protocol")) and (kwargs["protocol"].lower() in SCHEME_PORT_MAP):
38-
qs = qs.filter(Q(port__isnull=True) | Q(port__exact=SCHEME_PORT_MAP[kwargs["protocol"].lower()]))
39-
else:
40-
qs = qs.filter(port__isnull=True)
39+
qs = qs.filter(port__isnull=True)
4140

4241
qs = qs.filter(path__exact=kwargs["path"]) if kwargs.get("path") else qs.filter(path__isnull=True)
4342

@@ -110,9 +109,8 @@ def err_log(message, html_log, endpoint_html_log, endpoint):
110109
f"({parts.protocol})"
111110
)
112111
err_log(message, html_log, endpoint_html_log, endpoint)
113-
else:
114-
if change:
115-
endpoint.protocol = parts.protocol
112+
elif change:
113+
endpoint.protocol = parts.protocol
116114

117115
if parts.userinfo:
118116
if change:
@@ -133,9 +131,8 @@ def err_log(message, html_log, endpoint_html_log, endpoint):
133131
f"host ({parts.port})"
134132
)
135133
err_log(message, html_log, endpoint_html_log, endpoint)
136-
else:
137-
if change:
138-
endpoint.port = parts.port
134+
elif change:
135+
endpoint.port = parts.port
139136
except ValueError:
140137
message = f"uses non-numeric port: {endpoint.port}"
141138
err_log(message, html_log, endpoint_html_log, endpoint)
@@ -147,9 +144,8 @@ def err_log(message, html_log, endpoint_html_log, endpoint):
147144
f"({parts.path})"
148145
)
149146
err_log(message, html_log, endpoint_html_log, endpoint)
150-
else:
151-
if change:
152-
endpoint.path = parts.path
147+
elif change:
148+
endpoint.path = parts.path
153149

154150
if parts.query:
155151
if endpoint.query and (endpoint.query != parts.query):
@@ -158,9 +154,8 @@ def err_log(message, html_log, endpoint_html_log, endpoint):
158154
f"({parts.query})"
159155
)
160156
err_log(message, html_log, endpoint_html_log, endpoint)
161-
else:
162-
if change:
163-
endpoint.query = parts.query
157+
elif change:
158+
endpoint.query = parts.query
164159

165160
if parts.fragment:
166161
if endpoint.fragment and (endpoint.fragment != parts.fragment):
@@ -169,9 +164,8 @@ def err_log(message, html_log, endpoint_html_log, endpoint):
169164
f"({parts.fragment})"
170165
)
171166
err_log(message, html_log, endpoint_html_log, endpoint)
172-
else:
173-
if change:
174-
endpoint.fragment = parts.fragment
167+
elif change:
168+
endpoint.fragment = parts.fragment
175169

176170
if change and (endpoint.pk not in broken_endpoints): # do not save broken endpoints
177171
endpoint.save()

dojo/endpoint/views.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -381,38 +381,37 @@ def endpoint_bulk_update_all(request, pid=None):
381381
messages.SUCCESS,
382382
f"Bulk delete of {deleted_endpoint_count} endpoints was successful.",
383383
extra_tags="alert-success")
384-
else:
385-
if endpoints_to_update:
384+
elif endpoints_to_update:
386385

387-
if pid is not None:
388-
product = get_object_or_404(Product, id=pid)
389-
user_has_permission_or_403(request.user, product, Permissions.Finding_Edit)
386+
if pid is not None:
387+
product = get_object_or_404(Product, id=pid)
388+
user_has_permission_or_403(request.user, product, Permissions.Finding_Edit)
390389

391-
endpoints = get_authorized_endpoints(Permissions.Endpoint_Edit, endpoints, request.user)
390+
endpoints = get_authorized_endpoints(Permissions.Endpoint_Edit, endpoints, request.user)
392391

393-
skipped_endpoint_count = total_endpoint_count - endpoints.count()
394-
updated_endpoint_count = endpoints.count()
392+
skipped_endpoint_count = total_endpoint_count - endpoints.count()
393+
updated_endpoint_count = endpoints.count()
395394

396-
if skipped_endpoint_count > 0:
397-
add_error_message_to_response(f"Skipped mitigation of {skipped_endpoint_count} endpoints because you are not authorized.")
395+
if skipped_endpoint_count > 0:
396+
add_error_message_to_response(f"Skipped mitigation of {skipped_endpoint_count} endpoints because you are not authorized.")
398397

399-
eps_count = Endpoint_Status.objects.filter(endpoint__in=endpoints).update(
400-
mitigated=True,
401-
mitigated_by=request.user,
402-
mitigated_time=timezone.now(),
403-
last_modified=timezone.now(),
404-
)
398+
eps_count = Endpoint_Status.objects.filter(endpoint__in=endpoints).update(
399+
mitigated=True,
400+
mitigated_by=request.user,
401+
mitigated_time=timezone.now(),
402+
last_modified=timezone.now(),
403+
)
405404

406-
if updated_endpoint_count > 0:
407-
messages.add_message(request,
408-
messages.SUCCESS,
409-
f"Bulk mitigation of {updated_endpoint_count} endpoints ({eps_count} endpoint statuses) was successful.",
410-
extra_tags="alert-success")
411-
else:
405+
if updated_endpoint_count > 0:
412406
messages.add_message(request,
413-
messages.ERROR,
414-
"Unable to process bulk update. Required fields were not selected.",
415-
extra_tags="alert-danger")
407+
messages.SUCCESS,
408+
f"Bulk mitigation of {updated_endpoint_count} endpoints ({eps_count} endpoint statuses) was successful.",
409+
extra_tags="alert-success")
410+
else:
411+
messages.add_message(request,
412+
messages.ERROR,
413+
"Unable to process bulk update. Required fields were not selected.",
414+
extra_tags="alert-danger")
416415
return HttpResponseRedirect(reverse("endpoint", args=()))
417416

418417

dojo/engagement/views.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,9 +1369,8 @@ def view_edit_risk_acceptance(request, eid, raid, *, edit_mode=False):
13691369
return redirect_to_return_url_or_else(request, reverse("view_risk_acceptance", args=(eid, raid)))
13701370
logger.error("errors found")
13711371

1372-
else:
1373-
if edit_mode:
1374-
risk_acceptance_form = EditRiskAcceptanceForm(instance=risk_acceptance)
1372+
elif edit_mode:
1373+
risk_acceptance_form = EditRiskAcceptanceForm(instance=risk_acceptance)
13751374

13761375
note_form = NoteForm()
13771376
replace_form = ReplaceRiskAcceptanceProofForm(instance=risk_acceptance)

dojo/finding/helper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,9 @@ def update_finding_group(finding, finding_group):
237237
finding.finding_group.findings.remove(finding)
238238
logger.debug("adding finding %d to finding_group %s", finding.id, finding_group)
239239
finding_group.findings.add(finding)
240-
else:
241-
if finding.finding_group:
242-
logger.debug("removing finding %d from finding_group %s", finding.id, finding.finding_group)
243-
finding.finding_group.findings.remove(finding)
240+
elif finding.finding_group:
241+
logger.debug("removing finding %d from finding_group %s", finding.id, finding.finding_group)
242+
finding.finding_group.findings.remove(finding)
244243

245244

246245
def get_group_by_group_name(finding, finding_group_by_option):

0 commit comments

Comments
 (0)