From fd29b0519efcfe8da66a313303bafc5057e72a32 Mon Sep 17 00:00:00 2001 From: Ieuan Scanlon Date: Tue, 7 Nov 2023 09:45:14 +0000 Subject: [PATCH] chore(release): API rendering changes & attribute hotfix (#1453) --- .../clinicalcode/api/views/Concept.py | 9 ++++--- .../clinicalcode/entity_utils/api_utils.py | 6 +++++ .../entity_utils/concept_utils.py | 9 ++++--- .../clinicalcode/entity_utils/create_utils.py | 4 +++ CodeListLibrary_project/cll/settings.py | 3 +-- .../generic_entity/detail/detail_buttons.html | 8 ------ .../components/details/outputs/api.html | 27 ------------------- .../phenotype_clinical_code_lists.html | 8 ------ 8 files changed, 23 insertions(+), 51 deletions(-) diff --git a/CodeListLibrary_project/clinicalcode/api/views/Concept.py b/CodeListLibrary_project/clinicalcode/api/views/Concept.py index 0428e4f1b..93b23ce6d 100644 --- a/CodeListLibrary_project/clinicalcode/api/views/Concept.py +++ b/CodeListLibrary_project/clinicalcode/api/views/Concept.py @@ -135,9 +135,12 @@ def get_concept_detail(request, concept_id, version_id=None, export_codes=False) incl_attributes=True ) for code in concept_codes: - code['attributes'] = dict(zip( - historical_concept.code_attribute_header, code['attributes'] - )) + attributes = code.get('attributes') + headers = historical_concept.code_attribute_header + if attributes is not None and headers is not None: + code['attributes'] = dict(zip( + headers, attributes + )) return Response( data=concept_codes, diff --git a/CodeListLibrary_project/clinicalcode/entity_utils/api_utils.py b/CodeListLibrary_project/clinicalcode/entity_utils/api_utils.py index bd6f76e56..26ab7abf6 100644 --- a/CodeListLibrary_project/clinicalcode/entity_utils/api_utils.py +++ b/CodeListLibrary_project/clinicalcode/entity_utils/api_utils.py @@ -3,6 +3,7 @@ from rest_framework import status from django.db.models.functions import JSONObject from django.db.models import ForeignKey, F +from rest_framework.renderers import JSONRenderer from ..models.GenericEntity import GenericEntity from ..models.Template import Template @@ -16,6 +17,11 @@ from . import gen_utils from . import constants +""" REST renderer """ +class PrettyJsonRenderer(JSONRenderer): + def get_indent(self, accepted_media_type, renderer_context): + return 2 + """ Parameter validation """ def is_malformed_entity_id(primary_key): diff --git a/CodeListLibrary_project/clinicalcode/entity_utils/concept_utils.py b/CodeListLibrary_project/clinicalcode/entity_utils/concept_utils.py index 24296da7b..541451416 100644 --- a/CodeListLibrary_project/clinicalcode/entity_utils/concept_utils.py +++ b/CodeListLibrary_project/clinicalcode/entity_utils/concept_utils.py @@ -453,9 +453,12 @@ def get_concept_component_details(concept_id, concept_history_id, aggregate_code codes = list(codes) if format_for_api: for code in codes: - code['attributes'] = dict(zip( - historical_concept.code_attribute_header, code['attributes'] - )) + attributes = code.get('attributes') + headers = historical_concept.code_attribute_header + if attributes is not None and headers is not None: + code['attributes'] = dict(zip( + headers, attributes + )) # Append codes to component if required if include_codes: diff --git a/CodeListLibrary_project/clinicalcode/entity_utils/create_utils.py b/CodeListLibrary_project/clinicalcode/entity_utils/create_utils.py index 1e6ecaa0e..ac74226ab 100644 --- a/CodeListLibrary_project/clinicalcode/entity_utils/create_utils.py +++ b/CodeListLibrary_project/clinicalcode/entity_utils/create_utils.py @@ -455,6 +455,10 @@ def validate_concept_form(form, errors): component_code.get('attributes'), 'string_array' ) if isinstance(code_attributes, list): + if len(set(code_attributes)) != len(code_attributes): + errors.append(f'Invalid concept with ID {concept_id} - attribute headers must be unique.') + return None + code_attributes = code_attributes[:len(attribute_headers)] code['attributes'] = code_attributes diff --git a/CodeListLibrary_project/cll/settings.py b/CodeListLibrary_project/cll/settings.py index 58420170f..73aa62cac 100644 --- a/CodeListLibrary_project/cll/settings.py +++ b/CodeListLibrary_project/cll/settings.py @@ -360,8 +360,7 @@ def get_env_value(env_variable, cast=None): if not BROWSABLEAPI: REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'] = ( - 'rest_framework.renderers.JSONRenderer', - 'rest_framework_xml.renderers.XMLRenderer', + 'clinicalcode.entity_utils.api_utils.PrettyJsonRenderer', ) #==============================================================================# diff --git a/CodeListLibrary_project/cll/templates/clinicalcode/generic_entity/detail/detail_buttons.html b/CodeListLibrary_project/cll/templates/clinicalcode/generic_entity/detail/detail_buttons.html index b530a52d1..3dd5c01e4 100644 --- a/CodeListLibrary_project/cll/templates/clinicalcode/generic_entity/detail/detail_buttons.html +++ b/CodeListLibrary_project/cll/templates/clinicalcode/generic_entity/detail/detail_buttons.html @@ -21,14 +21,6 @@ JSON -
  • - - XML - -
  • diff --git a/CodeListLibrary_project/cll/templates/components/details/outputs/api.html b/CodeListLibrary_project/cll/templates/components/details/outputs/api.html index c8a8faebc..7d844f855 100644 --- a/CodeListLibrary_project/cll/templates/components/details/outputs/api.html +++ b/CodeListLibrary_project/cll/templates/components/details/outputs/api.html @@ -15,23 +15,6 @@

    - site_root{% url 'api:get_generic_entity_detail_by_version' entity.id entity.history_id %}?format=xml - - {% else %} - - site_root{% url 'api:get_generic_entity_detail_by_version' entity.id entity.history_id %}?format=xml - - {% endif %} - - - JSON @@ -118,16 +101,6 @@

    - site_root{% url 'api:get_generic_entity_field_by_version' entity.id entity.history_id 'codes' %}?format=xml - - - JSON diff --git a/CodeListLibrary_project/cll/templates/components/details/outputs/phenotype_clinical_code_lists.html b/CodeListLibrary_project/cll/templates/components/details/outputs/phenotype_clinical_code_lists.html index 38d9be57d..9cf83a5f9 100644 --- a/CodeListLibrary_project/cll/templates/components/details/outputs/phenotype_clinical_code_lists.html +++ b/CodeListLibrary_project/cll/templates/components/details/outputs/phenotype_clinical_code_lists.html @@ -42,14 +42,6 @@ JSON -
  • - - XML - -