Skip to content

Commit 70108ed

Browse files
authored
Ruff: Add and fix D2 (#10844)
1 parent dfe37a6 commit 70108ed

176 files changed

Lines changed: 509 additions & 543 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dojo/admin.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,25 @@
2222

2323

2424
class QuestionChildAdmin(PolymorphicChildModelAdmin):
25-
"""
26-
Base admin class for all child models of Question
27-
"""
25+
26+
"""Base admin class for all child models of Question"""
2827

2928
base_model = Question
3029

3130

3231
class TextQuestionAdmin(QuestionChildAdmin):
33-
"""
34-
ModelAdmin for a TextQuestion
35-
"""
32+
33+
"""ModelAdmin for a TextQuestion"""
3634

3735

3836
class ChoiceQuestionAdmin(QuestionChildAdmin):
39-
"""
40-
ModelAdmin for a ChoiceQuestion
41-
"""
37+
38+
"""ModelAdmin for a ChoiceQuestion"""
4239

4340

4441
class QuestionParentAdmin(PolymorphicParentModelAdmin):
45-
"""
46-
Question parent model admin
47-
"""
42+
43+
"""Question parent model admin"""
4844

4945
base_model = Question
5046
child_models = (
@@ -60,29 +56,25 @@ class QuestionParentAdmin(PolymorphicParentModelAdmin):
6056

6157

6258
class AnswerChildAdmin(PolymorphicChildModelAdmin):
63-
"""
64-
Base admin class for all child Answer models
65-
"""
59+
60+
"""Base admin class for all child Answer models"""
6661

6762
base_model = Answer
6863

6964

7065
class TextAnswerAdmin(AnswerChildAdmin):
71-
"""
72-
ModelAdmin for TextAnswer
73-
"""
66+
67+
"""ModelAdmin for TextAnswer"""
7468

7569

7670
class ChoiceAnswerAdmin(AnswerChildAdmin):
77-
"""
78-
ModelAdmin for ChoiceAnswer
79-
"""
71+
72+
"""ModelAdmin for ChoiceAnswer"""
8073

8174

8275
class AnswerParentAdmin(PolymorphicParentModelAdmin):
83-
"""
84-
The parent model admin for answer
85-
"""
76+
77+
"""The parent model admin for answer"""
8678

8779
list_display = (
8880
"answered_survey",

dojo/api_v2/prefetch/prefetcher.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
class _Prefetcher:
1919
@staticmethod
2020
def _build_serializers():
21-
"""Returns a map model -> serializer where model is a django model and serializer is the corresponding
21+
"""
22+
Returns a map model -> serializer where model is a django model and serializer is the corresponding
2223
serializer used to serialize the model
2324
2425
Returns:
@@ -52,7 +53,8 @@ def __init__(self):
5253
self._prefetch_data = {}
5354

5455
def _find_serializer(self, field_type):
55-
"""Find the best suited serializer for the given type.
56+
"""
57+
Find the best suited serializer for the given type.
5658
5759
Args:
5860
field_type (django.db.models.fields): the field type for which we need to find a serializer
@@ -72,7 +74,8 @@ def _find_serializer(self, field_type):
7274
return self._find_serializer(parent_class)
7375

7476
def _prefetch(self, entry, fields_to_fetch):
75-
"""Apply prefetching for the given field on the given entry
77+
"""
78+
Apply prefetching for the given field on the given entry
7679
7780
Args:
7881
entry (ModelInstance): Instance of a model as returned by a django queryset

dojo/api_v2/prefetch/schema.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def _get_path_to_GET_serializer_map(generator):
1818

1919

2020
def get_serializer_ref_name(serializer):
21-
"""Get serializer's ref_name
21+
"""
22+
Get serializer's ref_name
2223
inspired by https://github.com/axnsan12/drf-yasg/blob/78031f0c189585c30fccb5005a6899f2d34289a9/src/drf_yasg/utils.py#L416
2324
2425
:param serializer: Serializer instance
@@ -37,14 +38,14 @@ def get_serializer_ref_name(serializer):
3738

3839

3940
def prefetch_postprocessing_hook(result, generator, request, public):
40-
"""OpenAPI v3 (drf-spectacular) Some endpoints are using the PrefetchListMixin and PrefetchRetrieveMixin.
41+
"""
42+
OpenAPI v3 (drf-spectacular) Some endpoints are using the PrefetchListMixin and PrefetchRetrieveMixin.
4143
These have nothing to do with Django prefetch_related.
4244
The endpoints have an @extend_schema configured with an extra parameter 'prefetch'
4345
This parameter contains an array of relations to prefetch. These prefetched models
4446
will be returned in an additional property in the response.
4547
The below processor ensures the result schema matches this.
4648
"""
47-
4849
serializer_classes = _get_path_to_GET_serializer_map(generator)
4950

5051
paths = result.get("paths", {})

dojo/api_v2/prefetch/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
def _is_many_to_many_relation(field):
5-
"""Check if a field specified a many-to-many relationship as defined by django.
5+
"""
6+
Check if a field specified a many-to-many relationship as defined by django.
67
This is the case if the field is an instance of the ManyToManyDescriptor as generated
78
by the django framework
89
@@ -16,7 +17,8 @@ def _is_many_to_many_relation(field):
1617

1718

1819
def _is_one_to_one_relation(field):
19-
"""Check if a field specified a one-to-one relationship as defined by django.
20+
"""
21+
Check if a field specified a one-to-one relationship as defined by django.
2022
This is the case if the field is an instance of the ForwardManyToOne as generated
2123
by the django framework
2224
@@ -30,7 +32,8 @@ def _is_one_to_one_relation(field):
3032

3133

3234
def _get_prefetchable_fields(serializer):
33-
"""Get the fields that are prefetchable according to the serializer description.
35+
"""
36+
Get the fields that are prefetchable according to the serializer description.
3437
Method mainly used by for automatic schema generation.
3538
3639
Args:

dojo/api_v2/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,7 @@ def get(self, request, format=None):
23702370

23712371
# Authorization: authenticated users, DjangoModelPermissions
23722372
class ImportScanView(mixins.CreateModelMixin, viewsets.GenericViewSet):
2373+
23732374
"""
23742375
Imports a scan report into an engagement or product.
23752376
@@ -2433,6 +2434,7 @@ def get_queryset(self):
24332434
class EndpointMetaImporterView(
24342435
mixins.CreateModelMixin, viewsets.GenericViewSet,
24352436
):
2437+
24362438
"""
24372439
Imports a CSV file into a product to propagate arbitrary meta and tags on endpoints.
24382440
@@ -2508,6 +2510,7 @@ def get_queryset(self):
25082510

25092511
# Authorization: object-based
25102512
class ReImportScanView(mixins.CreateModelMixin, viewsets.GenericViewSet):
2513+
25112514
"""
25122515
Reimports a scan report into an existing test.
25132516
@@ -2908,6 +2911,7 @@ def report_generate(request, obj, options):
29082911
class SystemSettingsViewSet(
29092912
mixins.ListModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet,
29102913
):
2914+
29112915
"""Basic control over System Settings. Use 'id' 1 for PUT, PATCH operations"""
29122916

29132917
permission_classes = (permissions.IsSuperUser, DjangoModelPermissions)

dojo/authorization/authorization_decorators.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
def user_is_authorized(model, permission, arg, lookup="pk", func=None):
1414
"""Decorator for functions that ensures the user has permission on an object."""
15-
1615
if func is None:
1716
return functools.partial(
1817
user_is_authorized, model, permission, arg, lookup,
@@ -41,7 +40,6 @@ def _wrapped(request, *args, **kwargs):
4140

4241
def user_has_global_permission(permission, func=None):
4342
"""Decorator for functions that ensures the user has a (global) permission"""
44-
4543
if func is None:
4644
return functools.partial(user_has_global_permission, permission)
4745

@@ -54,10 +52,7 @@ def _wrapped(request, *args, **kwargs):
5452

5553

5654
def user_is_configuration_authorized(permission, func=None):
57-
"""
58-
Decorator for views that checks whether a user has a particular permission enabled.
59-
"""
60-
55+
"""Decorator for views that checks whether a user has a particular permission enabled."""
6156
if func is None:
6257
return functools.partial(user_is_configuration_authorized, permission)
6358

dojo/authorization/roles_permissions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,7 @@ def get_roles_with_permissions():
517517

518518

519519
def get_global_roles_with_permissions():
520-
"""
521-
Extra permissions for global roles, on top of the permissions granted to the "normal" roles above.
522-
"""
520+
"""Extra permissions for global roles, on top of the permissions granted to the "normal" roles above."""
523521
return {
524522
Roles.Maintainer: {Permissions.Product_Type_Add},
525523
Roles.Owner: {Permissions.Product_Type_Add},

dojo/engagement/views.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,7 @@ def add_tests(request, eid):
693693

694694
class ImportScanResultsView(View):
695695
def get_template(self) -> str:
696-
"""
697-
Returns the template that will be presented to the user
698-
"""
696+
"""Returns the template that will be presented to the user"""
699697
return "dojo/import_scan_results.html"
700698

701699
def get_development_environment(
@@ -715,9 +713,7 @@ def get_engagement_or_product(
715713
engagement_id: Optional[int] = None,
716714
product_id: Optional[int] = None,
717715
) -> Tuple[Engagement, Product, Product | Engagement]:
718-
"""
719-
Using the path parameters, either fetch the product or engagement
720-
"""
716+
"""Using the path parameters, either fetch the product or engagement"""
721717
engagement = product = engagement_or_product = None
722718
# Get the product if supplied
723719
# Get the engagement if supplied
@@ -740,9 +736,7 @@ def get_form(
740736
request: HttpRequest,
741737
**kwargs: dict,
742738
) -> ImportScanForm:
743-
"""
744-
Returns the default import form for importing findings
745-
"""
739+
"""Returns the default import form for importing findings"""
746740
if request.method == "POST":
747741
return ImportScanForm(request.POST, request.FILES, **kwargs)
748742
return ImportScanForm(**kwargs)
@@ -776,9 +770,7 @@ def get_jira_form(
776770
request: HttpRequest,
777771
engagement_or_product: Engagement | Product,
778772
) -> Tuple[JIRAImportScanForm | None, bool]:
779-
"""
780-
Returns a JiraImportScanForm if jira is enabled
781-
"""
773+
"""Returns a JiraImportScanForm if jira is enabled"""
782774
jira_form = None
783775
push_all_jira_issues = False
784776
# Determine if jira issues should be pushed automatically
@@ -919,18 +911,14 @@ def get_importer(
919911
self,
920912
context: dict,
921913
) -> BaseImporter:
922-
"""
923-
Gets the importer to use
924-
"""
914+
"""Gets the importer to use"""
925915
return DefaultImporter(**context)
926916

927917
def import_findings(
928918
self,
929919
context: dict,
930920
) -> str | None:
931-
"""
932-
Attempt to import with all the supplied information
933-
"""
921+
"""Attempt to import with all the supplied information"""
934922
try:
935923
importer_client = self.get_importer(context)
936924
context["test"], _, finding_count, closed_finding_count, _, _, _ = importer_client.process_scan(
@@ -952,9 +940,7 @@ def process_form(
952940
form: ImportScanForm,
953941
context: dict,
954942
) -> str | None:
955-
"""
956-
Process the form and manipulate the input in any way that is appropriate
957-
"""
943+
"""Process the form and manipulate the input in any way that is appropriate"""
958944
# Update the running context dict with cleaned form input
959945
context.update({
960946
"scan": request.FILES.get("file", None),
@@ -1024,9 +1010,7 @@ def process_credentials_form(
10241010
form: CredMappingForm,
10251011
context: dict,
10261012
) -> str | None:
1027-
"""
1028-
Process the credentials form by creating
1029-
"""
1013+
"""Process the credentials form by creating"""
10301014
if cred_user := form.cleaned_data["cred_user"]:
10311015
# Select the credential mapping object from the selected list and only allow if the credential is associated with the product
10321016
cred_user = Cred_Mapping.objects.filter(
@@ -1046,18 +1030,14 @@ def success_redirect(
10461030
self,
10471031
context: dict,
10481032
) -> HttpResponseRedirect:
1049-
"""
1050-
Redirect the user to a place that indicates a successful import
1051-
"""
1033+
"""Redirect the user to a place that indicates a successful import"""
10521034
return HttpResponseRedirect(reverse("view_test", args=(context.get("test").id, )))
10531035

10541036
def failure_redirect(
10551037
self,
10561038
context: dict,
10571039
) -> HttpResponseRedirect:
1058-
"""
1059-
Redirect the user to a place that indicates a failed import
1060-
"""
1040+
"""Redirect the user to a place that indicates a failed import"""
10611041
return HttpResponseRedirect(reverse(
10621042
"import_scan_results",
10631043
args=(context.get("engagement", context.get("product")).id, ),
@@ -1069,9 +1049,7 @@ def get(
10691049
engagement_id: Optional[int] = None,
10701050
product_id: Optional[int] = None,
10711051
) -> HttpResponse:
1072-
"""
1073-
Process GET requests for the Import View
1074-
"""
1052+
"""Process GET requests for the Import View"""
10751053
# process the request and path parameters
10761054
request, context = self.handle_request(
10771055
request,
@@ -1087,9 +1065,7 @@ def post(
10871065
engagement_id: Optional[int] = None,
10881066
product_id: Optional[int] = None,
10891067
) -> HttpResponse:
1090-
"""
1091-
Process POST requests for the Import View
1092-
"""
1068+
"""Process POST requests for the Import View"""
10931069
# process the request and path parameters
10941070
request, context = self.handle_request(
10951071
request,

dojo/finding/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def engagement_post_delete(sender, instance, **kwargs):
566566

567567

568568
def fix_loop_duplicates():
569-
""" Due to bugs in the past and even currently when under high parallel load, there can be transitive duplicates. """
569+
"""Due to bugs in the past and even currently when under high parallel load, there can be transitive duplicates."""
570570
""" i.e. A -> B -> C. This can lead to problems when deleting findingns, performing deduplication, etc """
571571
candidates = Finding.objects.filter(duplicate_finding__isnull=False, original_finding__isnull=False).order_by("-id")
572572

dojo/forms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def render(self, name, *args, **kwargs):
140140

141141

142142
class MonthYearWidget(Widget):
143+
143144
"""
144145
A Widget that splits date input into two <select> boxes for month and year,
145146
with 'day' defaulting to the first of the month.
@@ -148,6 +149,7 @@ class MonthYearWidget(Widget):
148149
149150
django/trunk/django/forms/extras/widgets.py
150151
"""
152+
151153
none_value = (0, "---")
152154
month_field = "%s_month"
153155
year_field = "%s_year"
@@ -3176,8 +3178,8 @@ def __init__(self, *args, **kwargs):
31763178
# Show in admin a multichoice list of validator names
31773179
# pass this to form using field_name='validator_name' ?
31783180
class QuestionForm(forms.Form):
3179-
""" Base class for a Question
3180-
"""
3181+
3182+
"""Base class for a Question"""
31813183

31823184
def __init__(self, *args, **kwargs):
31833185
self.helper = FormHelper()

0 commit comments

Comments
 (0)