Skip to content

Commit

Permalink
Handle missing media file gracefully #43
Browse files Browse the repository at this point in the history
  • Loading branch information
joemull committed Feb 19, 2024
1 parent 01fc19c commit faff5c3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ def load_json_with_decimals(file_ref):
)


def open_media_file(filename):
"""
Opens a JSON file saved as a Janeway media file.
Logs an error if the file is not found.
"""
try:
file = cms_models.MediaFile.objects.get(label=filename)
with file.file.open('r') as file_ref:
return load_json_with_decimals(file_ref)
except cms_models.MediaFile.DoesNotExist as e:
logger.error(e)
logger.error(f'...while trying to load {filename}')
return []


def open_saved_world_bank_data(indicator: str, year: int) -> List:
"""
Opens saved API data for indicator
Expand All @@ -115,9 +130,7 @@ def open_saved_world_bank_data(indicator: str, year: int) -> List:
plugin_settings.SHORT_NAME,
f'{indicator}_{year}.json',
)
file = cms_models.MediaFile.objects.get(label=filename)
with file.file.open('r') as file_ref:
return load_json_with_decimals(file_ref)
return open_media_file(filename)


def get_abstract_band(size, level, country, currency):
Expand Down Expand Up @@ -267,6 +280,4 @@ def update_demo_band_data():

def get_saved_demo_band_data() -> List:
filename = os.path.join(plugin_settings.SHORT_NAME, DEMO_DATA_FILENAME)
file = cms_models.MediaFile.objects.get(label=filename)
with file.file.open('r') as file_ref:
return load_json_with_decimals(file_ref)
return open_media_file(filename)

0 comments on commit faff5c3

Please sign in to comment.