Skip to content

Commit

Permalink
Merge pull request CenterForOpenScience#10526 from cslzchen/feature/e…
Browse files Browse the repository at this point in the history
…ng-5247-download-link

[ENG-5247] Support metadata download for a given record
  • Loading branch information
cslzchen authored Feb 2, 2024
2 parents fc585aa + 8c51384 commit 1a3dddb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 7 additions & 1 deletion api/cedar_metadata_records/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ class Meta:
read_only=True,
)

links = LinksField({'self': 'get_absolute_url'})
links = LinksField({
'self': 'get_absolute_url',
'metadata_download': 'get_metadata_download_link',
})

def get_absolute_url(self, obj):
return absolute_reverse('cedar-metadata-records:cedar-metadata-record-detail', kwargs={'record_id': obj._id})

def get_metadata_download_link(self, obj):
return absolute_reverse('cedar-metadata-records:cedar-metadata-record-metadata-download', kwargs={'record_id': obj._id})

def update(self, instance, validated_data):
raise NotImplementedError

Expand Down
1 change: 1 addition & 0 deletions api/cedar_metadata_records/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

urlpatterns = [
re_path(r'^$', views.CedarMetadataRecordList.as_view(), name=views.CedarMetadataRecordList.view_name),
re_path(r'^(?P<record_id>[0-9A-Za-z]+)/metadata_download/$', views.CedarMetadataRecordMetadataDownload.as_view(), name=views.CedarMetadataRecordMetadataDownload.view_name),
re_path(r'^(?P<record_id>[0-9A-Za-z]+)/$', views.CedarMetadataRecordDetail.as_view(), name=views.CedarMetadataRecordDetail.view_name),
]
35 changes: 34 additions & 1 deletion api/cedar_metadata_records/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from rest_framework import permissions as drf_permissions
from rest_framework.exceptions import NotFound
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.renderers import JSONRenderer
from rest_framework.views import Response

from api.base import permissions as base_permissions
from api.base.filters import ListFilterMixin
Expand Down Expand Up @@ -80,3 +82,34 @@ def get_object(self):
return CedarMetadataRecord.objects.get(_id=self.kwargs['record_id'])
except CedarMetadataRecord.DoesNotExist:
raise NotFound

class CedarMetadataRecordMetadataDownload(JSONAPIBaseView, RetrieveAPIView):

permission_classes = (
CedarMetadataRecordPermission,
drf_permissions.IsAuthenticatedOrReadOnly,
base_permissions.TokenHasScope,
)
required_read_scopes = [CoreScopes.CEDAR_METADATA_RECORD_READ]
required_write_scopes = [CoreScopes.CEDAR_METADATA_RECORD_WRITE]

renderer_classes = [JSONRenderer]

# This view goes under the _/ namespace
versioning_class = PrivateVersioning
view_category = 'cedar-metadata-records'
view_name = 'cedar-metadata-record-metadata-download'

def get_object(self):
try:
return CedarMetadataRecord.objects.get(_id=self.kwargs['record_id'])
except CedarMetadataRecord.DoesNotExist:
raise NotFound

def get_serializer_class(self):
return None

def get(self, request, **kwargs):
record = self.get_object()
file_name = f'{record._id}-{record.get_template_name()}-v{record.get_template_version()}.json'
return Response(record.metadata, headers={'Content-Disposition': f'attachment; filename={file_name}'})
3 changes: 3 additions & 0 deletions osf/models/cedar_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def get_template_semantic_iri(self):
def get_template_name(self):
return self.template.schema_name

def get_template_version(self):
return self.template.template_version

def save(self, *args, **kwargs):
self.guid.referent.update_search()
return super().save(*args, **kwargs)

0 comments on commit 1a3dddb

Please sign in to comment.