Commit 69868dc5 authored by abuddenberg's avatar abuddenberg
Browse files

Adding higher-level functions to CRUD figures with images. Added example usage

parent a2db51cf
......@@ -101,12 +101,12 @@ class Image(Gcisbase):
'what_is_the_name_of_the_image_listed_in_the_report': 'title'
}
def __init__(self, data, filename=None):
def __init__(self, data, filepath=None):
super(Image, self).__init__(data, fields=self._gcis_fields, trans=self._translations)
#Hack
self.identifier = self.identifier.replace('/image/', '')
self.filename = filename
self.filepath = filepath
def as_json(self):
out_fields = self._gcis_fields
......
from gcis_client import GcisClient
base_url = 'http://data.gcis-dev-front.joss.ucar.edu'
gcis = GcisClient(base_url, 'andrew.buddenberg@noaa.gov', 'fcee8e3f11f36313e463ece51aab15242f71f3d552d565be')
# #Let's pull a list of all figures in Chapter 2 of the NCA3draft
# for partial_figure in gcis.get_figure_listing('nca3draft', chapter_id='our-changing-climate'):
# #The listing doesn't provide all available fields for the figure (Images, for instance).
# #There aren't very many figures, so let's go ahead and grab a complete version of each
# full_figure = gcis.get_figure('nca3draft', partial_figure.identifier, chapter_id='our-changing-climate')
# print full_figure
#Let's work with the infamous temperature figure
fig2_6 = gcis.get_figure('nca3draft', 'temperature-change')
#Warning: Images and Chapters are specifically excluded from JSON output. This is what gets sent to GCIS. So...
print fig2_6.as_json(indent=4)
#...you might want to view the original input
print fig2_6.original
#Let's see about the images:
for image in fig2_6.images:
print image
......@@ -27,16 +27,49 @@ class GcisClient:
self.base_url = url
self.headers['Authorization'] = 'Basic ' + b64encode(username + ':' + password)
def update_figure(self, report_id, figure):
def create_figure(self, report_id, chapter_id, figure, skip_images=False):
if figure.identifier in (None, ''):
raise Exception('Invalid identifier', figure.identifier)
url = '{b}/report/{rpt}/chapter/{chp}/figure/{fig}'.format(
b=self.base_url, rpt=report_id, chp=chapter_id, fig=figure.identifier
)
responses = [requests.post(url, figure.as_json(), headers=self.headers)]
if skip_images is False:
for image in figure.images:
responses.append(
(self.create_image(image),
self.associate_image_with_figure(image.identifier, report_id, figure.identifier))
)
return responses
def update_figure(self, report_id, figure, skip_images=False):
if figure.identifier in (None, ''):
raise Exception('Invalid identifier', figure.identifier)
update_url = '{b}/report/{rpt}/figure/{fig}'.format(b=self.base_url, rpt=report_id, fig=figure.identifier)
return requests.post(update_url, figure.as_json(), headers=self.headers)
responses = [requests.post(update_url, figure.as_json(), headers=self.headers)]
if skip_images is False:
for image in figure.images:
responses.append(self.update_image(image))
return responses
def delete_figure(self, report_id, figure_id):
url = '{b}/report/{rpt}/figure/{fig}'.format(b=self.base_url, rpt=report_id, fig=figure_id)
return requests.delete(url, headers=self.headers)
@check_image
def create_image(self, image):
update_url = '{b}/image/'.format(b=self.base_url, img=image.identifier)
return requests.post(update_url, image.as_json(), headers=self.headers)
url = '{b}/image/'.format(b=self.base_url, img=image.identifier)
responses = [requests.post(url, image.as_json(), headers=self.headers)]
if image.filename is not None:
responses.append(self.upload_image_file(image.identifier, image.filename))
return responses
@check_image
def update_image(self, image):
......
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