def update_asset(id): ''' Update an existing asset, the return will be right from uframe if all goes well. Either a success or an error message. Login required. ''' try: data = json.loads(request.data) uframe_obj = UFrameAssetsCollection() put_body = uframe_obj.from_json(data) uframe_assets_url = _uframe_url(uframe_obj.__endpoint__, id) response = requests.put(uframe_assets_url, data=json.dumps(put_body), headers=_uframe_headers()) if response.status_code == 200: asset_cache = cache.get('asset_list') cache.delete('asset_list') if asset_cache: for row in asset_cache: if row['id'] == id: row.update(data) if "error" not in asset_cache: cache.set('asset_list', asset_cache, timeout=CACHE_TIMEOUT) return response.text, response.status_code except requests.exceptions.ConnectionError as e: error = "Error: Cannot connect to uframe. %s" % e print error return make_response(error, 500)
def create_asset(): ''' Create a new asset, the return will be right from uframe if all goes well. Either a success or an error message. Login required. ''' try: data = json.loads(request.data) uframe_obj = UFrameAssetsCollection() post_body = uframe_obj.from_json(data) post_body.pop('assetId') #post_body.pop('metaData') post_body.pop('lastModifiedTimestamp') post_body.pop('manufacturerInfo') post_body.pop('attachments') post_body.pop('classCode') post_body.pop('seriesClassification') post_body.pop('purchaseAndDeliveryInfo') #return json.dumps(post_body) uframe_assets_url = _uframe_url(uframe_obj.__endpoint__) #return uframe_assets_url response = requests.post(uframe_assets_url, data=json.dumps(post_body), headers=_uframe_headers()) if response.status_code == 201: json_response = json.loads(response.text) data['id'] = json_response['id'] asset_cache = cache.get('asset_list') cache.delete('asset_list') if asset_cache: asset_cache.append(data) cache.set('asset_list', asset_cache, timeout=CACHE_TIMEOUT) return response.text, response.status_code except requests.exceptions.ConnectionError as e: error = "Error: Cannot connect to uframe. %s" % e print error return make_response(error, 500)