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))
def get_document_json(document_id):
document = CustomDocument.objects.get(id=document_id)
value = {
'value': document_id,
'id': document.id,
'file_name': document.filename,
'file_extension': document.file_extension,
'url': document.url,
'title': document.title,
'display_text': document.display_text
}
return value
try:
document = CustomDocument.objects.get(id=document_id)
value = {
'value': document_id,
'id': document.id,
'file_name': document.filename,
'file_extension': document.file_extension,
'url': document.url,
'title': document.title,
'display_text': document.display_text
}
return value
except CustomDocument.DoesNotExist:
logger.error('CustomDocument {} does not exist'.format(document_id))
return None
def augment_fields(raw_data):
@ -102,18 +106,17 @@ def augment_fields(raw_data):
data['value'] = augment_fields(item_data)
if _type == 'cms_document_block':
try:
_value = data['value']
value = get_document_json(_value)
_value = data['value']
value = get_document_json(_value)
if value is not None:
data['value'] = value
except CustomDocument.DoesNotExist:
logger.error('CustomDocument {} does not exist'.format(_value))
if _type == 'solution' or _type == 'instruction':
_value = data['value']
document_id = _value.get('document')
if document_id is not None:
document = get_document_json(document_id)
_value['document'] = document
if document is not None:
_value['document'] = document
data['value'] = _value