Commit bb90ecb3 authored by abuddenberg's avatar abuddenberg
Browse files

Merge branch 'surveys'

parents c49bfd2b 21fdef44
...@@ -93,3 +93,27 @@ Let's see about the images: ...@@ -93,3 +93,27 @@ Let's see about the images:
How about the whole Image?: How about the whole Image?:
print gcis.get_image('69da6d93-4426-4061-a2a1-7b3d01f2dc1c').as_json(indent=4) print gcis.get_image('69da6d93-4426-4061-a2a1-7b3d01f2dc1c').as_json(indent=4)
Want to find the lineage of a publication?
fig2_1 = gcis.get_figure('nca3', 'ten-indicators-of-a-warming-world')
print fig2_1.parents
Add a parent to a figure directly...
cmip3 = gcis.get_dataset('nca3-cmip3-r201205')
gcis.associate_figure_with_parent('nca3', 'ten-indicators-of-a-warming-world', Parent.from_obj(cmip3))
...or as part of an update
fig2_1.add_parent(Parent.from_obj(cmip3))
gcis.update_figure('nca3', 'our-changing-climate', fig2_1)
**NOTE:** GcisClient will disassociate and then reassociate Parents as part of an update. Adding a new Parent during an
update will cause "Parent dissociation failed" messages to appear--these can be safely ignored.
**ALSO:** Updates do NOT, presently, remove parents you have not included on the Figure object.
Remove a parent from a figure
gcis.delete_figure_parent_assoc('nca3', 'ten-indicators-of-a-warming-world', Parent.from_obj(cmip3))
...@@ -170,4 +170,6 @@ def sort_webform_list(report_id): ...@@ -170,4 +170,6 @@ def sort_webform_list(report_id):
if __name__ == '__main__': if __name__ == '__main__':
main() print 'The problems script is no longer maintained (and was a bad idea in the first place).'
# Think really hard about uncommenting this, then don't.
# main()
...@@ -420,7 +420,7 @@ def sync(replace=False): ...@@ -420,7 +420,7 @@ def sync(replace=False):
# #Remove existing parents # #Remove existing parents
for p in gcis_fig.parents: for p in gcis_fig.parents:
gcis.delete_figure_pub_assoc(report_id, gcis_id, p) gcis.delete_figure_parent_assoc(report_id, gcis_id, p)
for image in gcis_fig.images: for image in gcis_fig.images:
#TODO: There are better ways to do this. Build File support. #TODO: There are better ways to do this. Build File support.
...@@ -445,4 +445,6 @@ def cleanup_after_replace(): ...@@ -445,4 +445,6 @@ def cleanup_after_replace():
print gcis.associate_image_with_figure(image_id, 'nca3', figure_id) print gcis.associate_image_with_figure(image_id, 'nca3', figure_id)
if __name__ == '__main__': if __name__ == '__main__':
main() print 'The sync_figures script is no longer maintained and is for reference only.'
# Uncomment at your own risk.
# main()
#!/usr/bin/env python #!/usr/bin/env python
from gcis_clients import GcisClient, gcis_dev_auth from gcis_clients import GcisClient, gcis_dev_auth
from gcis_clients.domain import Parent
base_url = 'http://data.globalchange.gov' base_url = 'http://data.globalchange.gov'
...@@ -38,9 +39,17 @@ for image in fig2_7.images: ...@@ -38,9 +39,17 @@ for image in fig2_7.images:
#How about the whole Image? #How about the whole Image?
print gcis.get_image('69da6d93-4426-4061-a2a1-7b3d01f2dc1c').as_json(indent=4) print gcis.get_image('69da6d93-4426-4061-a2a1-7b3d01f2dc1c').as_json(indent=4)
#Let's assign some GCMD keywords (The first 4 from the list) #Want to find the lineage of a publication?
# keyword_ids = [k['identifier'] for k in gcis.get_keyword_listing()[0:4]] fig2_1 = gcis.get_figure('nca3', 'ten-indicators-of-a-warming-world')
print fig2_1.parents
# for keyword_id in keyword_ids: #Add a parent to a figure directly...
# resp = gcis.associate_keyword_with_figure(keyword_id, 'nca3draft', 'observed-us-temperature-change') cmip3 = gcis.get_dataset('nca3-cmip3-r201205')
# print resp.status_code, resp.text gcis.associate_figure_with_parent('nca3', 'ten-indicators-of-a-warming-world', Parent.from_obj(cmip3))
#...or as part of an update
fig2_1.add_parent(Parent.from_obj(cmip3))
gcis.update_figure('nca3', 'our-changing-climate', fig2_1)
#Remove a parent from a figure
gcis.delete_figure_parent_assoc('nca3', 'ten-indicators-of-a-warming-world', Parent.from_obj(cmip3))
__author__ = 'abuddenberg'
from gcis_clients import GcisClient, SurveyClient, survey_token, gcis_dev_auth, gcis_stage_auth
from gcis_clients.domain import Report, Chapter
from sync_utils import realize_parents, realize_contributors
from collections import OrderedDict
import pickle
import sys
gcis = GcisClient('http://data.gcis-dev-front.joss.ucar.edu', *gcis_dev_auth)
# gcis = GcisClient('https://data-review.globalchange.gov', *gcis_stage_auth)
surveys = SurveyClient('https://healthresources.globalchange.gov', survey_token)
sync_metadata_tree = {
'usgcrp-climate-and-health-assessment-draft': OrderedDict([
('executive-summary', []),
('climate-change-and-human-health', [
('/metadata/figures/3698', 'major_u_s__climate_trends'), #1.1 #climate-change-and-human-health
('/metadata/figures/3632', 'percent_changes_in_the_annual_number_of_extreme_precipitation_events_'), #1.2 #climate-change-and-human-health
('/metadata/figures/3635', 'projected_changes_in_temperature_and_precipitation_by_mid_century'), #1.3 #climate-change-and-human-health
('/metadata/figures/3633', 'projected_changes_in_the_hottest_coldest_and_wettest_driest_day_of_the_year'), #1.4 #climate-change-and-human-health
('/metadata/figures/3757', 'climate_change_and_health'), #1.5 #climate-change-and-human-health
]),
('temperature-related-death-and-illness', [
('/metadata/figures/3811', 'climate_change_and_health__extreme_heat'), #2.1 #temperature-related-death-and-illness
('/metadata/figures/3585', 'heat_related_deaths_during_the_1995_chicago_heat_wave'), #2.2 #temperature-related-death-and-illness
('/metadata/figures/3643', 'projected_net_changes_in_extreme_temperature_related_deaths'), #2.3 #temperature-related-death-and-illness
('/metadata/figures/3653', 'projected_changes_in_deaths_in_u_s__cities_by_season'), #2.4 #temperature-related-death-and-illness
]),
('air-quality-impacts', [
('/metadata/figures/3812', 'climate_change_and_health__outdoor_air_quality'), #3.1 #air-quality-impacts
('/metadata/figures/3647', 'projected_change_in_average_daily_maximum_temperature__seasonal_average_maximum_daily_8_hr_ozone__and_ozone_related_premature_deaths_in_2013'), #3.2 #air-quality-impacts
('/metadata/figures/3649', 'projected_change_in_ozone_related_premature_deaths'), #3.3 #air-quality-impacts
('/metadata/figures/3650', 'ragweed_pollen_season_lengthens'), #3.4 #air-quality-impacts
]),
('vectorborne-diseases', [
('/metadata/figures/3807', 'climate_change_and_health__lyme_disease'), #4.1 #vectorborne-diseases
('/metadata/figures/3659', 'changes_in_lyme_disease_case_report_distribution'), #4.2 #vectorborne-diseases
('/metadata/figures/3658', 'life_cycle_of_blacklegged_ticks__ixodes_scapularis'), #4.3 #vectorborne-diseases
('/metadata/figures/3747', 'lyme_disease_onset_week_modeling_scenarios'), #4.4 #vectorborne-diseases
('/metadata/figures/3674', 'incidence_of_west_nile_neuroinvasive_disease_in_the_united_states'), #4.5 #vectorborne-diseases
('/metadata/figures/3675', 'climate_impacts_on_west_nile_virus_transmission'), #4.6 #vectorborne-diseases
]),
('water-related-illnesses', [
('/metadata/figures/3824', 'climate_change_and_health___vibrio'), #5.1 #water-related-illnesses
('/metadata/figures/3700', 'links_between_climate_change__water_quantity_and_quality__and_human_exposure_to_water_related_illness'), #5.2 #water-related-illnesses #TOO BIG
('/metadata/figures/3671', 'locations_of_livestock_and_projections_of_heavy_precipitation'), #5.3 #water-related-illnesses #TOO BIG
('/metadata/figures/3673', 'potential_routes_of_manure_borne_microbial_contaminants_to_ground_and_surface_water_supplies_'), #5.3 #water-related-illnesses UNUSED?
('/metadata/figures/3709', 'projections_of_vibrio_occurrence_and_abundance_in_chesapeake_bay'), #5.4 #water-related-illnesses
('/metadata/figures/3704', 'changes_in_suitable_coastal_vibrio_habitat_in_alaska'), #5.5 #water-related-illnesses
('/metadata/figures/3734', 'projected_changes_in_caribbean_gambierdiscus_species'), #5.6 #water-related-illnesses
('/metadata/figures/3712', 'projections_of_growth_of_alexandrium_fundyense_in_puget_sound'), #5.7 #water-related-illnesses
]),
('food-safety--nutrition--and-distribution', [
('/metadata/figures/3579', 'farm_to_table'), #6.1 #food-safety--nutrition--and-distribution
## ('/metadata/figures/3600', 'mycotoxin_in_corn'), #6.1 #food-safety--nutrition--and-distribution BOX 1?
('/metadata/figures/3809', 'climate_change_and_health__salmonella'), #6.2 #food-safety--nutrition--and-distribution
('/metadata/figures/3748', 'seasonality_of_human_illnesses_associated_with_foodborne_pathogens'), #6.3 #food-safety--nutrition--and-distribution
('/metadata/figures/3688', 'effects_of_carbon_dioxide_on_protein_and_minerals'), #6.4 #food-safety--nutrition--and-distribution
('/metadata/figures/3597', 'mississippi_river_level_at_st__louis__missouri'), #6.5 #food-safety--nutrition--and-distribution
]),
('extreme-weather', [
('/metadata/figures/3810', 'estimated_deaths_and_billion_dollar_losses_from_extreme_weather_events_in_the_u_s__2004_2013'), #7.1 #extreme-weather #Has Activities
## ('/metadata/figures/3772', 'trends_in_flood_magnitude'), #7.2 #extreme-weather NOT USED
('/metadata/figures/3808', 'climate_change_and_health__flooding'), #7.2 #extreme-weather
('/metadata/figures/3760', 'hurricane_induced_flood_effects_in_eastern_and_central_united_states'), #7.3 #extreme-weather
]),
('mental-health-and-well-being', [
('/metadata/figures/3789', 'climate_change_and_mental_health'), #8.1 #mental-health-and-well-being
('/metadata/figures/3722', 'the_impact_of_climate_change_on_physical__mental__and_community_health'), #8.2 #mental-health-and-well-being
]),
('populations-of-concern', [
('/metadata/figures/3696', 'determinants_of_vulnerability'), #9.1 #populations-of-concern
('/metadata/figures/3694', 'social_determinants_of_health'), #9.2 #populations-of-concern
('/metadata/figures/3758', 'children_at_different_lifestages_experience_unique_vulnerabilities_to_climate_change'), #9.3 #populations-of-concern
('/metadata/figures/3714', 'mapping_social_vulnerability'), #9.4 #populations-of-concern
('/metadata/figures/3717', 'mapping_communities_vulnerable_to_heat_in_georgia'), #9.5 #populations-of-concern
]),
('appendix-1--technical-support-document', [
('/metadata/figures/3623', 'emissions_levels_determine_temperature_rises'), #1.1 #climate-change-and-human-health
('/metadata/figures/3759', 'the_shared_socioeconomic_pathways'), #1.2 #climate-change-and-human-health
('/metadata/figures/3726', 'example_spatial_resolution_of_climate_models'), #1.3 #climate-change-and-human-health
('/metadata/figures/3638', 'sensitivity_analysis_of_differences_in_modeling_approaches'), #1.4 #climate-change-and-human-health
])
])
}
def main():
print gcis.test_login()
# regenerate_image_id_map()
image_id_map = pickle.load(open('image_id_cache.pk1', 'r'))
# create_health_report()
for report_id in sync_metadata_tree:
for chapter_id in sync_metadata_tree[report_id]:
for survey_url, figure_id in sync_metadata_tree[report_id][chapter_id]:
print survey_url, gen_edit_link(survey_url)
figure, datasets = surveys.get_survey(survey_url, do_download=True)
realize_parents(gcis, figure.parents)
realize_contributors(gcis, figure.contributors)
print 'Contributors: ', figure.contributors
print 'Parents: ', figure.parents
for ds in [p for p in figure.parents if p.publication_type_identifier == 'dataset']:
# Assign synthetic activity identifier to for datasets associated with figure
if ds.activity and ds.activity.identifier is None:
ds.activity.identifier = generate_activity_id(figure, ds.publication)
print 'Dataset: ', ds.activity
#Create the figure in GCIS
# print 'Creating figure... ', gcis.create_figure(report_id, chapter_id, figure, skip_images=True)
print 'Updating figure... ', gcis.update_figure(report_id, chapter_id, figure, skip_images=True)
for i in figure.images:
i.identifier = image_id_map[i.identifier]
print '\t', i
realize_parents(gcis, i.parents)
realize_contributors(gcis, i.contributors)
print '\t\tContributors: ', i.contributors
print '\t\tParents: ', i.parents
for ds in [p for p in i.parents if p.publication_type_identifier == 'dataset']:
# Assign synthetic activity identifier to for datasets associated with images
if ds.activity and ds.activity.identifier is None:
ds.activity.identifier = generate_activity_id(i, ds.publication)
print '\t\tDataset: ', ds, ds.activity
#Create image in GCIS
# print 'Creating image... ', gcis.create_image(i, report_id=report_id, figure_id=figure_id)
print 'Updating image... ', gcis.update_image(i)
def gen_edit_link(survey_id):
node_id = survey_id.split('/')[-1]
return 'https://healthresources.globalchange.gov/node/' + node_id
def generate_activity_id(image, dataset):
try:
return '-'.join([image.identifier.split('-')[0], dataset.identifier, '-process'])
except Exception, e:
sys.stderr.write('WARNING: Activity identifier generation failed\n')
def regenerate_image_id_map(existing=None):
from uuid import uuid4
image_id_map = existing if existing else {}
for report_id in sync_metadata_tree:
for chapter_id in sync_metadata_tree[report_id]:
for survey_url, figure_id in sync_metadata_tree[report_id][chapter_id]:
s = surveys.get_survey(survey_url, do_download=False)
for img in s.images:
if img.identifier in image_id_map:
print 'skipping: ', img.identifier
continue
else:
print 'added: ', img.identifier
image_id_map[img.identifier] = str(uuid4())
with open('image_id_cache.pk1', 'wb') as fout:
pickle.dump(image_id_map, fout)
print 'image_id_map regenerated'
def gen_survey_list():
realized_list = []
chapters = [c for c in sync_metadata_tree['usgcrp-climate-and-health-assessment-draft']]
survey_list = surveys.get_list()
for i, survey in enumerate(survey_list):
url = survey['url']
print 'Processing: {b}{url} ({i}/{total})'.format(b=surveys.base_url, url=url, i=i + 1, total=len(survey_list))
s = surveys.get_survey(url)
chp_id = chapters[s.chapter] if s and s.chapter else None
if s:
print s.identifier
print chp_id, s.figure_num, s.title
realized_list.append((chp_id, s.figure_num, s.identifier, s.title, url))
print ''
return realized_list
def create_health_report():
hr = Report({
'identifier': 'usgcrp-climate-and-health-assessment-draft',
'report_type_identifier': 'assessment',
'title': 'Impacts of Climate Change on Human Health in the United States: A Scientific Assessment',
'url': 'http://www.globalchange.gov/health-assessment',
'publication_year': '2015',
'contact_email': 'healthreport@usgcrp.gov'
})
# ['report_identifier', 'identifier', 'number', 'title', 'url']
chapters = [
('executive-summary', None, 'Executive Summary'),
('climate-change-and-human-health', 1, 'Climate Change and Human Health'),
('temperature-related-death-and-illness', 2, 'Temperature-Related Death and Illness'),
('air-quality-impacts', 3, 'Air Quality Impacts'),
('vectorborne-diseases', 4, 'Vectorborne Diseases'),
('water-related-illnesses', 5, 'Climate Impacts on Water-Related Illnesses'),
('food-safety--nutrition--and-distribution', 6, 'Food Safety, Nutrition, and Distribution'),
('extreme-weather', 7, 'Impacts of Extreme Events on Human Health'),
('mental-health-and-well-being', 8, 'Mental Health and Well-Being'),
('populations-of-concern', 9, 'Climate-Health Risk Factors and Populations of Concern'),
('appendix-1--technical-support-document', None, 'Appendix 1: Technical Support Document')
]
print gcis.create_report(hr)
for id, num, title in chapters:
ch = Chapter({
'identifier': id,
'number': num,
'title': title,
'report_identifier': hr.identifier
})
print gcis.create_chapter(hr.identifier, ch)
def create_cmip5_report():
cmip = Report({
'identifier': 'noaa-techreport-nesdis-144',
'report_type_identifier': 'report',
'title': 'Regional Surface Climate Conditions in CMIP3 and CMIP5 for the United States: Differences, Similarities, and Implications for the U.S. National Climate Assessment',
'publication_year': '2015'
})
print gcis.create_report(cmip)
chapters = [
('introduction', 1, 'Introduction'),
('data', 2, 'Data'),
('methods', 3, 'Methods'),
('temperature', 4, 'Temperature'),
('precipitation', 5, 'Precipitation'),
('summary', 6, 'Summary'),
('appendix', None, 'Appendix'),
('references', None, 'References'),
('acknowledgements', None, 'Acknowledgements'),
]
for id, num, title in chapters:
ch = Chapter({
'identifier': id,
'number': num,
'title': title,
'report_identifier': cmip.identifier
})
print gcis.create_chapter(cmip.identifier, ch)
main()
\ No newline at end of file
...@@ -85,6 +85,7 @@ function formsrv_2_figure_metadata_json($image_nid_query = ''){ ...@@ -85,6 +85,7 @@ function formsrv_2_figure_metadata_json($image_nid_query = ''){
n.nid, n.nid,
ci.field_metadata_t1_value as t1, ci.field_metadata_t1_value as t1,
ci.field_metadata_t2_value as t2, ci.field_metadata_t2_value as t2,
ci.field_cr_permission_status_value as copyright,
f.filepath f.filepath
FROM {node} n FROM {node} n
JOIN {content_type_chapter_image} ci ON n.nid = ci.nid AND n.vid = ci.vid JOIN {content_type_chapter_image} ci ON n.nid = ci.nid AND n.vid = ci.vid
...@@ -98,6 +99,7 @@ END; ...@@ -98,6 +99,7 @@ END;
while($row = $result->fetch_assoc()){ while($row = $result->fetch_assoc()){
$list[] = array( $list[] = array(
'nid' => $row['nid'], 'nid' => $row['nid'],
'copyright' => $row['copyright'],
't1' => json_decode($row['t1']), 't1' => json_decode($row['t1']),
't2' => json_decode($row['t2']), 't2' => json_decode($row['t2']),
'filepath' => $row['filepath'] 'filepath' => $row['filepath']
......
This diff is collapsed.
...@@ -6,7 +6,7 @@ import requests ...@@ -6,7 +6,7 @@ import requests
import yaml import yaml
import getpass import getpass
from domain import Figure, Image, Dataset, Activity, Person, Organization from domain import Figure, Image, Dataset, Activity, Person, Organization, Article, Webpage, Report
def check_image(fn): def check_image(fn):
...@@ -14,7 +14,7 @@ def check_image(fn): ...@@ -14,7 +14,7 @@ def check_image(fn):
# if len(args) < 1 or not isinstance(args[0], Image): # if len(args) < 1 or not isinstance(args[0], Image):
# raise Exception('Invalid Image') # raise Exception('Invalid Image')
if args[1].identifier in (None, ''): if args[1].identifier in (None, ''):
raise Exception('Invalid identifier', args[0].identifier) raise Exception('Invalid identifier', args[1].identifier)
return fn(*args, **kwargs) return fn(*args, **kwargs)
return wrapped return wrapped
...@@ -93,13 +93,16 @@ class GcisClient(object): ...@@ -93,13 +93,16 @@ class GcisClient(object):
username, api_key = args[1:3] username, api_key = args[1:3]
#User provides none or inconsistent args #User provides none or inconsistent args
else: else:
print 'Using http://data.globalchange.gov'
self.base_url = 'http://data.globalchange.gov' self.base_url = 'http://data.globalchange.gov'
#If credentials were not provided, obtain them #If credentials were not provided, obtain them
if username is None or api_key is None: if username is None or api_key is None:
username, api_key = get_credentials(self.base_url) username, api_key = get_credentials(self.base_url)
#Squash trailing slash in base_url, if given
if self.base_url.endswith('/'):
self.base_url = self.base_url[:-1]
self.s = requests.Session() self.s = requests.Session()
self.s.auth = (username, api_key) self.s.auth = (username, api_key)
self.s.headers.update({'Accept': 'application/json'}) self.s.headers.update({'Accept': 'application/json'})
...@@ -118,14 +121,25 @@ class GcisClient(object): ...@@ -118,14 +121,25 @@ class GcisClient(object):
) )
resp = self.s.post(url, data=figure.as_json(), verify=False) resp = self.s.post(url, data=figure.as_json(), verify=False)
if resp.status_code != 200:
return resp
if figure.local_path is not None:
self.upload_figure_file(report_id, chapter_id, figure.identifier, figure.local_path)
if skip_images is False: if skip_images is False:
for image in figure.images: for image in figure.images:
self.create_image(image), self.create_image(image),
self.associate_image_with_figure(image.identifier, report_id, figure.identifier) self.associate_image_with_figure(image.identifier, report_id, figure.identifier)
for c in figure.contributors:
self.associate_contributor_with_figure(c, report_id, chapter_id, figure.identifier)
for p in figure.parents: for p in figure.parents:
self.associate_figure_with_parent(report_id, figure.identifier, p) if p.activity:
self.create_or_update_activity(p.activity)
activity_id = p.activity.identifier if p.activity else None
self.associate_figure_with_parent(report_id, figure.identifier, p, activity_id=activity_id)
return resp return resp
...@@ -152,7 +166,10 @@ class GcisClient(object): ...@@ -152,7 +166,10 @@ class GcisClient(object):
self.associate_contributor_with_figure(c, report_id, chapter_id, figure.identifier) self.associate_contributor_with_figure(c, report_id, chapter_id, figure.identifier)
for p in figure.parents: for p in figure.parents:
self.associate_figure_with_parent(report_id, figure.identifier, p) if p.activity:
self.create_or_update_activity(p.activity)
activity_id = p.activity.identifier if p.activity else None
self.associate_figure_with_parent(report_id, figure.identifier, p, activity_id=activity_id)
return resp return resp
...@@ -161,36 +178,56 @@ class GcisClient(object): ...@@ -161,36 +178,56 @@ class GcisClient(object):
url = '{b}/report/{rpt}/figure/{fig}'.format(b=self.base_url, rpt=report_id, fig=figure_id) url = '{b}/report/{rpt}/figure/{fig}'.format(b=self.base_url, rpt=report_id, fig=figure_id)
return self.s.delete(url, verify=False) return self.s.delete(url, verify=False)
@http_resp
def upload_figure_file(self, report_id, chapter_id, figure_id, local_path):
url = '{b}/report/{rpt}/chapter/{chp}/figure/files/{id}/{fn}'.format(b=self.base_url, rpt=report_id, chp=chapter_id, id=figure_id, fn=basename(local_path))
# For future multi-part encoding support
# return self.s.put(url, headers=headers, files={'file': (filename, open(filepath, 'rb'))})
if not os.path.exists(local_path):
raise Exception('File not found: ' + local_path)
return self.s.put(url, data=open(local_path, 'rb'), verify=False)
@check_image @check_image
def create_image(self, image, report_id=None, figure_id=None): def create_image(self, image, report_id=None, figure_id=None):
url = '{b}/image/'.format(b=self.base_url) url = '{b}/image/'.format(b=self.base_url)
resp = self.s.post(url, data=image.as_json(), verify=False) resp = self.s.post(url, data=image.as_json(), verify=False)
if resp.status_code != 200:
return resp
if image.local_path is not None: if image.local_path is not None:
self.upload_image_file(image.identifier, image.local_path) self.upload_image_file(image.identifier, image.local_path)
if figure_id and report_id: if figure_id and report_id:
self.associate_image_with_figure(image.identifier, report_id, figure_id) self.associate_image_with_figure(image.identifier, report_id, figure_id)
for dataset in image.datasets: # for dataset in image.datasets:
if not self.dataset_exists(dataset.identifier): # if not self.dataset_exists(dataset.identifier):
self.create_dataset(dataset) # self.create_dataset(dataset)
# if not self.activity_exists(dataset.activity.identifier): # # if not self.activity_exists(dataset.activity.identifier):
# self.create_activity(dataset.activity)) # # self.create_activity(dataset.activity))
self.create_or_update_activity(dataset.activity) # self.create_or_update_activity(dataset.activity)
self.associate_dataset_with_image(dataset.identifier, image.identifier, # self.associate_image_with_parent(dataset.identifier, image.identifier,
activity_id=dataset.activity.identifier) # activity_id=dataset.activity.identifier)
for p in image.parents:
if p.activity:
self.create_or_update_activity(p.activity)
activity_id = p.activity.identifier if p.activity else None
self.associate_image_with_parent(image.identifier, p, activity_id=activity_id)
return resp return resp
@check_image @check_image
def update_image(self, image, old_id=None): def update_image(self, image, old_id=None):
url = '{b}/image/{img}'.format(b=self.base_url, img=old_id or image.identifier) url = '{b}/image/{img}'.format(b=self.base_url, img=old_id or image.identifier)
for dataset in image.datasets:
# self.update_activity(dataset.activity)
self.create_or_update_activity(dataset.activity)
self.associate_dataset_with_image(dataset.identifier, image.identifier,
activity_id=dataset.activity.identifier)
for c in image.contributors: for c in image.contributors:
self.associate_contributor_with_image(c, image.identifier) self.associate_contributor_with_image(c, image.identifier)
for p in image.parents:
if p.activity:
self.create_or_update_activity(p.activity)
activity_id = p.activity.identifier if p.activity else None
self.associate_image_with_parent(image.identifier, p, activity_id=activity_id)
return self.s.post(url, data=image.as_json(), verify=False) return self.s.post(url, data=image.as_json(), verify=False)
@check_image @check_image
...@@ -283,12 +320,14 @@ class GcisClient(object): ...@@ -283,12 +320,14 @@ class GcisClient(object):
resp = self.s.get(url, verify=False) resp = self.s.get(url, verify=False)
return resp.status_code, resp.text return resp.status_code, resp.text
@http_resp
def get_report(self, report_id): def get_report(self, report_id):
url = '{b}/report/{id}'.format(b=self.base_url, id=report_id) url = '{b}/report/{id}'.format(b=self.base_url, id=report_id)
resp = self.s.get(url, verify=False) resp = self.s.get(url, verify=False)
return resp.json() try:
return Report(resp.json())
except ValueError:
raise Exception(resp.text)