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: clean-up after multiple merges not cover by new rules #10078

Merged
merged 15 commits into from
May 2, 2024
Merged
55 changes: 20 additions & 35 deletions dojo/api_v2/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,8 @@ def has_permission(self, request, view):
)
elif engagement_id:
# engagement_id doesn't exist
raise serializers.ValidationError(
"Engagement '%s' doesn''t exist" % engagement_id
)
msg = f"Engagement '{engagement_id}' doesn't exist"
raise serializers.ValidationError(msg)

if not auto_create_context:
raise_no_auto_create_import_validation_error(
Expand Down Expand Up @@ -513,9 +512,8 @@ def has_permission(self, request, view):
)
elif product_id:
# product_id doesn't exist
raise serializers.ValidationError(
"product '%s' doesn''t exist" % product_id
)
msg = f"product '{product_id}' doesn't exist"
raise serializers.ValidationError(msg)
else:
msg = "Need product_id or product_name to perform import"
raise serializers.ValidationError(msg)
Expand Down Expand Up @@ -663,9 +661,8 @@ def has_permission(self, request, view):
)
elif test_id:
# test_id doesn't exist
raise serializers.ValidationError(
"Test '%s' doesn't exist" % test_id
)
msg = f"Test '{test_id}' doesn't exist"
raise serializers.ValidationError(msg)

