Skip to content

Commit

Permalink
#53 #54 Added vocabulary api detail views.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaraballo17 committed Dec 8, 2020
1 parent ffb993b commit 6fe65f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
10 changes: 4 additions & 6 deletions src/cvservices/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
class RDFRenderer(renderers.BaseRenderer):
excluded_fields = ['term']
media_type = 'text/plain'
# media_type = 'application/rdf+xml'
format = 'skos'

def render(self, data: Union[Dict, List[Dict]], media_type: str = None, renderer_context=None) -> str:
Expand All @@ -24,7 +23,7 @@ def render(self, data: Union[Dict, List[Dict]], media_type: str = None, renderer
:param renderer_context: The renderer context.
:return: A SKOS RDF rendition of the serialized controlled vocabulary.
"""
if data is None:
if not data:
return ''

if not isinstance(data, list):
Expand Down Expand Up @@ -86,7 +85,7 @@ def render(self, data: Union[Dict, List[Dict]], media_type: str = None, renderer
:param renderer_context: The renderer context.
:return: A CSV rendition of the serialized controlled vocabulary.
"""
if data is None:
if not data:
return ''

if not isinstance(data, list):
Expand All @@ -96,10 +95,9 @@ def render(self, data: Union[Dict, List[Dict]], media_type: str = None, renderer
csv_buffer: StringIO = StringIO()
# Get the fieldnames from the first object in the data
fieldnames: KeysView = data[0].keys()
# Create a CSV writer with the string buffer
# Create a CSV writer with the string buffer and write the data
csv_writer: csv.DictWriter = csv.DictWriter(csv_buffer, fieldnames=fieldnames)
# Write the headers and data to the CSV writer
csv_writer.writeheader()
csv_writer.writerows(data)
# Return the contents of the writer

return csv_buffer.getvalue()
18 changes: 9 additions & 9 deletions src/cvservices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
from cvservices.serializers import VocabularySerializer
from odm2cvs.controlled_vocabularies import Vocabulary, vocabularies

api_list_views: Dict = {}
# api_detail_views = {}
api_views: Dict = {}


class VocabularyConceptList(APIView):
class VocabularyConcept(APIView):
"""
API View for listing all concepts of one type of Controlled Vocabulary.
"""
vocabulary_code: str = ''
vocabulary: Vocabulary = {}
renderer_classes: Tuple[BaseRenderer] = (JSONRenderer, CSVRenderer, RDFRenderer, )

def get(self, request: Request, format: str = None) -> Response:
vocabulary_serializer: VocabularySerializer = VocabularySerializer(many=True)
def get(self, request: Request, term: str = None, format: str = None) -> Response:
queryset: QuerySet = self.vocabulary.get('model').objects.filter(vocabulary_status=ControlledVocabulary.CURRENT)
if term:
queryset = queryset.filter(term=term)

vocabulary_serializer: VocabularySerializer = VocabularySerializer(many=True)
serialized_vocabularies: Union[Dict, List] = vocabulary_serializer.to_representation(queryset)
return Response(serialized_vocabularies)


for vocabulary_code, vocabulary in vocabularies.items():
# api list views
api_list_views[vocabulary_code] = VocabularyConceptList.as_view(
vocabulary=vocabulary, vocabulary_code=vocabulary_code
)
# list all api views
api_views[vocabulary_code] = VocabularyConcept.as_view(vocabulary=vocabulary, vocabulary_code=vocabulary_code)
9 changes: 5 additions & 4 deletions src/odm2cvs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.auth import views as auth_views
from rest_framework.urlpatterns import format_suffix_patterns

from cvservices.views import api_list_views
from cvservices.views import api_views
from cvinterface.views.base_views import UnitsListView
from cvinterface.views.request_views import RequestsView, request_list_views, request_create_views, request_update_views
from cvinterface.views.vocabulary_views import VocabulariesView, list_views, detail_views
Expand Down Expand Up @@ -53,8 +53,9 @@
]


# api list views
for cv_name, api_view in api_list_views.items():
# api views
for cv_name, api_view in api_views.items():
urlpatterns += format_suffix_patterns([
path(f'api/v1/{cv_name}/', api_view, name=f'{cv_name}_api_list')
path(f'api/v1/{cv_name}/', api_view, name=f'{cv_name}_api_list'),
path(f'api/v1/{cv_name}/<slug:term>', api_view, name=f'{cv_name}_api_detail')
])

0 comments on commit 6fe65f7

Please sign in to comment.