def get_parrots(account_id, db, secure = True): querystring = request.query; from_ = querystring.get("from") to_ = querystring.get("to") skip = int(querystring.get('skip', 0)) limit = int(querystring.get('limit', 0)) query_subscriptions = {'account_id': ObjectId(account_id), 'active': True} if from_ or to_: query_subscriptions['created_at'] = {} if from_: query_subscriptions['created_at']['$gte'] = datetime.strptime(from_, '%Y-%m-%d') if to_: query_subscriptions['created_at']['$lte'] = datetime.strptime(to_, '%Y-%m-%d') if querystring.screen_name: screen_name_regex = re.compile(querystring.screen_name,re.IGNORECASE) query_subscriptions['twitter_screen_name'] = screen_name_regex parrots_from_subscriptions = Subscriptions.find(db, query_subscriptions, {'parrot_id': True, '_id': False}).skip(skip).limit(limit).sort([('_id', -1)]) parrots_id = map(lambda x: x.get('parrot_id'), parrots_from_subscriptions) parrots = list(Parrots.find(db, {'_id': {'$in': parrots_id}})) for parrot in parrots: parrot['payments'] = filter(lambda x: x.get('account_id') == account_id, parrot.get('payments')) # TODO: fix this response.headers['Content-type'] = 'application/json' return json.dumps(map(lambda x: Parrots.toJSON(x), parrots))