Skip to content

Commit

Permalink
Fix get auth number two (CenterForOpenScience#10614)
Browse files Browse the repository at this point in the history
* Don't try to get fileversions for folders
---------

Co-authored-by: Jon Walz <[email protected]>
Co-authored-by: John Tordoff <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent 2d8e4c1 commit 8483e57
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions addons/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,40 +302,25 @@ def get_authenticated_resource(resource_id):
return resource


def get_file_version_from_wb(waterbutler_data: dict) -> FileVersion:
path = waterbutler_data.get('path')
if waterbutler_data['provider'] != 'osfstorage' or not path:
return

path = waterbutler_data.get('path')
file_id = path.strip('/')
version = waterbutler_data.get('version')
if version:
try:
return FileVersion.objects.filter(
basefilenode___id=file_id,
identifier=int(version)
).select_related('region').get()
except FileVersion.DoesNotExist:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
else:
try:
return BaseFileNode.active.get(
_id=file_id,
).versions.order_by(
'-created'
).first()
except BaseFileNode.DoesNotExist:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)

def _get_osfstorage_file_version(file_node: OsfStorageFileNode, version_string: str = None) -> FileVersion:
if not (file_node and file_node.is_file):
return None

def get_file_node_from_wb(waterbutler_data: dict) -> FileVersion:
path = waterbutler_data.get('path')
file_id = path.strip('/')
try:
return BaseFileNode.active.get(_id=file_id)
except BaseFileNode.DoesNotExist:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST)
return FileVersion.objects.select_related('region').get(
basefilenode=file_node,
identifier=version_string or str(file_node.versions.count())
)
except FileVersion.DoesNotExist:
raise HTTPError(http_status.HTTP_400_BAD_REQUEST, 'Requested File Version unavailable')


def _get_osfstorage_file_node(file_path: str) -> OsfStorageFileNode:
if not file_path:
return None

file_id = file_path.strip('/')
return OsfStorageFileNode.load(file_id)


def authenticate_user_if_needed(auth, waterbutler_data, resource):
Expand Down Expand Up @@ -406,7 +391,11 @@ def get_auth(auth, **kwargs):
provider = None

# Get the file version from Waterbutler data, which is used for file-specific actions
fileversion = get_file_version_from_wb(waterbutler_data)
file_node = None
fileversion = None
if waterbutler_data['provider'] == 'osfstorage':
file_node = _get_osfstorage_file_node(waterbutler_data.get('path'))
fileversion = _get_osfstorage_file_version(file_node, waterbutler_data.get('version'))

# Fetch Waterbutler credentials and settings for the resource
credentials, waterbutler_settings = get_waterbutler_data(
Expand All @@ -419,10 +408,8 @@ def get_auth(auth, **kwargs):
if fileversion:
# Trigger any file-specific signals based on the action taken (e.g., file viewed, downloaded)
if action == 'render':
file_node = get_file_node_from_wb(waterbutler_data)
file_signals.file_viewed.send(auth=auth, fileversion=fileversion, file_node=file_node)
elif action == 'download':
file_node = get_file_node_from_wb(waterbutler_data)
file_signals.file_downloaded.send(auth=auth, fileversion=fileversion, file_node=file_node)

# Construct the response payload including the JWT
Expand Down

0 comments on commit 8483e57

Please sign in to comment.