def get_strategies(size, page, sort, order, begin, end, name, strategy_id): query = {} if not name else { '$or': [{ 'name': { '$regex': '.*{}.*'.format(name), '$options': 'i' } }, { 'team_name': { '$regex': '.*{}.*'.format(name), '$options': 'i' } }, { 'description': { '$regex': '.*{}.*'.format(name), '$options': 'i' } }, { 'key': { '$regex': '.*{}.*'.format(name), '$options': 'i' } }] } query = query if not strategy_id else {'_id': ObjectId(strategy_id)} if begin and end: query['date_added'] = {'$gte': begin, '$lte': end} elif begin: query['date_added'] = {'$gte': begin} elif end: query['date_added'] = {'$lte': end} total_elements = ApiAccess.get_revenyou_db().signal_provider.find( query).count() cursor = ApiAccess.get_revenyou_db().signal_provider.\ find(query).\ skip(page * size).\ limit(size).\ collation({'locale': 'en' }) if order != '': cursor = cursor.sort(sort, -1 if order == 'desc' else 1) result = { 'total_elements': total_elements, 'total_pages': math.ceil(float(total_elements) / size), 'current_page': page, 'content': [] } for document in cursor: result['content'].append(document) result['content'][-1]['_id'] = str(result['content'][-1]['_id']) result['content'][-1]['date_added'] = utils.utc_ts_to_local_str( document.get('date_added', None)) result['content'][-1]['date_updated'] = utils.utc_ts_to_local_str( document.get('date_updated', None)) return result
def get_customer_subscribed_snapshot(user_id): sub_cursor = ApiAccess.get_revenyou_db().subscribed.find( {'user_id': user_id}, { '_id': 0, 'quantity': 1, 'strategy_id': 1, 'status': 1, 'date_added': 1 }) result = [] for document in sub_cursor: strategy_name = ApiAccess.get_revenyou_db().signal_provider.find_one({ '_id': ObjectId(document.get('strategy_id', '')) }).get('name', '') date_time = utils.utc_ts_to_local_str(document.get('date_added', None)).split() result.append({ 'date': date_time[0], 'time': (date_time[1] if len(date_time) > 1 else ''), 'operation': TimelineSteps.bot_subscription.value['operation'], 'description': '{}: {}/{}'.format( TimelineSteps.bot_subscription.value['description'], strategy_name, document.get('quantity', '')), 'status': document.get('status', SubscriptionStatus.unknown.value) }) return json.dumps(result)
def get_articles(size, page, sort, order, name, article_id): query = {} if not name else {'en.title': {'$regex': '.*{}.*'.format(name), '$options': 'i'}} query = query if not article_id else {'_id': ObjectId(article_id)} total_elements = ApiAccess.get_revenyou_db().bots_store_articles.find(query).count() cursor = ApiAccess.get_revenyou_db().bots_store_articles.\ find(query).\ skip(page * size).\ limit(size).\ collation({'locale': 'en' }) if order != '': cursor = cursor.sort(sort, -1 if order == 'desc' else 1) result = { 'total_elements': total_elements, 'total_pages': math.ceil(float(total_elements)/size), 'current_page': page, 'content': []} for document in cursor: result['content'].append(document) result['content'][-1]['_id'] = str(result['content'][-1]['_id']) return result
def get_feedbacks(size, page, sort, order, name): query = {} if not name else {'$or': [{'message': {'$regex': '.*{}.*'.format(name), '$options': 'i'}}, {'version': {'$regex': '.*{}.*'.format(name), '$options': 'i'}}]} total_elements = ApiAccess.get_revenyou_db().feedback.find(query).count() cursor = ApiAccess.get_revenyou_db().feedback.\ find(query).\ skip(page * size).\ limit(size).\ collation({'locale': 'en' }) if order != '': cursor = cursor.sort(sort, -1 if order == 'desc' else 1) result = { 'total_elements': total_elements, 'total_pages': math.ceil(float(total_elements)/size), 'current_page': page, 'content': []} for document in cursor: result['content'].append(document) result['content'][-1]['_id'] = str(result['content'][-1]['_id']) return result
def get_user_subscribed_strategies(user_id, size, page, sort, order, begin, end): query = {'user_id': user_id} if begin and end: query['date_added'] = {'$gte': begin, '$lte': end} elif begin: query['date_added'] = {'$gte': begin} elif end: query['date_added'] = {'$lte': end} total_elements = ApiAccess.get_revenyou_db().subscribed.find(query).count() cursor = ApiAccess.get_revenyou_db().subscribed.\ find(query).\ skip(page * size).\ limit(size).\ collation({'locale': 'en' }) if order != '': cursor = cursor.sort(sort, -1 if order == 'desc' else 1) cursor = list(cursor) result = { 'total_elements': total_elements, 'total_pages': math.ceil(float(total_elements) / size), 'current_page': page, 'content': [] } signal_providers = ApiAccess.get_revenyou_db().signal_provider.\ find({'_id': \ {'$in' : [ObjectId(doc.get('strategy_id', '')) for doc in cursor]}}, {'name': 1, 'pair': 1, 'icon_url': 1}).\ limit(size) strategy_info = {} for item in signal_providers: strategy_info[str(item['_id'])] = { 'strategy_name': item.get('name', ''), 'strategy_pair': item.get('pair', ''), 'strategy_icon': item.get('icon_url', '') } for document in cursor: result['content'].append(document) result['content'][-1]['_id'] = str(result['content'][-1]['_id']) result['content'][-1]['date_added'] = utils.utc_ts_to_local_str( document.get('date_added', None)) result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str( document.get('date_modified', None)) strategy_id = document.get('strategy_id', None) if strategy_id is not None and strategy_id in strategy_info: result['content'][-1].update(strategy_info[strategy_id]) pnl = (float(result['content'][-1]['updated_quantity']) - float(result['content'][-1]['quantity'])) / float(result['content'][-1]['quantity']) * 100 \ if result['content'][-1]['updated_quantity'].isnumeric() else None result['content'][-1]['pnl'] = "{:.2f}%".format(pnl) if pnl else '' return result
def get_customer_unsubscribed_snapshot(user_id): unsub_cursor = ApiAccess.get_revenyou_db().unsubscribed.find( {'user_id': user_id}, { '_id': 0, 'bot_name': 1, 'date_added': 1 }) result = [] for document in unsub_cursor: date_time = utils.utc_ts_to_local_str(document.get('date_added', None)).split() result.append({ 'date': date_time[0], 'time': (date_time[1] if len(date_time) > 1 else ''), 'operation': TimelineSteps.bot_unsubscription.value['operation'], 'description': '{}: {}'.format( TimelineSteps.bot_unsubscription.value['description'], document.get('bot_name', '')), 'status': SubscriptionStatus.active.value }) return json.dumps(result)
def get_csv_exportable(cols): global _max_limit strategies = [] skip_ = 0 while True: partial = list(ApiAccess.get_revenyou_db().signal_provider.\ find({}, {'_id': 0}).\ skip(skip_).\ limit(_max_limit)) skip_ += _max_limit if len(partial) == 0: break strategies.extend(partial) result = [] for strategy in strategies: result.append({}) for col in cols: if col == 'date_added': result[-1]['date_added'] = utils.utc_ts_to_local_str( strategy.get('date_added', None)) continue if col == 'date_updated': result[-1]['date_updated'] = utils.utc_ts_to_local_str( strategy.get('date_updated', None)) continue result[-1][col] = strategy.get(col, '') return json.dumps(result)
def get_unsub_value(user_id): pipeline = [{ '$match': { 'user_id': user_id } }, { '$group': { '_id': '', 'total': { '$sum': { '$toDouble': '$deposit_balance' } }, 'change': { '$sum': { '$toDouble': '$profit' } }, } }, { '$addFields': { 'value': { '$add': ['$total', '$change'] } } }, { '$project': { '_id': 0, 'value': 1 } }] result = list(ApiAccess.get_revenyou_db().unsubscribed.aggregate(pipeline)) return result[0].get('value', 0) if len(result) > 0 else 0
def get_new_contact_list(): maybe_id = get_last_id() #Last day or from last saved query = {'date_added': {'$gte': int((datetime.now() - timedelta(days=1)).timestamp())}} \ if maybe_id is None else {'_id': {'$gt': maybe_id}} query.update({'email': {'$exists': True, '$ne': ''}}) contacts = [] skip_ = 0 while True: partial = list(ApiAccess.get_revenyou_db().sign_up.\ find(query, {'email': 1, 'first_name': 1, 'last_name': 1, 'nationality': 1}).\ sort('_id', -1).\ skip(skip_).\ limit(_max_limit)) skip_ += _max_limit if len(partial) == 0: break contacts.extend(partial) if len(contacts) == 0: return ApiAccess.get_skynet_db().cached_contacts.drop() ApiAccess.get_skynet_db().cached_contacts.insert_many(contacts) for contact in contacts: contact['country'] = contact.pop('nationality', '').upper() del contact['_id'] send_contacts(contacts)
def get_customer_snapshot(user_id): cursor = ApiAccess.get_revenyou_db().sign_up.find({'user_id': user_id}, { '_id': 0, 'youhex_name': 1, 'status': 1, 'date_added': 1 }) if cursor.count() == 0: return {} date_time = utils.utc_ts_to_local_str(cursor[0].get('date_added', None)).split() result = { 'date': date_time[0], 'time': (date_time[1] if len(date_time) > 1 else ''), 'operation': TimelineSteps.user_creation.value['operation'], 'description': '{}: {}'.format(TimelineSteps.user_creation.value['description'], cursor[0].get('youhex_name', '')), 'status': cursor[0].get('status', UserStatus.unknown.value) } return result
def get_last_date(): last_date = list(ApiAccess.get_revenyou_db().deposit.\ find({}, {'_id': 0, 'date_added': 1}).\ sort('date_added', -1).\ limit(1)) if len(last_date) > 0: return last_date[0]['date_added']
def get_subscribed_customer_count(begin=None, end=None): if begin and end: pipeline = [{'$match': {'date_added': {'$gte': begin, '$lte': end}}}] elif begin: pipeline = [{'$match': {'date_added': {'$gte': begin}}}] elif end: pipeline = [{'$match': {'date_added': {'$lte': end}}}] else: pipeline = [] pipeline.extend([{ '$match': { 'status': SubscriptionStatus.active.value } }, { '$group': { '_id': '$user_id' } }, { '$count': 'count' }]) cursor = ApiAccess.get_revenyou_db().subscribed.aggregate(pipeline) result = [{'_id': 'Subscribed users', 'count': 0}] for pair in cursor: result[0].update(pair) return result
def get_full_kyc_customer_count(begin=None, end=None): query = { '$and': [{ 'swiftdil_customer_id': { '$exists': True } }, { 'swiftdil_customer_id': { '$ne': '' } }, { 'swiftdil_customer_id': { '$ne': '-' } }] } if begin and end: query['date_added'] = {'$gte': begin, '$lte': end} elif begin: query['date_added'] = {'$gte': begin} elif end: query['date_added'] = {'$lte': end} count = ApiAccess.get_revenyou_db().sign_up.find(query).count() return [{'_id': 'Full KYC users', 'count': count}]
def get_swift_id(user_id): cursor = ApiAccess.get_revenyou_db().sign_up.find({'user_id': user_id}, { 'swiftdil_customer_id': 1, '_id': 0 }) return json.dumps(cursor[0] if cursor.count() > 0 else '')
def get_csv_exportable(max, cols, begin, end): query = {} if begin and end: query['date_added'] = {'$gte': begin, '$lte': end} elif begin: query['date_added'] = {'$gte': begin} elif end: query['date_added'] = {'$lte': end} cursor = list(ApiAccess.get_revenyou_db().sign_up.\ find(query, {'_id': 0}).\ limit(max).\ collation({'locale': 'en'})) if 'phone_number' in cols: carriers = ApiAccess.get_revenyou_db().carrier.\ find({'_id': \ {'$in' : [ObjectId(doc.get('carrier_id', '')) for doc in cursor]}}, {'non_formated_phone_number': 1}).\ limit(max) phone_numbers = {} for item in carriers: phone_numbers[str(item['_id'])] = item.get( 'non_formated_phone_number', '') result = [] for document in cursor: result.append({}) for col in cols: if col == 'phone_number': result[-1]['phone_number'] = phone_numbers.get( document.get('carrier_id', ''), '') continue if col == 'full_name': result[-1]['full_name'] = '{} {}'.format( document.get('first_name', ''), document.get('last_name', '')) continue if col == 'date_added': result[-1]['date_added'] = utils.utc_ts_to_local_str( document.get('date_added', None)) continue result[-1][col] = document.get(col, '') return json.dumps(result)
def get_user_unsubscribed_strategies(user_id, size, page, sort, order, begin, end): query = {'user_id': user_id} if begin and end: query['date_added'] = {'$gte': begin, '$lte': end} elif begin: query['date_added'] = {'$gte': begin} elif end: query['date_added'] = {'$lte': end} total_elements = ApiAccess.get_revenyou_db().unsubscribed.find( query).count() cursor = ApiAccess.get_revenyou_db().unsubscribed.\ find(query).\ skip(page * size).\ limit(size).\ collation({'locale': 'en' }) if order != '': cursor = cursor.sort(sort, -1 if order == 'desc' else 1) result = { 'total_elements': total_elements, 'total_pages': math.ceil(float(total_elements) / size), 'current_page': page, 'content': [] } for document in cursor: result['content'].append(document) result['content'][-1]['_id'] = str(result['content'][-1]['_id']) result['content'][-1]['date_added'] = utils.utc_ts_to_local_str( document.get('date_added', None)) result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str( document.get('date_modified', None)) result['content'][-1]['date_subscribed'] = utils.utc_ts_to_local_str( document.get('date_subscribed', None)) balance = float(result['content'][-1]['deposit_balance']) result['content'][-1]['pnl'] = '{:.2f}%'.format( (float(result['content'][-1]['profit']) + balance - balance) / balance * 100) return result
def get_trade_data(begin=None, end=None): begin = begin * 1000 if begin else None end = end * 1000 if end else None price_begin = str(end\ if end else int(datetime.now().timestamp())) price_end = str(int((datetime.fromtimestamp(end / 1000) - timedelta(hours=12)).timestamp())\ if end else int((datetime.now() - timedelta(hours=12)).timestamp())) if begin and end: pipeline = [{'$match': {'createdTsMsec': {'$gte': begin, '$lte': end}}}] elif begin: pipeline = [{'$match': {'createdTsMsec': {'$gte': begin}}}] elif end: pipeline = [{'$match': {'createdTsMsec': {'$lte': end}}}] else: pipeline = [] pipeline.extend([ {'$match': {'trades': {'$exists': True, '$not': {'$size': 0}}}}, {'$addFields': {'traded_quantity': {'$sum': '$trades.quantity'}}}, {'$group': { '_id': '$marketAsset', 'traded': {'$sum': '$traded_quantity'}, 'fee': {'$sum': {'$multiply': ['$traded_quantity', '$dynamicFee']}}, 'date': {'$min': '$createdTs'} }} ]) cache = {} docs = list(ApiAccess.get_revenyou_db().order_v1.aggregate(pipeline)) for doc in docs: doc['date'] = doc['date'].strftime('%m-%d-%Y') doc['traded'] = doc['traded'].to_decimal() fee_decimal = doc['fee'].to_decimal() if doc['_id'] in cache: doc['value'] = fee_decimal * cache[doc['_id']] if type(cache[doc['_id']]) != str else cache[doc['_id']] continue coin_value = get_historical_price(doc['_id'], price_begin, price_end) doc['value'] = doc['traded'] * decimal.Decimal(coin_value) if type(coin_value) == float else coin_value doc['fee_value'] = fee_decimal * decimal.Decimal(coin_value) if type(coin_value) == float else coin_value doc['fee'] = fee_decimal cache[doc['_id']] = decimal.Decimal(coin_value) if type(coin_value) == float else coin_value total_value = decimal.Decimal(.0) total_fee_value = decimal.Decimal(.0) for doc in docs: if doc['value'] == 'No price' or doc['fee_value'] == 'No price': continue total_value += decimal.Decimal(doc['value']) total_fee_value += decimal.Decimal(doc['fee_value']) docs.append({'_id': 'total', 'fee': total_fee_value, 'value': total_value, 'fee_value': total_fee_value, 'traded': total_value}) return docs
def get_active_strategy_names(): names = list(ApiAccess.get_revenyou_db().signal_provider.find( {'stage': 'active'}, { '_id': 1, 'name': 1 })) for name in names: name['_id'] = str(name['_id']) return json.dumps(names)
def get_notes(user_id): notes = list(ApiAccess.get_revenyou_db().notes.\ find({'user_id': user_id}, {'_id': False}).\ sort('date_added', -1).\ limit(10)) for note in notes: note.update((k, utils.utc_ts_to_local_str(v)) for k, v in note.items() if k == 'date_added') return {'notes': notes}
def get_kyc_customer_count(begin=None, end=None): query = {} if begin and end: query['date_added'] = {'$gte': begin, '$lte': end} elif begin: query['date_added'] = {'$gte': begin} elif end: query['date_added'] = {'$lte': end} count = ApiAccess.get_revenyou_db().sign_up.find(query).count() return [{'_id': 'KYC users', 'count': count}]
def get_geographic_customer_count(): global _max_limit pipeline = [ { '$skip': 0 }, { '$limit': _max_limit }, { '$group': { '_id': '$country_code', 'value': { '$sum': 1 } } }, { '$project': { '_id': 0, 'id': "$_id", 'value': 1 } }, ] skip_index = next( (index for (index, stage) in enumerate(pipeline) if '$skip' in stage), None) if skip_index is None: raise KeyError('No $skip provided') data = [] while True: partial_result = list( ApiAccess.get_revenyou_db().carrier.aggregate(pipeline)) if len(partial_result) == 0: break pipeline[skip_index]['$skip'] += _max_limit data.extend(partial_result) data = sorted(data, key=lambda k: k['value'], reverse=True) result = (pd.DataFrame(data)\ .groupby('id', as_index=False)\ .value.sum()\ .sort_values('value')\ .to_dict('records')) return { 'min': result[-1].get('value', 0) if len(result) > 0 else 0, 'max': result[0].get('value', 0) if len(result) > 0 else 0, 'content': result }
def daily_new_deposit_aggregator(): maybe_start = get_last_saved_date() start_date = maybe_start if maybe_start is not None else get_oldest_date() end_date = int( (datetime.fromtimestamp(start_date) + timedelta(hours=24)).timestamp()) pipeline = [{ '$match': { 'order_status': OrderStatus.approved.value, 'bank': TransactionType.external.value, 'date_added': { '$gte': start_date, '$lt': end_date } } }, { '$group': { '_id': '$user_id', 'quantity': { '$sum': { '$toDouble': '$quantity' } }, 'date_added': { '$first': '$date_added' } } }] match_index = next( (index for (index, stage) in enumerate(pipeline) if '$match' in stage), None) if match_index is None: raise KeyError('No $match provided') last_deposit_date = get_last_date() while True: data = list(ApiAccess.get_revenyou_db().deposit.aggregate(pipeline)) if len(data) > 0: unordered_bulk_write(data) start_date = end_date end_date = int((datetime.fromtimestamp(start_date) + timedelta(hours=24)).timestamp()) if start_date > last_deposit_date: break pipeline[match_index]['$match']['date_added'].update({ '$gte': start_date, '$lt': end_date })
def get_customer_internal(user_id, is_corporate): db = ApiAccess.get_revenyou_db().corporate_account if is_corporate \ else ApiAccess.get_revenyou_db().sign_up cursor = list(db.find({'user_id': user_id})) if len(cursor) == 0: return {} user = cursor[0] carrier_id = user.get('carrier_id', None) country = user.get('country', None) carrier = ApiAccess.get_revenyou_db().carrier.find_one( {'_id': ObjectId(carrier_id)}) if carrier_id is not None else {} country = pycountry.countries.get( alpha_3=country.upper()) if country is not None else None user['mobile'] = carrier.get('non_formated_phone_number', '') if carrier else '' user['_id'] = str(user['_id']) user['country'] = country.name if country is not None else 'Unknown' user['date_added'] = utils.utc_ts_to_local_str(user.get( 'date_added', None)) user['date_modified'] = utils.utc_ts_to_local_str( user.get('date_modified', None)) user['full_name'] = '{} {}'.format(user.get('first_name', ''), user.get('last_name', '')) try: if 'document_front_url' in user: user['document_front_url'] = cloudinary.CloudinaryImage( user['document_front_url']).build_url() if 'document_back_url' in user: user['document_back_url'] = cloudinary.CloudinaryImage( user['document_back_url']).build_url() except Exception: pass return user
def get_deposit_withdrawal_lookup(): while True: pipeline = [{ '$match': { 'order_status': OrderStatus.approved.value } }, { '$sort': { '_id': 1 } }, { '$limit': _default_partition_size }, { '$lookup': { 'from': 'withdrawal', 'let': { 'user_id': '$user_id', 'status': '$order_status' }, 'pipeline': [{ '$match': { '$expr': { '$and': [{ '$eq': ['$user_id', '$$user_id'] }, { '$eq': ['$withdrawal_status', '$$status'] }] } } }], 'as': 'withdrawals' } }] maybe_id = get_last_id(ApiAccess.get_skynet_db().deposit_withdrawal) if maybe_id: match_index = next((index for (index, stage) in enumerate(pipeline) if '$match' in stage), None) if match_index is None: raise KeyError('No $match provided') pipeline[match_index]['$match'].update({'_id': {'$gt': maybe_id}}) data = list(ApiAccess.get_revenyou_db().deposit.aggregate(pipeline)) if len(data) == 0: break ApiAccess.get_skynet_db().deposit_withdrawal.insert_many(data) time.sleep(_default_cool_off_timeout)
def get_user_withdraw_amount(user_id): data = list(ApiAccess.get_revenyou_db().withdrawal.aggregate([{ '$match': { 'user_id': user_id, 'withdrawal_status': OrderStatus.approved.value } }, { '$group': { '_id': '', 'withdrawal_amount': { '$sum': { '$toDouble': '$quantity_in_fiat' } } } }])) return data[0] if len(data) > 0 else {'withdrawal_amount': 0}
def get_user_deposit_amount(user_id): data = list(ApiAccess.get_revenyou_db().deposit.aggregate([{ '$match': { 'user_id': user_id, 'order_status': OrderStatus.approved.value, 'bank': TransactionType.external.value } }, { '$group': { '_id': '', 'deposit_amount': { '$sum': { '$toDouble': '$quantity' } } } }])) return data[0] if len(data) > 0 else {'deposit_amount': 0}
def store_unpaid_users(): unpaid_users = list(ApiAccess.get_revenyou_db().withdrawal.aggregate([{ '$match': { 'cleared': False, 'confirmed_by_user': True, 'dump_withdrawal': { '$exists': True, '$ne': [] } } }])) data_clear, data_fail = [], [] for item in unpaid_users: (data_fail, data_clear)[ check_withdrawal_screening(item) and \ check_withdrawal_amount(item)].append(item) upsert_collection(data_clear, 'clear_withdrawals') upsert_collection(data_fail, 'failed_withdrawals')
def group_by_team(strategies): sp_data = list(ApiAccess.get_revenyou_db().signal_provider.find( {'_id': { '$in': [ObjectId(_id) for _id in strategies.keys()] }}, { 'team_name': 1, 'name': 1, 'user_id': 1, 'market_cap_eur': 1 })) result = defaultdict(list) for info in sp_data: user_id = info.get('user_id', 'unknown') strategy_id = str(info.get('_id', '')) if user_id not in result: result[user_id] = {'bots': [], 'names': set()} result[user_id]['bots'].append({ 'strategy_id': strategy_id, 'strategy_name': info.get('name', ''), 'market_cap_eur': info.get('market_cap_eur', '') }) result[user_id]['names'].add(info.get('team_name', 'unknown')) result[user_id]['bots'][-1].update(strategies.get(strategy_id, {})) for team, info in result.items(): info['names'] = list(info['names']) info['total_amount'] = sum( [amount['quantity_in_euro'] for amount in info['bots']]) info['total_count'] = sum( [amount['subscribers'] for amount in info['bots']]) info['total_avg'] = statistics.mean( [amount['quantity_avg_euro'] for amount in info['bots']]) info['total_base'] = sum( [amount['quantity_in_base'] for amount in info['bots']]) return result
def check_withdrawal_screening(withdrawal_info): cursor = list(ApiAccess.get_revenyou_db().sign_up.find( {'user_id': withdrawal_info['user_id']}, { 'swiftdil_customer_id': 1, '_id': 0 })) maybe_swift_id = cursor[0]['swiftdil_customer_id'] \ if len(cursor) > 0 and 'swiftdil_customer_id' in cursor[0] \ else None if not maybe_swift_id: return True screenings = get_customer_screenings(maybe_swift_id).get('content', []) for scr in screenings: if 'outcome' not in scr: continue if any(status not in {'CLEAR', 'DISMISSED'} \ for status in scr['outcome'].values()): withdrawal_info['reason'] = 'Screening failed' return False return True
def get_corporates_internal(size, page, sort, order, begin, end, name): pipeline = [{ '$addFields': { 'full_name': { '$concat': ['$first_name', ' ', '$middle_name', ' ', '$last_name'] } } }] if begin and end: pipeline.append( {'$match': { 'date_added': { '$gte': begin, '$lte': end } }}) elif begin: pipeline.append({'$match': {'date_added': {'$gte': begin}}}) elif end: pipeline.append({'$match': {'date_added': {'$lte': end}}}) if name: initials = name.split() full_name_regex = '.*{}.*'.format(initials[0]) for initial in initials[1:]: full_name_regex += ' .*{}.*'.format(initial) pipeline.append({ '$match': { '$or': [{ 'full_name': { '$regex': full_name_regex, '$options': 'i' } }, { 'email': { '$regex': '.*{}.*'.format(name), '$options': 'i' } }] } }) count_pipline = pipeline.copy() count_pipline.append({'$count': 'count'}) maybe_count = list( ApiAccess.get_revenyou_db().corporate_account.aggregate(count_pipline)) total_elements = maybe_count[0]['count'] if maybe_count else 0 pipeline.extend([{ '$addFields': { 'full_name_cap': { '$toLower': '$full_name' } } }, { '$sort': { sort: (-1 if order == 'desc' else 1) } }, { '$limit': size + (page * size) }, { '$skip': (page * size) }]) cursor = list( ApiAccess.get_revenyou_db().corporate_account.aggregate(pipeline)) result = { 'total_elements': total_elements, 'total_pages': math.ceil(float(total_elements) / size), 'current_page': page, 'content': [] } for document in cursor: result['content'].append(document) result['content'][-1]['_id'] = str(result['content'][-1]['_id']) result['content'][-1]['date_added'] = utils.utc_ts_to_local_str( document.get('date_added', None)) result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str( document.get('date_modified', None)) return result