if not auto_create_context:
raise_no_auto_create_import_validation_error(
Expand Down Expand Up @@ -958,18 +955,16 @@ def raise_no_auto_create_import_validation_error(
raise ValidationError(msg)

if product_type_name and not product_type:
raise serializers.ValidationError(
"Product Type '%s' doesn't exist" % (product_type_name)
)
msg = f"Product Type '{product_type_name}' doesn't exist"
raise serializers.ValidationError(msg)

if product_name and not product:
if product_type_name:
msg = f"Product '{product_name}' doesn't exist in Product_Type '{product_type_name}'"
raise serializers.ValidationError(msg)
else:
raise serializers.ValidationError(
"Product '%s' doesn't exist" % product_name
)
msg = f"Product '{product_name}' doesn't exist"
raise serializers.ValidationError(msg)

if engagement_name and not engagement:
msg = f"Engagement '{engagement_name}' doesn't exist in Product '{product_name}'"
Expand Down Expand Up @@ -1028,48 +1023,38 @@ def check_auto_create_permission(

if product and product_name and engagement_name:
if not user_has_permission(user, product, Permissions.Engagement_Add):
raise PermissionDenied(
"No permission to create engagements in product '%s'"
% product_name
)
msg = f"No permission to create engagements in product '{product_name}'"
raise PermissionDenied(msg)

if not user_has_permission(
user, product, Permissions.Import_Scan_Result
):
raise PermissionDenied(
"No permission to import scans into product '%s'"
% product_name
)
msg = f"No permission to import scans into product '{product_name}'"
raise PermissionDenied(msg)

# all good
return True

if not product and product_name:
if not product_type_name:
raise serializers.ValidationError(
"Product '%s' doesn't exist and no product_type_name provided to create the new product in"
% product_name
)
msg = f"Product '{product_name}' doesn't exist and no product_type_name provided to create the new product in"
raise serializers.ValidationError(msg)

if not product_type:
if not user_has_global_permission(
user, Permissions.Product_Type_Add
):
raise PermissionDenied(
"No permission to create product_type '%s'"
% product_type_name
)
msg = f"No permission to create product_type '{product_type_name}'"
raise PermissionDenied(msg)
# new product type can be created with current user as owner, so
# all objects in it can be created as well
return True
else:
if not user_has_permission(
user, product_type, Permissions.Product_Type_Add_Product
):
raise PermissionDenied(
"No permission to create products in product_type '%s'"
% product_type
)
msg = f"No permission to create products in product_type '{product_type}'"
raise PermissionDenied(msg)

# product can be created, so objects in it can be created as well
return True
Expand Down
18 changes: 5 additions & 13 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,8 @@ def to_representation(self, value):
elif isinstance(value, str):
value = tagulous.utils.parse_tags(value)
else:
raise ValueError(
"unable to convert %s into list of tags"
% type(value).__name__
)
msg = f"unable to convert {type(value).__name__} into list of tags"
raise ValueError(msg)
return value


Expand Down Expand Up @@ -1912,9 +1910,7 @@ def validate(self, data):
def validate_severity(self, value: str) -> str:
if value not in SEVERITIES:
msg = f"Severity must be one of the following: {SEVERITIES}"
raise serializers.ValidationError(
msg
)
raise serializers.ValidationError(msg)
return value


Expand Down Expand Up @@ -1996,9 +1992,7 @@ class Meta:
def validate_severity(self, value: str) -> str:
if value not in SEVERITIES:
msg = f"Severity must be one of the following: {SEVERITIES}"
raise serializers.ValidationError(
msg
)
raise serializers.ValidationError(msg)
return value


Expand All @@ -2015,9 +2009,7 @@ class Meta:
def validate_severity(self, value: str) -> str:
if value not in SEVERITIES:
msg = f"Severity must be one of the following: {SEVERITIES}"
raise serializers.ValidationError(
msg
)
raise serializers.ValidationError(msg)
return value


Expand Down
6 changes: 3 additions & 3 deletions dojo/components/sql_group_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def __init__(
super().__init__(
expression,
distinct="DISTINCT " if distinct else "",
ordering=" ORDER BY %s" % ordering if ordering is not None else "",
separator=' SEPARATOR "%s"' % separator,
ordering=f" ORDER BY {ordering}" if ordering is not None else "",
separator=f' SEPARATOR "{separator}"',
output_field=CharField(),
**extra
)
Expand All @@ -23,7 +23,7 @@ def as_mysql(self, compiler, connection):
compiler,
connection,
template="%(function)s(%(distinct)s%(expressions)s%(ordering)s%(separator)s)",
separator=" SEPARATOR '%s'" % self.separator,
separator=f" SEPARATOR '{self.separator}'",
)

def as_sql(self, compiler, connection, **extra):
Expand Down
6 changes: 3 additions & 3 deletions dojo/endpoint/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def delete_endpoint(request, eid):
'Endpoint and relationships removed.',
extra_tags='alert-success')
create_notification(event='other',
title='Deletion of %s' % endpoint,
title=f'Deletion of {endpoint}',
product=product,
description=f'The endpoint "{endpoint}" was deleted by {request.user}',
url=reverse('endpoint'),
Expand Down Expand Up @@ -287,7 +287,7 @@ def add_product_endpoint(request):
messages.SUCCESS,
'Endpoint added successfully.',
extra_tags='alert-success')
return HttpResponseRedirect(reverse('endpoint') + "?product=%s" % form.product.id)
return HttpResponseRedirect(reverse('endpoint') + f"?product={form.product.id}")
add_breadcrumb(title="Add Endpoint", top_level=False, request=request)
return render(request,
'dojo/add_endpoint.html',
Expand Down Expand Up @@ -507,7 +507,7 @@ def import_endpoint_meta(request, pid):
endpoint_meta_import(file, product, create_endpoints, create_tags, create_dojo_meta, origin='UI', request=request)
except Exception as e:
logger.exception(e)
add_error_message_to_response('An exception error occurred during the report import:%s' % str(e))
add_error_message_to_response(f'An exception error occurred during the report import:{str(e)}')
return HttpResponseRedirect(reverse('endpoint') + "?product=" + pid)

add_breadcrumb(title="Endpoint Meta Importer", top_level=False, request=request)
Expand Down
25 changes: 12 additions & 13 deletions dojo/engagement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ def edit_engagement(request, eid):
if (new_status == "Cancelled" or new_status == "Completed"):
engagement.active = False
create_notification(event='close_engagement',
title='Closure of %s' % engagement.name,
description='The engagement "%s" was closed' % (engagement.name),
title=f'Closure of {engagement.name}',
description=f'The engagement "{engagement.name}" was closed',
engagement=engagement, url=reverse('engagement_all_findings', args=(engagement.id, ))),
else:
engagement.active = True
Expand Down Expand Up @@ -362,7 +362,7 @@ def delete_engagement(request, eid):
message,
extra_tags='alert-success')
create_notification(event='other',
title='Deletion of %s' % engagement.name,
title=f'Deletion of {engagement.name}',
product=product,
description=f'The engagement "{engagement.name}" was deleted by {request.user}',
url=request.build_absolute_uri(reverse('view_engagements', args=(product.id, ))),
Expand Down Expand Up @@ -405,7 +405,7 @@ def copy_engagement(request, eid):
'Engagement Copied successfully.',
extra_tags='alert-success')
create_notification(event='other',
title='Copying of %s' % engagement.name,
title=f'Copying of {engagement.name}',
description=f'The engagement "{engagement.name}" was copied by {request.user}',
product=product,
url=request.build_absolute_uri(reverse('view_engagement', args=(engagement_copy.id, ))),
Expand Down Expand Up @@ -884,7 +884,7 @@ def post(self, request, eid=None, pid=None):

except Exception as e:
logger.exception(e)
add_error_message_to_response('An exception error occurred during the report import:%s' % str(e))
add_error_message_to_response(f'An exception error occurred during the report import:{str(e)}')
error = True

# Save the credential to the test
Expand Down Expand Up @@ -917,8 +917,8 @@ def close_eng(request, eid):
'Engagement closed successfully.',
extra_tags='alert-success')
create_notification(event='close_engagement',
title='Closure of %s' % eng.name,
description='The engagement "%s" was closed' % (eng.name),
title=f'Closure of {eng.name}',
description=f'The engagement "{eng.name}" was closed',
engagement=eng, url=reverse('engagement_all_findings', args=(eng.id, ))),
return HttpResponseRedirect(reverse("view_engagements", args=(eng.product.id, )))

Expand All @@ -933,9 +933,9 @@ def reopen_eng(request, eid):
'Engagement reopened successfully.',
extra_tags='alert-success')
create_notification(event='other',
title='Reopening of %s' % eng.name,
title=f'Reopening of {eng.name}',
engagement=eng,
description='The engagement "%s" was reopened' % (eng.name),
description=f'The engagement "{eng.name}" was reopened',
url=reverse('view_engagement', args=(eng.id, ))),
return HttpResponseRedirect(reverse("view_engagements", args=(eng.product.id, )))

Expand Down Expand Up @@ -1051,7 +1051,7 @@ def add_risk_acceptance(request, eid, fid=None):

return redirect_to_return_url_or_else(request, reverse('view_engagement', args=(eid, )))
else:
risk_acceptance_title_suggestion = 'Accept: %s' % finding
risk_acceptance_title_suggestion = f'Accept: {finding}'
form = RiskAcceptanceForm(initial={'owner': request.user, 'name': risk_acceptance_title_suggestion})

finding_choices = Finding.objects.filter(duplicate=False, test__engagement=eng).filter(NOT_ACCEPTED_FINDINGS_QUERY).order_by('title')
Expand Down Expand Up @@ -1291,8 +1291,7 @@ def download_risk_acceptance(request, eid, raid):
response = StreamingHttpResponse(
FileIterWrapper(
open(settings.MEDIA_ROOT + "/" + risk_acceptance.path.name, mode='rb')))
response['Content-Disposition'] = 'attachment; filename="%s"' \
% risk_acceptance.filename()
response['Content-Disposition'] = f'attachment; filename="{risk_acceptance.filename()}"'
mimetype, _encoding = mimetypes.guess_type(risk_acceptance.path.name)
response['Content-Type'] = mimetype
return response
Expand Down Expand Up @@ -1361,7 +1360,7 @@ def engagement_ics(request, eid):
output = cal.serialize()
response = HttpResponse(content=output)
response['Content-Type'] = 'text/calendar'
response['Content-Disposition'] = 'attachment; filename=%s.ics' % eng.name
response['Content-Disposition'] = f'attachment; filename={eng.name}.ics'
return response


Expand Down
Loading
Loading