Skip to content

Commit

Permalink
ref: fix lookups by None keys (#75156)
Browse files Browse the repository at this point in the history
when models are checked for typing django-stubs points out these don't
make sense (technically they are allowed at runtime, but nonsensical)

<!-- Describe your PR here. -->
  • Loading branch information
asottile-sentry authored Jul 29, 2024
1 parent e127337 commit 9e9bb34
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/sentry/models/avatars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def save(self, *args, **kwargs):

def get_file(self):
file_id = getattr(self, self.file_write_fk(), None)
if file_id is None:
return None

file_class = self.file_class()
try:
return file_class.objects.get(pk=file_id)
Expand Down
28 changes: 14 additions & 14 deletions src/sentry/notifications/notificationcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ def __init__(
self.type = type
self.provider = provider

org_mapping = OrganizationMapping.objects.filter(organization_id=organization_id).first()
org = (
serialize_organization_mapping(org_mapping)
if organization_id and org_mapping is not None
else None
)
if organization_id is not None:
org_mapping = OrganizationMapping.objects.filter(
organization_id=organization_id
).first()
org = serialize_organization_mapping(org_mapping) if org_mapping is not None else None
else:
org = None
if org and features.has("organizations:team-workflow-notifications", org):
self.recipients: list[Recipient] = []
for recipient in recipients:
Expand Down Expand Up @@ -289,14 +290,13 @@ def _get_layered_setting_providers(
)
)

org_mapping = OrganizationMapping.objects.filter(
organization_id=self.organization_id
).first()
org = (
serialize_organization_mapping(org_mapping)
if self.organization_id and org_mapping is not None
else None
)
if self.organization_id is not None:
org_mapping = OrganizationMapping.objects.filter(
organization_id=self.organization_id
).first()
org = serialize_organization_mapping(org_mapping) if org_mapping is not None else None
else:
org = None
has_team_workflow = org and features.has("organizations:team-workflow-notifications", org)

for recipient in self.recipients:
Expand Down
14 changes: 10 additions & 4 deletions src/sentry/replays/lib/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,20 @@ def set(self, segment: RecordingSegmentStorageMeta, value: bytes) -> None:
class FilestoreBlob(Blob):
"""Filestore service driver blob manager."""

def _segment_file(self, segment: RecordingSegmentStorageMeta) -> File:
if segment.file:
return segment.file
elif segment.file_id is not None:
return File.objects.get(pk=segment.file_id)
else:
raise ValueError("expected either segment.file or segment.file_id")

def delete(self, segment: RecordingSegmentStorageMeta) -> None:
file = segment.file or File.objects.get(pk=segment.file_id)
file.delete()
self._segment_file(segment).delete()

@metrics.wraps("replays.lib.storage.FilestoreBlob.get")
def get(self, segment: RecordingSegmentStorageMeta) -> bytes:
file = segment.file or File.objects.get(pk=segment.file_id)
return file.getfile().read()
return self._segment_file(segment).getfile().read()

@metrics.wraps("replays.lib.storage.FilestoreBlob.set")
def set(self, segment: RecordingSegmentStorageMeta, value: bytes) -> None:
Expand Down

0 comments on commit 9e9bb34

Please sign in to comment.