Skip to content

Commit

Permalink
Merge pull request CenterForOpenScience#10531 from cslzchen/feature/c…
Browse files Browse the repository at this point in the history
…edar-bug-fix

[Cedar] Fix file cedar metadata records relationship
  • Loading branch information
cslzchen authored Feb 5, 2024
2 parents 4817f03 + 060f08e commit 5eaac71
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions api/files/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
ShowIfVersion,
)
from api.base.utils import absolute_reverse, get_user_auth
from api.base.exceptions import Conflict
from api.base.exceptions import Conflict, JSONAPIException
from api.base.versioning import get_kebab_snake_case_field

class CheckoutField(ser.HyperlinkedRelatedField):
Expand Down Expand Up @@ -370,6 +370,13 @@ def get_file_guid(self, obj):
return guid._id
return None

def get_file_guid_or_error(self, obj):
if obj:
guid = obj.get_guid()
if guid:
return guid._id
raise JSONAPIException

def get_absolute_url(self, obj):
return api_v2_url('files/{}/'.format(obj._id))

Expand All @@ -395,7 +402,7 @@ class FileSerializer(BaseFileSerializer):

cedar_metadata_records = RelationshipField(
related_view='files:file-cedar-metadata-records-list',
related_view_kwargs={'file_id': '<_id>'},
related_view_kwargs={'file_guid': 'get_file_guid_or_error'},
)

def get_target_type(self, obj):
Expand Down
2 changes: 1 addition & 1 deletion api/files/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

urlpatterns = [
re_path(r'^(?P<file_id>\w+)/$', views.FileDetail.as_view(), name=views.FileDetail.view_name),
re_path(r'^(?P<file_id>\w+)/cedar_metadata_records/$', views.FileCedarMetadataRecordsList.as_view(), name=views.FileCedarMetadataRecordsList.view_name),
re_path(r'^(?P<file_guid>\w+)/cedar_metadata_records/$', views.FileCedarMetadataRecordsList.as_view(), name=views.FileCedarMetadataRecordsList.view_name),
re_path(r'^(?P<file_id>\w+)/versions/$', views.FileVersionsList.as_view(), name=views.FileVersionsList.view_name),
re_path(r'^(?P<file_id>\w+)/versions/(?P<version_id>\w+)/$', views.FileVersionDetail.as_view(), name=views.FileVersionDetail.view_name),
]
2 changes: 1 addition & 1 deletion api/files/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class FileCedarMetadataRecordsList(JSONAPIBaseView, generics.ListAPIView, ListFi
view_name = 'file-cedar-metadata-records-list'

def get_default_queryset(self):
return CedarMetadataRecord.objects.filter(guid___id=self.kwargs['file_id'])
return CedarMetadataRecord.objects.filter(guid___id=self.kwargs['file_guid'])

def get_queryset(self):
return self.get_queryset_from_request()

0 comments on commit 5eaac71

Please sign in to comment.