Skip to content

Commit

Permalink
Updated workingset (#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackScanlon authored Jan 22, 2025
2 parents 2c4e168 + 813009c commit b8ac044
Show file tree
Hide file tree
Showing 17 changed files with 5,655 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,10 @@ def build_final_codelist_from_concepts(
if include_headers:
concept_data |= { 'code_attribute_header': concept_entity.code_attribute_header}

if 'attributes' in concept and concept['attributes']:
concept_data |= {'attributes': concept['attributes']}


# Get codes
concept_codes = concept_utils.get_concept_codelist(
concept_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,9 @@ def get_minimal_concept_data(concept):
'coding_system': model_utils.get_coding_system_details(concept.coding_system)
} | concept_data

def get_clinical_concept_data(concept_id, concept_history_id, include_reviewed_codes=False,
def get_clinical_concept_data(concept_id, concept_history_id,include_reviewed_codes=False,
aggregate_component_codes=False, include_component_codes=True,
include_attributes=False, strippable_fields=None,
include_attributes=False, strippable_fields=None,concept_attributes=None,
remove_userdata=False, hide_user_details=False,
derive_access_from=None, requested_entity_id=None,
format_for_api=False, include_source_data=False):
Expand All @@ -853,6 +853,7 @@ def get_clinical_concept_data(concept_id, concept_history_id, include_reviewed_c
include_attributes (boolean): Should we include attributes?
strippable_fields (list): Whether to strip any fields from the Concept model when
building the concept's data result
concept_attributes (list): The workingset list of attributes
remove_userdata (boolean): Whether to remove userdata related fields from the result (assoc. with each Concept)
derive_access_from (RequestContext): Using the RequestContext, determine whether a user can edit a Concept
format_for_api (boolean): Flag to format against legacy API
Expand Down Expand Up @@ -990,6 +991,7 @@ def get_clinical_concept_data(concept_id, concept_history_id, include_reviewed_c
'concept_version_id': concept_history_id,
'coding_system': model_utils.get_coding_system_details(historical_concept.coding_system),
'details': concept_data,
'attributes': concept_attributes,
'components': components_data.get('components') if components_data is not None else [],
}
else:
Expand Down
36 changes: 32 additions & 4 deletions CodeListLibrary_project/clinicalcode/entity_utils/create_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ def get_template_creation_data(request, entity, layout, field, default=None):
if field_type == 'concept':
values = []
for item in data:
workingset_concept_attributes = None
if 'attributes' in item:
workingset_concept_attributes = item['attributes']
value = concept_utils.get_clinical_concept_data(
item['concept_id'],
item['concept_version_id'],
concept_attributes=workingset_concept_attributes,
aggregate_component_codes=False,
requested_entity_id=entity.id,
derive_access_from=request,
Expand Down Expand Up @@ -506,6 +510,28 @@ def validate_concept_form(form, errors):
'components': [ ],
}

# Validate concept attributes based on their type
concept_attributes = []
if form.get('attributes'):
for attribute in form.get('attributes'):
if attribute['value']:
attribute_type = attribute.get('type')
attribute_value = attribute.get('value')
if not gen_utils.is_empty_string(attribute_value):
if attribute_type == 'INT':
try:
attribute_value = str(int(attribute_value))
except ValueError:
errors.append(f'Attribute {attribute["name"]} must be an integer.')
continue
elif attribute_type == 'FLOAT':
try:
attribute['value'] = str(float(attribute_value))
except ValueError:
errors.append(f'Attribute {attribute["name"]} must be a float.')
continue
concept_attributes.append(attribute)

if not is_new_concept and concept_id is not None and concept_history_id is not None:
concept = model_utils.try_get_instance(Concept, id=concept_id)
concept = model_utils.try_get_entity_history(concept, history_id=concept_history_id)
Expand All @@ -514,6 +540,7 @@ def validate_concept_form(form, errors):
return None
field_value['concept']['id'] = concept_id
field_value['concept']['history_id'] = concept_history_id
field_value['concept']['attributes'] = concept_attributes
else:
is_new_concept = True

Expand Down Expand Up @@ -661,6 +688,7 @@ def validate_concept_form(form, errors):
field_value['concept']['name'] = concept_name
field_value['concept']['coding_system'] = concept_coding
field_value['concept']['code_attribute_header'] = attribute_headers
field_value['concept']['attributes'] = concept_attributes
field_value['components'] = components

return field_value
Expand Down Expand Up @@ -1163,7 +1191,7 @@ def build_related_entities(request, field_data, packet, override_dirty=False, en
if override_dirty or concept.get('is_dirty'):
result = try_update_concept(request, item)
if result is not None:
entities.append({'method': 'update', 'entity': result, 'historical': result.history.latest() })
entities.append({'method': 'update', 'entity': result, 'historical': result.history.latest(), 'attributes': concept.get('attributes')})
continue

# If we're not dirty, append the current concept
Expand All @@ -1172,20 +1200,20 @@ def build_related_entities(request, field_data, packet, override_dirty=False, en
result = model_utils.try_get_instance(Concept, id=concept_id)
historical = model_utils.try_get_entity_history(result, history_id=concept_history_id)
if historical is not None:
entities.append({ 'method': 'set', 'entity': result, 'historical': historical })
entities.append({ 'method': 'set', 'entity': result, 'historical': historical, 'attributes': concept.get('attributes') })
continue

# Create new concept & components
result = try_create_concept(request, item, entity=entity)
if result is None:
continue
entities.append({ 'method': 'create', 'entity': result, 'historical': result.history.latest() })
entities.append({ 'method': 'create', 'entity': result, 'historical': result.history.latest(), 'attributes': concept.get('attributes') })

# Build concept list
return True, [
'phenotype_owner',
[obj.get('entity') for obj in entities if obj.get('method') == 'create'],
[{ 'concept_id': obj.get('historical').id, 'concept_version_id': obj.get('historical').history_id } for obj in entities]
[{ 'concept_id': obj.get('historical').id, 'concept_version_id': obj.get('historical').history_id, 'attributes': obj.get('attributes')} for obj in entities]
]

return False, None
Expand Down
Loading

0 comments on commit b8ac044

Please sign in to comment.