def create_figure_file(conn, figure_json, figure_name): """Create Figure FileAnnotation from json data.""" if len(figure_json['panels']) == 0: raise Exception('No Panels') first_img_id = figure_json['panels'][0]['imageId'] # we store json in description field... description = {} description['name'] = figure_name description['imageId'] = first_img_id # Try to set Group context to the same as first image conn.SERVICE_OPTS.setOmeroGroup('-1') i = conn.getObject("Image", first_img_id) gid = i.getDetails().getGroup().getId() conn.SERVICE_OPTS.setOmeroGroup(gid) json_bytes = json.dumps(figure_json).encode('utf-8') file_size = len(json_bytes) f = BytesIO() try: f.write(json_bytes) update = conn.getUpdateService() orig_file = conn.createOriginalFileFromFileObj( f, '', figure_name, file_size, mimetype="application/json") finally: f.close() fa = FileAnnotationI() fa.setFile(OriginalFileI(orig_file.getId(), False)) fa.setNs(rstring(JSON_FILEANN_NS)) desc = json.dumps(description) fa.setDescription(rstring(desc)) fa = update.saveAndReturnObject(fa, conn.SERVICE_OPTS) return fa.getId().getValue()
def save_web_figure(conn, json_data): """ Saves 'figureJSON' in POST as an original file. If 'fileId' is specified in POST, then we update that file. Otherwise create a new one with name 'figureName' from POST. """ image_ids = [] first_img_id = None try: for panel in json_data['panels']: image_ids.append(panel['imageId']) if len(image_ids) > 0: first_img_id = int(image_ids[0]) # remove duplicates image_ids = list(set(image_ids)) # pretty-print json figure_json = json.dumps(json_data, sort_keys=True, indent=2, separators=(',', ': ')) except Exception: pass # See https://github.com/will-moore/figure/issues/16 figure_json = figure_json.encode('utf8') if 'figureName' in json_data and len(json_data['figureName']) > 0: figure_name = json_data['figureName'] else: print("No figure name found") return # we store json in description field... description = {} if first_img_id is not None: # We duplicate the figure name here for quicker access when # listing files # (use this instead of file name because it supports unicode) description['name'] = figure_name description['imageId'] = first_img_id if 'baseUrl' in panel: description['baseUrl'] = panel['baseUrl'] desc = json.dumps(description) # Create new file # Try to set Group context to the same as first image curr_gid = conn.SERVICE_OPTS.getOmeroGroup() i = None if first_img_id: i = conn.getObject("Image", first_img_id) if i is not None: gid = i.getDetails().getGroup().getId() conn.SERVICE_OPTS.setOmeroGroup(gid) else: # Don't leave as -1 conn.SERVICE_OPTS.setOmeroGroup(curr_gid) file_size = len(figure_json) f = BytesIO() f.write(figure_json) orig_file = conn.createOriginalFileFromFileObj(f, '', figure_name, file_size, mimetype="application/json") fa = FileAnnotationI() fa.setFile(OriginalFileI(orig_file.getId(), False)) fa.setNs(wrap(JSON_FILEANN_NS)) fa.setDescription(wrap(desc)) update = conn.getUpdateService() fa = update.saveAndReturnObject(fa, conn.SERVICE_OPTS) ann_id = fa.getId().getValue() return ann_id