semgrep: Enforce no bare select_related() calls.

Previously, #26419 addressed the majority of these calls, but did not
prevent more from creeping in.  Remove the one remaining
callsite (after the cleanup from the previous commits), and ban any
future use of the pattern.
This commit is contained in:
Alex Vandiver 2024-03-22 14:01:55 +00:00 committed by Tim Abbott
parent fd5a091b30
commit 7d0c12aa3a
2 changed files with 10 additions and 3 deletions

View File

@ -43,6 +43,15 @@ rules:
exclude: exclude:
- "**/migrations/" - "**/migrations/"
- id: dont-use-empty-select_related
pattern-either:
- pattern: $X.objects. ... .select_related()
- pattern: $X.objects. ... .prefetch_related()
message: |
Do not use a bare '.select_related()' or '.prefetch_related()', which can join many more tables than expected. Specify the relations to follow explicitly.
languages: [python]
severity: ERROR
- id: dont-import-models-in-migrations - id: dont-import-models-in-migrations
patterns: patterns:
- pattern-not: from zerver.lib.redis_utils import get_redis_client - pattern-not: from zerver.lib.redis_utils import get_redis_client

View File

@ -38,9 +38,7 @@ def fix_messages(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> No
return return
def get_bot_by_delivery_email(email: str) -> Any: def get_bot_by_delivery_email(email: str) -> Any:
return UserProfile.objects.select_related().get( return UserProfile.objects.get(delivery_email__iexact=email.strip(), realm=internal_realm)
delivery_email__iexact=email.strip(), realm=internal_realm
)
notification_bot = get_bot_by_delivery_email(settings.NOTIFICATION_BOT) notification_bot = get_bot_by_delivery_email(settings.NOTIFICATION_BOT)