Skip to content
Snippets Groups Projects
Commit d5394e04 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

handle errors better

parent f21d277b
No related branches found
No related tags found
No related merge requests found
...@@ -2,12 +2,27 @@ from django.conf import settings ...@@ -2,12 +2,27 @@ from django.conf import settings
import json import json
import urllib import urllib
from os.path import join
def get_automation_id_choices(): def get_automation_id_choices():
base_url = getattr(settings, 'AUTOMATION_BASE_URL', None) base_url = getattr(settings, 'AUTOMATION_BASE_URL', None)
cache_dir = getattr(settings, 'AUTOMATION_CACHE_DIR', 'cache')
cached_shows = join(cache_dir, 'shows.json')
shows = [] shows = []
if base_url: if base_url:
shows_list = json.load(urllib.urlopen(base_url))['shows'] try:
shows_json = urllib.urlopen(base_url).read()
shows_list = json.loads(shows_json)['shows']
except IOError:
try:
with open(cached_shows) as cache:
shows_list = json.loads(cache.read())['shows']
except IOError:
shows_list = []
else:
with open(cached_shows, 'w') as cache:
cache.write(shows_json)
shows = [(s['id'], s['title']) for s in shows_list] shows = [(s['id'], s['title']) for s in shows_list]
return shows return shows
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment