forked from CenterForOpenScience/SHARE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: stateless pls_format_metadata api
- Loading branch information
Showing
10 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
|
||
urlpatterns = [ | ||
path('pls-format-metadata', views.pls_format_metadata), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
|
||
from django.http import HttpResponse | ||
|
||
from share.util.extensions import Extensions | ||
from share.regulate import Regulator | ||
|
||
|
||
def pls_format_metadata(request): | ||
if request.method != 'POST': | ||
return HttpResponse( | ||
status=405, | ||
headers={'Allow': 'POST'}, | ||
content='only POST!' | ||
) | ||
|
||
transformer_key = request.GET.get('transformer', 'v2_push') | ||
normal_graph = pls_normalize(request.body, transformer_key) | ||
requested_formats = request.GET.getlist('formats') | ||
formatted_records = [ | ||
pls_format(normal_graph, format_key) | ||
for format_key in requested_formats | ||
] | ||
return HttpResponse( | ||
content=json.dumps(formatted_records), | ||
) | ||
|
||
|
||
def pls_normalize(raw_datum, transformer_key): | ||
transformer = Extensions.get('share.transformers', transformer_key)() | ||
graph = transformer.transform(raw_datum) | ||
Regulator().regulate(graph) # in-place | ||
|
||
|
||
def pls_format(graph, record_formats): | ||
formatters = [ | ||
Extensions.get('share.metadata_formats', record_format)() | ||
for record_format in record_formats | ||
] | ||
return [ | ||
formatter.format_from_graph(graph) | ||
for formatter in formatters | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from tests.share.metadata_formats.base import FORMATTER_TEST_INPUTS | ||
from tests.share.metadata_formats.test_oai_dc_formatter import TestOaiDcFormatter as oaidc_test_cases | ||
|
||
|
||
class TestPlsFormatMetadata: | ||
def _get_test_keys(self): | ||
return FORMATTER_TEST_INPUTS.keys() | ||
|
||
def _get_input(self, test_key): | ||
return FORMATTER_TEST_INPUTS[test_key]['normalized_datum_kwargs']['data'] | ||
|
||
def _get_expected_output(self, test_key): | ||
return oaidc_test_cases.expected_outputs[test_key] | ||
|
||
def test_works(self, client): | ||
for test_key in self._get_test_keys(): | ||
try: | ||
resp = client.post( | ||
'/api/v2/pls-format-metadata', | ||
self._get_input(test_key), | ||
) | ||
assert resp.status_code == 200 | ||
assert resp.json() == self._get_expected_output(test_key) | ||
print(f'success! ({test_key})') | ||
except Exception as e: | ||
print(f'fail! ({test_key}, {e})') |