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,17 +25,21 @@ 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):
document = CustomDocument.objects.get(id=document_id) try:
value = { document = CustomDocument.objects.get(id=document_id)
'value': document_id, value = {
'id': document.id, 'value': document_id,
'file_name': document.filename, 'id': document.id,
'file_extension': document.file_extension, 'file_name': document.filename,
'url': document.url, 'file_extension': document.file_extension,
'title': document.title, 'url': document.url,
'display_text': document.display_text 'title': document.title,
} '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,18 +106,17 @@ 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)
_value['document'] = document if document is not None:
_value['document'] = document
data['value'] = _value data['value'] = _value