def get_events(): ''' -- M@C 05/12/2015 Added to support event query on stream data. ''' try: ''' Listing GET request of all events. This method is cached for 1 hour. ''' data = {} cached = cache.get('event_list') if cached: data = cached else: url = current_app.config['UFRAME_ASSETS_URL']\ + '/%s' % 'events' payload = requests.get(url) data = payload.json() if payload.status_code != 200: return jsonify({"events": payload.json()}), payload.status_code data = _compile_events(data) if "error" not in data: cache.set('event_list', data, timeout=CACHE_TIMEOUT) if request.args.get('ref_des') and request.args.get('ref_des') != "": ref_des = request.args.get('ref_des') resp = get_events_by_ref_des(data, ref_des) return resp if request.args.get('search') and request.args.get('search') != "": return_list = [] ven_set = [] ven_subset = [] search_term = str(request.args.get('search')).split() search_set = set(search_term) for subset in search_set: if len(return_list) > 0: if len(ven_set) > 0: ven_set = deepcopy(ven_subset) else: ven_set = deepcopy(return_list) ven_subset = [] for item in return_list: if subset.lower() in str(item['eventClass']).lower(): ven_subset.append(item) elif subset.lower() in str(item['id']).lower(): ven_subset.append(item) elif subset.lower() in str(item['startDate']).lower(): ven_subset.append(item) data = ven_subset else: for item in data: if subset.lower() in str(item['eventClass']).lower(): return_list.append(item) elif subset.lower() in str(item['id']).lower(): return_list.append(item) elif subset.lower() in str(item['startDate']).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)
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)