Browse Source

ref: fix lookups by `None` keys (#75156)

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. -->
anthony sottile 7 months ago
parent
commit
9e9bb346fc

+ 3 - 0
src/sentry/models/avatars/base.py

@@ -50,6 +50,9 @@ class AvatarBase(Model):
 
     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)

+ 14 - 14
src/sentry/notifications/notificationcontroller.py

@@ -72,12 +72,13 @@ class NotificationController:
         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:
@@ -289,14 +290,13 @@ class NotificationController:
             )
         )
 
-        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:

+ 10 - 4
src/sentry/replays/lib/storage.py

@@ -99,14 +99,20 @@ class Blob(ABC):
 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: