Commit 2165d08b authored by abuddenberg's avatar abuddenberg
Browse files

Refactored image paths. Added convenience method to download all images for a figure

parent dc6044a4
......@@ -3,8 +3,8 @@
from base64 import b64encode
import urllib
import json
from wsgiref import headers
import requests
from os.path import exists
from domain import Figure, Image
......@@ -77,8 +77,8 @@ class GcisClient(object):
def create_image(self, image, report_id=None, figure_id=None):
url = '{b}/image/'.format(b=self.base_url, img=image.identifier)
responses = [requests.post(url, image.as_json(), headers=self.headers)]
if image.filepath is not None:
responses.append(self.upload_image_file(image.identifier, image.filepath))
if image.local_path is not None:
responses.append(self.upload_image_file(image.identifier, image.local_path))
if figure_id and report_id:
responses.append(self.associate_image_with_figure(image.identifier, report_id, figure_id))
......@@ -98,11 +98,14 @@ class GcisClient(object):
url = '{b}/report/{rpt}/figure/rel/{fig}'.format(b=self.base_url, rpt=report_id, fig=figure_id)
return requests.post(url, json.dumps({'add_image_identifier': image_id}), headers=self.headers)
def upload_image_file(self, image_id, filepath):
def upload_image_file(self, image_id, local_path):
url = '{b}/image/files/{id}'.format(b=self.base_url, id=image_id)
# For future multi-part encoding support
# return requests.put(url, headers=headers, files={'file': (filename, open(filepath, 'rb'))})
return requests.put(url, data=open(filepath, 'rb'), headers=self.headers)
if not exists(local_path):
raise Exception('File not found: ' + local_path)
return requests.put(url, data=open(local_path, 'rb'), headers=self.headers)
#Full listing
def get_figure_listing(self, report_id, chapter_id=None):
......
......@@ -3,7 +3,7 @@
import urllib
import requests
import re
from os.path import exists, join
from os.path import join
from domain import Figure, Image
......@@ -62,8 +62,8 @@ class WebformClient:
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 exists(join(self.images_dir, filename))
# def local_image_exists(self, 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)
......@@ -71,12 +71,12 @@ class WebformClient:
print resp.status_code, resp.text
return True if resp.status_code == 200 else False
def download_image(self, path):
url = '{b}{path}?token={t}'.format(b=self.base_url, path=path, t=self.token)
def download_image(self, image):
url = '{b}{path}?token={t}'.format(b=self.base_url, path=image.remote_path, t=self.token)
resp = requests.get(url, stream=True)
if resp.status_code == 200:
filepath = join(self.images_dir, path.split('/')[-1])
filepath = join(self.images_dir, image.remote_path.split('/')[-1])
with open(filepath, 'wb') as image_out:
for bytes in resp.iter_content(chunk_size=4096):
image_out.write(bytes)
......@@ -85,3 +85,9 @@ class WebformClient:
else:
return resp
def download_all_images(self, figure):
responses = []
for image in figure.images:
responses.append(self.download_image(image))
return responses
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