def filterEvents(event_name, vacancy, distance, location_coords, category): ## TODO: filter based on other params # print("finding event name" + str(event_name)) db = firestore.Client() events_ref = db.collection(u'events') if location_coords is not None: location = location_coords.split(',') location_coords = (float(location[0]), float(location[1])) returnData = [] if event_name is not None: event_data = events_ref.where(u'name', u'==', event_name) event_data = event_data.stream() for each in event_data: returnData = helpers.parseEventData(each, location_coords) return returnData else: events_data = events_ref.stream() all_events = [] for each in events_data: data = helpers.parseEventData(each, location_coords) # print(data) all_events.append(data) returnData = all_events # print("Testing vars ", category, " ", vacancy," " , distance) if category is not None and len(category)>0: returnData = list(filter(lambda x : str(x['category']) == str(category), returnData)) # if vacancy is not None: # returnData = list(filter(lambda x : int(x['vacancy']) >= int(vacancy) if 'vacancy' in x, returnData)) results = [] if vacancy is not None and len(vacancy)>0: for x in returnData: if 'vacancy' in x.keys(): results.append(x) returnData = results if distance is not None and len(distance)>0: returnData = list(filter(lambda x : float(x['distance']) <= float(distance), returnData)) # print(returnData) ## filter events from all_events based on the other params #[x for x in all_events if x['max'] - x['count_of_participants'] > vacancy] return returnData
def getUserEvents(user_ref): ## user_ref -> user-reference db = firestore.Client() user_data = user_ref.get().to_dict() query = db.collection(u'events').where(u'is_active', u'==', True).where(u'confirmed_participants', u'array_contains', user_ref) query = query.stream() returnData = [] for each in query: ## each is an event data = helpers.parseEventData(each, None, "main", user_data) returnData.append(data) return returnData
def getUserInterests(user_ref): db = firestore.Client() query = db.collection(u'user_requests').where(u'user', u'==', user_ref) doc_ref = db.collection(u'events') query = query.stream() returnData = [] for each in query: data = each.to_dict() data['id'] = each.id del data['user'] data['location'] = helpers.parseGeoPoint(data['location']) if not data['event_id'] == "": data['event'] = helpers.parseEventData(doc_ref.document(data['event_id']).get()) returnData.append(data) return returnData
def getNotifications(user_ref): db = firestore.Client() query = db.collection(u'notifications').where(u'is_active', u'==', True).where(u'user', u'==', user_ref) data = query.stream() returnData = [] for each in data: each = each.to_dict() if each['event'].get().exists: each['event'] = helpers.parseEventData(each['event'].get()) else: del each['event'] del each['user'] del each['is_active'] returnData.append(each) returnData = sorted(returnData, key=lambda kv: kv['timestamp'], reverse=True) return returnData
def getNearbyEvents(user, location_coords): ##TODO: find a better way utilizing where query in firestore rather than get all and filter db = firestore.Client() query = db.collection(u'events') query = query.stream() location = location_coords.split(',') location_coords = (float(location[0]), float(location[1])) user_data = user.get().to_dict() returnData = [] for each in query: data = each.to_dict() if 'location_coords' in data and data['is_active']: distance = helpers.calculateDistanceBetweenLocationCoordinates(location_coords, helpers.parseGeoPoint(data['location_coords'], 'tuple')) #print (each.id, distance) if distance < 35: data = helpers.parseEventData(each, location_coords, None, user_data) returnData.append(data) return returnData