Refactor code

This commit is contained in:
Ramon Wenger 2022-09-13 16:39:24 +02:00
parent 7fae655543
commit 759cece268
1 changed files with 20 additions and 17 deletions

View File

@ -25,6 +25,7 @@ class GenericStreamFieldType(Scalar):
return list(augment_fields(raw_data)) return list(augment_fields(raw_data))
def get_document_json(document_id): def get_document_json(document_id):
try:
document = CustomDocument.objects.get(id=document_id) document = CustomDocument.objects.get(id=document_id)
value = { value = {
'value': document_id, 'value': document_id,
@ -36,6 +37,9 @@ def get_document_json(document_id):
'display_text': document.display_text 'display_text': document.display_text
} }
return value return value
except CustomDocument.DoesNotExist:
logger.error('CustomDocument {} does not exist'.format(document_id))
return None
def augment_fields(raw_data): def augment_fields(raw_data):
@ -102,17 +106,16 @@ def augment_fields(raw_data):
data['value'] = augment_fields(item_data) data['value'] = augment_fields(item_data)
if _type == 'cms_document_block': if _type == 'cms_document_block':
try:
_value = data['value'] _value = data['value']
value = get_document_json(_value) value = get_document_json(_value)
if value is not None:
data['value'] = value data['value'] = value
except CustomDocument.DoesNotExist:
logger.error('CustomDocument {} does not exist'.format(_value))
if _type == 'solution' or _type == 'instruction': if _type == 'solution' or _type == 'instruction':
_value = data['value'] _value = data['value']
document_id = _value.get('document') document_id = _value.get('document')
if document_id is not None: if document_id is not None:
document = get_document_json(document_id) document = get_document_json(document_id)
if document is not None:
_value['document'] = document _value['document'] = document
data['value'] = _value data['value'] = _value