コード例 #1
0
ファイル: assets.py プロジェクト: RyanMaciel/ooi-ui-services
def delete_asset(id):
    '''
    Delete an asset by providing the id
    '''
    try:
        uframe_obj = UFrameAssetsCollection()
        uframe_assets_url = _uframe_url(uframe_obj.__endpoint__, id)
        response = requests.delete(uframe_assets_url, 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:
                       thisAsset = row
                asset_cache.remove(thisAsset)

            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)
コード例 #2
0
ファイル: assets.py プロジェクト: RyanMaciel/ooi-ui-services
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)
コード例 #3
0
ファイル: events.py プロジェクト: RyanMaciel/ooi-ui-services
def update_event(id):
    '''
    Update an existing event, 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 = UFrameEventsCollection()
        put_body = uframe_obj.from_json(data)
        uframe_events_url = _uframe_url(uframe_obj.__endpoint__, id)
        response = requests.put(uframe_events_url, data=json.dumps(put_body), headers=_uframe_headers())
        cache.delete('event_list')
        return response.text

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        print error
        return make_response(error, 500)
コード例 #4
0
ファイル: assets.py プロジェクト: RyanMaciel/ooi-ui-services
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)
コード例 #5
0
    def test_private_methods(self):
    #_normalize_whitespace
        from ooiservices.app.uframe.assetController import _normalize_whitespace
        test_string = "TEST   THIS"
        test_length = len(test_string)
        single_space_string = _normalize_whitespace(test_string)
        norm_length = len(single_space_string)
        #Test that the new string length is less than the original.
        self.assertTrue(norm_length < test_length)
        #Make sure this didn't remove ALL the whitespace.
        self.assertTrue(len(single_space_string.split(' ')) == 2)

    #_remove_duplicates
        from ooiservices.app.uframe.assetController import _remove_duplicates
        duplicate_data_set = ["TEST", "TEST"]
        non_dup_data_set = _remove_duplicates(duplicate_data_set)
        self.assertTrue(len(non_dup_data_set) == 1)

    #_uframe_url
        from ooiservices.app.uframe.assetController import _uframe_url
        endpoint = "assets"
        endpoint_id = "1"
        uframe_base_url = self.app.config['UFRAME_ASSETS_URL']
        #Build the control url: List
        control = '/'.join([uframe_base_url, endpoint])
        list_endpoint_url = _uframe_url(endpoint)
        #Verify the url matches the config
        self.assertTrue(control == list_endpoint_url)
        #Build the control url: object
        control = '/'.join([uframe_base_url, endpoint, endpoint_id])
        obj_endpoint_url = _uframe_url(endpoint, endpoint_id)
        self.assertTrue(control == obj_endpoint_url)

    #_uframe_collection
        from ooiservices.app.uframe.assetController import _uframe_collection
        #using uframe url created previously
        #This will be more meaningful when uframe is up and running.
        collection = _uframe_collection(obj_endpoint_url)
        self.assertTrue(type(collection) is dict)
        #Test error message if uframe is not running.
        spoof_conn = _uframe_collection('NOTAREALURL')
        self.assertTrue(spoof_conn['status_code'] == 500)

    #_api_headers
        from ooiservices.app.uframe.assetController import _uframe_headers
        uframe_headers = _uframe_headers()
        self.assertTrue(uframe_headers['Accept'] == 'application/json')

    #_normalize
        #Use the asset.json file as a sample set for this test.
        from ooiservices.app.uframe.assetController import _normalize
        normalized_lat = _normalize(self.asset_json['metaData'][0]['value'])
        #expected return: 40 05 45.792 N
        self.assertTrue(normalized_lat == "40 05 45.792 N")

    #_convert_lat_lon
        from ooiservices.app.uframe.assetController import _convert_lat_lon
        #Test a North West input
        normalized_lon = _normalize(self.asset_json['metaData'][1]['value'])
        coords = _convert_lat_lon(normalized_lat, normalized_lon)
        self.assertTrue(coords == (40.0961, -70.8797))
        #Test a South input:
        south_lat = _normalize(self.asset_json['metaData'][11]['value'])
        south_coords = _convert_lat_lon(south_lat, normalized_lon)
        self.assertTrue(south_coords == (-40.0961, -70.8797))
        #Test a East input:
        east_lon = _normalize(self.asset_json['metaData'][12]['value'])
        east_coords = _convert_lat_lon(normalized_lat, east_lon)
        self.assertTrue(east_coords == (40.0961, 70.8797))
        #Test bad input:
        bad_coords = _convert_lat_lon("ABC", "DEF")
        self.assertTrue(bad_coords == (0.0, 0.0))
        #Test the conversion does not happen when the lat/lon is in dec deg.
        dec_deg_lat = self.asset_json['metaData'][9]['value']
        dec_deg_lon = self.asset_json['metaData'][10]['value']
        no_convert = _convert_lat_lon(dec_deg_lat, dec_deg_lon)
        self.assertTrue(no_convert == (dec_deg_lat, dec_deg_lon))

    #_convert_date_time
        from ooiservices.app.uframe.assetController import _convert_date_time
        #Date with no time:
        raw_date = self.asset_json['metaData'][4]['value']
        date = _convert_date_time(raw_date)
        self.assertTrue(date == '13-Apr-14')
        #Date and time:
        raw_time = self.asset_json['metaData'][5]['value']
        date_time = _convert_date_time(raw_date, raw_time)
        self.assertTrue(date_time == '13-Apr-14 17:29')

    #_convert_water_depth
        from ooiservices.app.uframe.assetController import _convert_water_depth
        #Water depth with a space between the value and units.
        raw_depth = self.asset_json['metaData'][3]['value']
        converted_water_depth = _convert_water_depth(raw_depth)
        self.assertTrue(converted_water_depth['value'] == 148)
        self.assertTrue(converted_water_depth['unit'] == 'm')
        #Water depth without a space between value and units.
        raw_depth = self.asset_json['metaData'][7]['value']
        converted_water_depth = _convert_water_depth(raw_depth)
        self.assertTrue(converted_water_depth['value'] == 148)
        self.assertTrue(converted_water_depth['unit'] == 'm')
        #Test a bad entry
        raw_depth = self.asset_json['metaData'][8]['value']
        converted_water_depth = _convert_water_depth(raw_depth)
        self.assertTrue('Error' in converted_water_depth['message'])
コード例 #6
0
    def test_private_methods(self):
        #_normalize_whitespace
        from ooiservices.app.uframe.assetController import _normalize_whitespace
        test_string = "TEST   THIS"
        test_length = len(test_string)
        single_space_string = _normalize_whitespace(test_string)
        norm_length = len(single_space_string)
        #Test that the new string length is less than the original.
        self.assertTrue(norm_length < test_length)
        #Make sure this didn't remove ALL the whitespace.
        self.assertTrue(len(single_space_string.split(' ')) == 2)

        #_remove_duplicates
        from ooiservices.app.uframe.assetController import _remove_duplicates
        duplicate_data_set = ["TEST", "TEST"]
        non_dup_data_set = _remove_duplicates(duplicate_data_set)
        self.assertTrue(len(non_dup_data_set) == 1)

        #_uframe_url
        from ooiservices.app.uframe.assetController import _uframe_url
        endpoint = "assets"
        endpoint_id = "1"
        uframe_base_url = self.app.config['UFRAME_ASSETS_URL']
        #Build the control url: List
        control = '/'.join([uframe_base_url, endpoint])
        list_endpoint_url = _uframe_url(endpoint)
        #Verify the url matches the config
        self.assertTrue(control == list_endpoint_url)
        #Build the control url: object
        control = '/'.join([uframe_base_url, endpoint, endpoint_id])
        obj_endpoint_url = _uframe_url(endpoint, endpoint_id)
        self.assertTrue(control == obj_endpoint_url)

        #_uframe_collection
        from ooiservices.app.uframe.assetController import _uframe_collection
        #using uframe url created previously
        #This will be more meaningful when uframe is up and running.
        collection = _uframe_collection(obj_endpoint_url)
        self.assertTrue(type(collection) is dict)
        #Test error message if uframe is not running.
        spoof_conn = _uframe_collection('NOTAREALURL')
        self.assertTrue(spoof_conn['status_code'] == 500)

        #_api_headers
        from ooiservices.app.uframe.assetController import _uframe_headers
        uframe_headers = _uframe_headers()
        self.assertTrue(uframe_headers['Accept'] == 'application/json')

        #_normalize
        #Use the asset.json file as a sample set for this test.
        from ooiservices.app.uframe.assetController import _normalize
        normalized_lat = _normalize(self.asset_json['metaData'][0]['value'])
        #expected return: 40 05 45.792 N
        self.assertTrue(normalized_lat == "40 05 45.792 N")

        #_convert_lat_lon
        from ooiservices.app.uframe.assetController import _convert_lat_lon
        #Test a North West input
        normalized_lon = _normalize(self.asset_json['metaData'][1]['value'])
        coords = _convert_lat_lon(normalized_lat, normalized_lon)
        self.assertTrue(coords == (40.0961, -70.8797))
        #Test a South input:
        south_lat = _normalize(self.asset_json['metaData'][11]['value'])
        south_coords = _convert_lat_lon(south_lat, normalized_lon)
        self.assertTrue(south_coords == (-40.0961, -70.8797))
        #Test a East input:
        east_lon = _normalize(self.asset_json['metaData'][12]['value'])
        east_coords = _convert_lat_lon(normalized_lat, east_lon)
        self.assertTrue(east_coords == (40.0961, 70.8797))
        #Test bad input:
        bad_coords = _convert_lat_lon("ABC", "DEF")
        self.assertTrue(bad_coords == (0.0, 0.0))
        #Test the conversion does not happen when the lat/lon is in dec deg.
        dec_deg_lat = self.asset_json['metaData'][9]['value']
        dec_deg_lon = self.asset_json['metaData'][10]['value']
        no_convert = _convert_lat_lon(dec_deg_lat, dec_deg_lon)
        self.assertTrue(no_convert == (dec_deg_lat, dec_deg_lon))

        #_convert_date_time
        from ooiservices.app.uframe.assetController import _convert_date_time
        #Date with no time:
        raw_date = self.asset_json['metaData'][4]['value']
        date = _convert_date_time(raw_date)
        self.assertTrue(date == '13-Apr-14')
        #Date and time:
        raw_time = self.asset_json['metaData'][5]['value']
        date_time = _convert_date_time(raw_date, raw_time)
        self.assertTrue(date_time == '13-Apr-14 17:29')

        #_convert_water_depth
        from ooiservices.app.uframe.assetController import _convert_water_depth
        #Water depth with a space between the value and units.
        raw_depth = self.asset_json['metaData'][3]['value']
        converted_water_depth = _convert_water_depth(raw_depth)
        self.assertTrue(converted_water_depth['value'] == 148)
        self.assertTrue(converted_water_depth['unit'] == 'm')
        #Water depth without a space between value and units.
        raw_depth = self.asset_json['metaData'][7]['value']
        converted_water_depth = _convert_water_depth(raw_depth)
        self.assertTrue(converted_water_depth['value'] == 148)
        self.assertTrue(converted_water_depth['unit'] == 'm')
        #Test a bad entry
        raw_depth = self.asset_json['metaData'][8]['value']
        converted_water_depth = _convert_water_depth(raw_depth)
        self.assertTrue('Error' in converted_water_depth['message'])