Esempio n. 1
0
def get_event(id):
    '''
    Object response for the GET(id) request.  This response is NOT cached.
    '''
    try:
        #set up all the contaners.
        data = {}
        asset_id = ""
        #create uframe instance, and fetch the data.
        uframe_obj = UFrameEventsCollection()
        payload = uframe_obj.to_json(id)
        data = payload.json()
        if payload.status_code != 200:
            return  jsonify({ "events" : payload.json()}), payload.status_code

        try:
            data['class'] = data.pop('@class')
            data['startDate'] = num2date(float(data['startDate'])/1000, units='seconds since 1970-01-01 00:00:00', calendar='gregorian')
            data['endDate'] = num2date(float(data['endDate'])/1000, units='seconds since 1970-01-01 00:00:00', calendar='gregorian')
        except (KeyError, TypeError):
            pass
        return jsonify(**data)

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        print error
        return make_response(error, 500)
Esempio n. 2
0
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)
def get_events_by_ref_des(ref_des):
    #Create the container for the processed response
    result = []
    #Get all the events to begin searching though...
    from ooiservices.app.uframe.UFrameEventsCollection import UFrameEventsCollection
    uframe_obj = UFrameEventsCollection()
    payload = uframe_obj.to_json()
    if payload.status_code != 200:
        return  jsonify({ "events" : payload.json()}), payload.status_code

    data = payload.json()
    for row in data:
        #variables used in loop
        temp_dict = {}
        platform = ""
        mooring = ""
        instrument = ""
        ref_des_check = ""
        try:
            if row['asset']['metaData']:
                for metaData in row['asset']['metaData']:
                    if metaData['key'] == 'Ref Des':
                        ref_des_check = metaData['value']
            if ref_des_check == ref_des:
                temp_dict['id'] = row['eventId']
                temp_dict['ref_des'] = ref_des_check
                temp_dict['start_date'] = num2date(float(row['startDate'])/1000, units='seconds since 1970-01-01 00:00:00', calendar='gregorian')
                temp_dict['class'] = row['@class']
                temp_dict['event_description'] = row['eventDescription']
                temp_dict['event_type'] = row['eventType']
                temp_dict['notes'] = row['notes']
                temp_dict['url'] =  url_for('uframe.get_event', id=row['eventId'])
                temp_dict['uframe_url'] = current_app.config['UFRAME_ASSETS_URL'] + '/%s/%s' % (uframe_obj.__endpoint__, row['eventId'])
                result.append(temp_dict)
                temp_dict = {}
        except (KeyError, TypeError):
            raise
    result = jsonify({ 'events' : result })
    return result
Esempio n. 4
0
def get_events():
    '''
    -- M@C 05/12/2015
    Added to support event query on stream data.
    '''
    try:
        if 'ref_des' in request.args:
            events = get_events_by_ref_des(request.args.get('ref_des'))
            return events
        else:
            '''
            Listing GET request of all events.  This method is cached for 1 hour.
            '''
            #set up all the contaners.
            data = {}

            #Manually set up the cache
            cached = cache.get('event_list')
            if cached:
                data = cached

            #create uframe instance, and fetch the data.
            uframe_obj = UFrameEventsCollection()
            payload = uframe_obj.to_json()
            data = payload.json()
            if payload.status_code != 200:
                return  jsonify({ "events" : payload.json()}), payload.status_code

            try:
                for row in data:
                    row['id'] = row.pop('eventId')
                    row['class'] = row.pop('@class')
                #parse the result and assign ref_des as top element.
            except (KeyError, TypeError, AttributeError):
                pass

            if "error" not in data:
                cache.set('event_list', data, timeout=CACHE_TIMEOUT)

            #data = sorted(data, key=itemgetter('id'))

            if request.args.get('search') and request.args.get('search') != "":
                return_list = []
                search_term = request.args.get('search')
                for item in data:
                    if search_term.lower() in str(item['class']).lower():
                        return_list.append(item)
                    if search_term.lower() in str(item['id']):
                        return_list.append(item)
                    #if search_term.lower() in str(item['referenceDesignator']).lower():
                    #    return_list.append(item)
                    if search_term.lower() in str(item['startDate']).lower():
                        return_list.append(item)
                    #if search_term.lower() in str(item['assetInfo']['owner']).lower():
                        #return_list.append(item)
                data = return_list

            result = jsonify({ 'events' : data })

            return result

    except requests.exceptions.ConnectionError as e:
        error = "Error: Cannot connect to uframe.  %s" % e
        print error
        return make_response(error, 500)