Commit dc6044a4 authored by abuddenberg's avatar abuddenberg
Browse files

Separated local and remote paths for Images. Downloads more robust

parent 763b5445
......@@ -62,7 +62,7 @@ class Figure(Gcisbase):
self.images = [Image(image) for image in image_list] if image_list else []
#Hack
self.identifier = self.identifier.replace('/figure/', '')
self.identifier = self.identifier.replace('/figure/', '') if self.identifier != '' else '***ID MISSING***'
@property
......@@ -110,18 +110,21 @@ class Image(Gcisbase):
'what_is_the_name_of_the_image_listed_in_the_report': 'title'
}
def __init__(self, data, filepath=None):
def __init__(self, data, local_path=None, remote_path=None):
super(Image, self).__init__(data, fields=self._gcis_fields, trans=self._translations)
#Hack
self.identifier = self.identifier.replace('/image/', '')
webform_filename = data.pop('what_is_the_file_name_extension_of_the_image', None)
if filepath is not None:
self.filepath = filepath
elif webform_filename is not None:
self.filepath = '/system/files/' + webform_filename.lower()
else:
self.filepath = None
# webform_filename = data.pop('what_is_the_file_name_extension_of_the_image', None)
# if local_path is not None:
# self.local_path = local_path
# elif webform_filename is not None:
# self.local_path = '/system/files/' + webform_filename.lower()
# else:
# self.local_path = None
self.local_path = local_path
self.remote_path = remote_path
def as_json(self, indent=0):
out_fields = self._gcis_fields
......
......@@ -3,7 +3,7 @@
import urllib
import requests
import re
import os.path
from os.path import exists, join
from domain import Figure, Image
......@@ -21,10 +21,11 @@ def sanitized(pattern):
class WebformClient:
def __init__(self, url, token, local_image_repo='../dist/images/'):
def __init__(self, url, token, local_image_dir='../dist/images/'):
self.base_url = url
self.token = token
self.images_dir = local_image_repo
self.images_dir = local_image_dir
self.remote_image_dir = '/system/files/'
def get_list(self):
url = '{b}/metadata/list?token={t}'.format(b=self.base_url, t=self.token)
......@@ -40,22 +41,29 @@ class WebformClient:
figure_json = requests.get(full_url).json()
#TODO: refactor the service so this isn't necessary
id = figure_json.keys()[0]
f = Figure(figure_json[id]['figure'][0])
# f.images = [Image(image) for image in figure_json[id]['images']]
for i in figure_json[id]['images']:
image = Image(i)
if image.filepath not in (None, ''):
#TODO: this sucks in every way; make it better
png_image = image.filepath.split('/')[-1].replace('.eps', '.png')
image.filepath = os.path.join(self.images_dir, png_image) if self.local_image_exists(png_image) else image.filepath
f.images.append(image)
figure_id = figure_json.keys()[0]
f = Figure(figure_json[figure_id]['figure'][0])
if 'images' in figure_json[figure_id]:
f.images = [
Image(image, local_path=self.get_local_image_path(image), remote_path=self.get_remote_image_path(image))
for image in figure_json[figure_id]['images']
]
return f
def get_remote_image_path(self, image_json):
filename_key = 'what_is_the_file_name_extension_of_the_image'
if image_json not in (None, '') and image_json[filename_key] not in (None, ''):
return self.remote_image_dir + image_json[filename_key].lower()
def get_local_image_path(self, image_json):
filename_key = 'what_is_the_file_name_extension_of_the_image'
if image_json not in (None, '') and image_json[filename_key] not in (None, ''):
return join(self.images_dir, image_json[filename_key].lower())
def local_image_exists(self, filename):
return os.path.exists(os.path.join(self.images_dir, filename))
return exists(join(self.images_dir, filename))
def remote_image_exists(self, path):
url = '{b}{path}?token={t}'.format(b=self.base_url, path=path, t=self.token)
......@@ -68,7 +76,7 @@ class WebformClient:
resp = requests.get(url, stream=True)
if resp.status_code == 200:
filepath = os.path.join(self.images_dir, path.split('/')[-1])
filepath = join(self.images_dir, path.split('/')[-1])
with open(filepath, 'wb') as image_out:
for bytes in resp.iter_content(chunk_size=4096):
image_out.write(bytes)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment