Commit 362926e4 authored by abuddenberg's avatar abuddenberg
Browse files

Image deletes and associates working

parent 351cbeb4
__author__ = 'abuddenberg'
from copy import deepcopy
import json
class Gcisbase(object):
def __init__(self, _gcis_fields, **kwargs):
self. __dict__.update(dict.fromkeys(_gcis_fields, None))
original = None
def __init__(self, data, fields=[], trans={}):
#Save off a copy of the original JSON for debugging
self.original = deepcopy(data)
#Create attributes from the master list
self. __dict__.update(dict.fromkeys(fields, None))
for k in kwargs:
#Perform translations
for term in trans:
val = data.pop(term, None)
if val is not None:
data[trans[term]] = val
for k in data:
if hasattr(self, k):
setattr(self, k, kwargs[k])
#Strip whitespace from strings for consistency
try:
data[k] = data[k].strip()
except AttributeError:
pass
finally:
setattr(self, k, data[k])
class Figure(Gcisbase):
_gcis_fields = [
'usage_limits', 'kindred_figures', 'time_end', 'keywords', 'lat_min', 'create_dt', 'lat_max', 'time_start',
'uuid', 'title', 'ordinal', 'lon_min', 'report_identifier', 'chapter', 'submission_dt', 'uri', 'lon_max',
'caption', 'source_citation', 'attributes', 'identifier', 'chapter_identifier'
'caption', 'source_citation', 'attributes', 'identifier', 'chapter_identifier', 'images'
]
_translations = {
......@@ -22,21 +43,18 @@ class Figure(Gcisbase):
'when_was_this_figure_created': 'create_dt'
}
def __init__(self, **kwargs):
#Create attributes from the master list
self. __dict__.update(dict.fromkeys(self._gcis_fields, None))
def __init__(self, data):
super(Figure, self).__init__(data, fields=self._gcis_fields, trans=self._translations)
#Special case for chapter
chap_tree = kwargs.pop('chapter', None)
self.chapter = Chapter(**chap_tree) if chap_tree else None
chap_tree = data.pop('chapter', None)
self.chapter = Chapter(chap_tree) if chap_tree else None
#Special case for images
image_list = kwargs.pop('images', None)
self.images = [Image(**image) for image in image_list if len()]
image_list = data.pop('images', None)
self.images = [Image(image) for image in image_list] if image_list else None
for k in kwargs:
if hasattr(self, k):
setattr(self, k, kwargs[k])
@property
def figure_num(self):
......@@ -45,6 +63,11 @@ class Figure(Gcisbase):
else:
return None
def as_json(self):
#Exclude a couple of fields
out_fields = set(self._gcis_fields) - set(['images', 'chapter'])
return json.dumps({f: self.__dict__[f] for f in out_fields})
def __str__(self):
return 'Figure: {f_num}: {f_name}'.format(f_num=self.figure_num, f_name=self.title)
......@@ -52,23 +75,40 @@ class Figure(Gcisbase):
return super(Figure, self).__repr__()
class Chapter(object):
class Chapter(Gcisbase):
_gcis_fields = ['report_identifier', 'identifier', 'number', 'url', 'title']
def __init__(self, **kwargs):
self. __dict__.update(dict.fromkeys(self._gcis_fields, None))
def __init__(self, data):
super(Chapter, self).__init__(data, fields=self._gcis_fields)
for k in kwargs:
if hasattr(self, k):
setattr(self, k, kwargs[k])
class Image(Gcisbase):
_gcis_fields = ['attributes', 'create_dt', 'description', 'identifier', 'lat_max', 'lat_min', 'lon_max', 'lon_min',
'position', 'submission_dt', 'time_end', 'time_start', 'title', 'usage_limits']
_translations = {
'list_any_keywords_for_the_image': 'attributes',
'when_was_this_image_created': 'create_dt',
'what_is_the_image_id': 'identifier',
'maximum_latitude': 'lat_max',
'minimum_latitude': 'lat_min',
'maximum_longitude': 'lon_max',
'minimum_longitude': 'lon_min',
'start_time': 'time_start',
'end_time': 'time_end',
'what_is_the_name_of_the_image_listed_in_the_report': 'title'
}
def __init__(self, data, filename=None):
super(Image, self).__init__(data, fields=self._gcis_fields, trans=self._translations)
class Image(object):
_gcis_fields = []
#Hack
self.identifier = self.identifier.replace('/image/', '')
self.filename = filename
def __init__(self, **kwargs):
self. __dict__.update(dict.fromkeys(self._gcis_fields, None))
def as_json(self):
out_fields = self._gcis_fields
return json.dumps({f: self.__dict__[f] for f in out_fields})
for k in kwargs:
if hasattr(self, k):
setattr(self, k, kwargs[k])
\ No newline at end of file
def __str__(self):
return 'Image: {id} {name}'.format(id=self.identifier, name=self.title)
#!/usr/bin/python
import httplib
import urllib
import json
from domain import Figure, Image
from pickle import dump
base_url = 'data.gcis-dev-front.joss.ucar.edu'
# base_url = 'http://data-stage.globalchange.gov/report/nca3'
headers = {
'Accept': 'application/json',
'Authorization': 'Basic YW5kcmV3LmJ1ZGRlbmJlcmdAbm9hYS5nb3Y6ZjBiNDc1OTUyMDY2MWY5M2U0N2E5Yzc4NjY1NWJjZjg0ZTZmZTU1NzUyYWI0ZmIx'
}
conn = httplib.HTTPConnection(base_url)
def check_image(fn):
def wrapped(*args, **kwargs):
# if len(args) < 1 or not isinstance(args[0], Image):
# raise Exception('Invalid Image')
if args[0].identifier in (None, ''):
raise Exception('Invalid identifier', args[0].identifier)
fn(*args, **kwargs)
return wrapped
def main():
f = get_figure(report='nca3draft', chapter='our-changing-climate', figure='temperature-change')
for i in f.images:
# print i.as_json()
i.identifier = ''
delete_image(i)
def update_figure(figure):
if figure.identifier in (None, ''):
raise Exception('Invalid identifier', figure.identifier)
@check_image
def create_image(image):
update_url = '/image/'.format(img=image.identifier)
conn.request('POST', update_url, image.as_json(), headers)
resp = conn.getresponse()
return resp.status, resp.reason, resp.read()
@check_image
def update_image(image):
update_url = '/image/{img}'.format(img=image.identifier)
conn.request('POST', update_url, image.as_json(), headers)
resp = conn.getresponse()
return resp.status, resp.reason, resp.read()
@check_image
def delete_image(image):
delete_url = '/image/{img}'.format(img=image.identifier)
conn.request('DELETE', delete_url, None, headers)
resp = conn.getresponse()
return resp.status, resp.reason, resp.read()
def associate_image_with_figure(image_id, report, figure_id):
url = '/report/{rpt}/figure/rel/{fig}'.format(rpt=report, fig=figure_id)
conn.request('POST', url, json.dumps({'add_image_identifier': image_id}), headers)
resp = conn.getresponse()
return resp.status, resp.reason, resp.read()
def upload_image_file(image_id)
#Full listing
def get_figure_listing(report, chapter=None):
chapter_filter = 'chapter/' + chapter if chapter else ''
url = '/report/{rpt}/{chap}figure?{p}'.format(rpt=report, chap=chapter_filter, p=urllib.urlencode({'all': '1'}))
conn.request('GET', url, None, headers)
resp = conn.getresponse()
return [Figure(figure) for figure in json.load(resp.read())]
def get_figure(report, figure, chapter=None):
chapter_filter = '/chapter' + chapter if chapter else ''
url = '/report/{rpt}/{chap}figure/{fig}?{p}'.format(rpt=report, chap=chapter_filter, fig=figure, p=urllib.urlencode({'all': '1'}))
conn.request('GET', url, None, headers)
resp = conn.getresponse()
return Figure(json.load(resp))
if __name__ == "__main__":
main()
......@@ -5,6 +5,9 @@ import urllib2
import json
import re
from domain import Figure, Image
from gcis_client import update_image, get_figure, create_image, associate_image_with_figure
prod = {'base': 'http://resources.assessment.globalchange.gov', 'token': 'mgTD63FAjG'}
dev_base = 'http://dev.nemac.org/asides10/metadata/figures/all?token=A2PNYxRuG9'
......@@ -35,15 +38,51 @@ def get_all_webforms():
def get_webform(url):
prod['url'] = url
full_url = '{base}{url}?token={token}'.format(**prod)
figure = json.load(urllib2.urlopen(full_url))
figure_json = json.load(urllib2.urlopen(full_url))
return figure
#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']]
return f
# for listing in get_list():
# print listing
#Hack
file_map = {
'69da6d93-4426-4061-a2a1-7b3d01f2dc1c': '../AK.jpg',
'230cb2f8-92e0-4897-ab5f-4d6339673832': '../US.jpg',
'1f5a3cdd-fc45-403e-bf11-d1772005b430': '../GPN.jpg',
'b180cfd9-b064-4644-a9a1-d2c3660c1be7': '../MW.jpg',
'fa83c34b-7b67-4b74-bcba-5bf60ba7730f': '../NE.jpg',
'ca983a87-53a7-4c42-b0e9-18d26fad40ba': '../SE.jpg',
'68537d68-b14c-4811-908a-5dc0ab73879b': '../GPS.jpg',
'26a28c2a-75f2-47f7-a40f-becfc468d3d6': '../SW.jpg',
'f69194e8-397d-4f9c-836c-335d259ee09c': '../HI.jpg',
'db4d291d-17c5-4e10-b760-6c8799a8d709': '../NW.jpg',
'8e74f576-a5af-46c0-b33a-f30072118b86': '../usgcrp_draft-038-012.jpg',
}
figure = get_webform('/metadata/figures/3175')
for image in figure.images:
image.filename = file_map[image.identifier]
# f = get_figure(report='nca3draft', chapter='our-changing-climate', figure='temperature-change')
#
# print json.dumps(f.original, indent=4)
# for i in figure.images[0:-2]:
# print create_image(i)
# print associate_image_with_figure('69da6d93-4426-4061-a2a1-7b3d01f2dc1c', 'nca3draft', 'temperature-change')
for listing in get_list():
print listing
url = '/metadata/figures/2681'
print json.dumps(get_webform(url), indent=4, sort_keys=True)
# print json.dumps(get_webform(url), indent=4, sort_keys=True)
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