Skip to content

Commit

Permalink
Merge pull request #10912 from cslzchen/feature/improve-guid-none-check
Browse files Browse the repository at this point in the history
[ENG-6887] Check `None` first in `.load()` or `.load_referent()`
  • Loading branch information
cslzchen authored Jan 13, 2025
2 parents b82d5ef + 82be578 commit e49b64c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion osf/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,25 @@ def load(cls, data, select_for_update=False, ignore_not_found=False):
If `ignore_not_found` is True, then don't log to sentry. This is used in `website.views.resolve_guid()`.
"""
if not data:
return None
base_guid_str, version = cls.split_guid(data)
try:
if not select_for_update:
return cls.objects.get(_id=base_guid_str)
return cls.objects.filter(_id=base_guid_str).select_for_update().get()
except cls.DoesNotExist:
if not ignore_not_found:
sentry.log_message(f'Object not found from base guid: [guid={base_guid_str}, version={version}]')
sentry.log_message(f'Object not found from base guid: '
f'[data={data}, guid={base_guid_str}, version={version}]')
return None

@classmethod
def load_referent(cls, guid_str, ignore_not_found=False):
"""Find and return the referent from a given guid str.
"""
if not guid_str:
return None, None
base_guid_str, version = cls.split_guid(guid_str)
base_guid_obj = cls.load(base_guid_str, ignore_not_found=ignore_not_found)
if not base_guid_obj:
Expand Down Expand Up @@ -561,6 +566,8 @@ def load(cls, guid_str, select_for_update=False):
base guid str and the version in the guid str. If the guid str does not have version, it returns the object
referred by the base guid obj.
"""
if not guid_str:
return None
try:
base_guid_str, version = Guid.split_guid(guid_str)
# Version exists
Expand Down

0 comments on commit e49b64c

Please sign in to comment.