def trans(): profile_key = TffProfile.create_key(username) profile = profile_key.get() if not profile: profile = TffProfile(key=profile_key, app_user=create_app_user_by_email( user_detail.email, user_detail.app_id)) pp_key = ProfilePointer.create_key(username) pp = pp_key.get() if pp: logging.error( "Failed to save invitation code of user '%s', we have a duplicate", user_detail.email) deferred.defer(store_invitation_code_in_userdata, username, user_detail, _countdown=10 * 60, _queue=SCHEDULED_QUEUE, _transactional=True) return False profile.put() pp = ProfilePointer(key=pp_key, username=username) pp.put() return True
def trans(): code = params.get("code") if not code: raise ApiCallException(u'Unknown invitation code received') pp = ProfilePointer.get_by_user_code(code) if not pp: raise ApiCallException(u'Unknown invitation code received') username = get_iyo_username(user_detail) if username == pp.username: raise ApiCallException(u'You can\'t use your own invitation code') my_profile = TffProfile.create_key(username).get() # type: TffProfile if not my_profile: raise ApiCallException(u'We were unable to find your profile') if my_profile.referrer_user: raise ApiCallException(u'You already set your referrer') referrer_profile = TffProfile.create_key(pp.username).get() if not referrer_profile: raise ApiCallException( u'We were unable to find your referrer\'s profile') my_profile.referrer_user = referrer_profile.app_user my_profile.referrer_username = pp.username my_profile.put() deferred.defer(remove_user_from_role, user_detail, RogerthatRoles.PUBLIC, _transactional=True) deferred.defer(add_user_to_role, user_detail, RogerthatRoles.MEMBERS, _transactional=True) deferred.defer(remove_user_from_organization, username, Organization.PUBLIC, _transactional=True) deferred.defer(invite_user_to_organization, username, Organization.MEMBERS, _transactional=True) deferred.defer(store_referral_in_user_data, my_profile.key, _transactional=True)
def api_get_node_status(params, user_detail): # type: (dict, UserDetailsTO) -> list[dict] try: profile = TffProfile.create_key( get_iyo_username(user_detail)).get() # type: TffProfile if not DEBUG and not profile.nodes: # fallback, should only happen when user checks his node status before our cron job has ran. logging.warn( 'Fetching node serial number from odoo since it was not set on TffProfile for user %s', user_detail.email) app_user = create_app_user(users.User(user_detail.email), user_detail.app_id) nodes = get_nodes_for_user(app_user) if nodes: username = get_iyo_username(app_user) profile = add_nodes_to_profile(username, nodes) else: raise ApiCallException( u'It looks like you either do not have a node yet or it has never been online yet.' ) return get_nodes_stats_from_influx(profile.nodes) except ApiCallException: raise except Exception as e: logging.exception(e) raise ApiCallException( u'Could not get node status. Please try again later.')
def get_extra_profile_fields(self, profile): tff_profile = TffProfile.create_key( profile.username).get() # type: TffProfile if not tff_profile: logging.error('No TffProfile found for profile %s', profile) return [] kyc_status = (tff_profile.kyc and tff_profile.kyc.status) or KYCStatus.UNVERIFIED.value return [search.NumberField('kyc_status', kyc_status)]
def _set_node_id(order_key): order = order_key.get() # type: NodeOrder node_id = get_node_id_from_odoo(order.odoo_sale_order_id) if node_id: username = get_iyo_username(order.app_user) profile = TffProfile.create_key(username).get() # type: TffProfile profile.node_id = node_id profile.put() logging.info('Saved node_id %s for user %s', node_id, username)
def invest(message_flow_run_id, member, steps, end_id, end_message_flow_id, parent_message_key, tag, result_key, flush_id, flush_message_flow_id, service_identity, user_details, flow_params, token): if flush_id == 'flush_kyc' or flush_id == 'flush_corporation': # KYC flow started from within the invest flow return kyc_part_1(message_flow_run_id, member, steps, end_id, end_message_flow_id, parent_message_key, tag, result_key, flush_id, flush_message_flow_id, service_identity, user_details, flow_params) try: email = user_details[0].email app_id = user_details[0].app_id app_user = create_app_user_by_email(email, app_id) logging.info('User %s wants to invest', email) version = get_key_name_from_key_string(steps[0].message_flow_id) currency = get_step_value(steps, 'message_get_currency').replace('_cur', '') if version.startswith(BUY_TOKENS_FLOW_V3) or version.startswith(BUY_TOKENS_FLOW_V5): amount = float(get_step_value(steps, 'message_get_order_size_ITO').replace(',', '.')) token_count_float = get_token_count(currency, amount) else: token_count_float = float(get_step_value(steps, 'message_get_order_size_ITO')) amount = get_investment_amount(currency, token_count_float) username = get_iyo_username(app_user) agreement = _create_investment_agreement(amount, currency, token, token_count_float, username, version, app_user, status=InvestmentAgreement.STATUS_CREATED) payment_info = [] usd_within_uae_step = get_step(steps, 'message_usd_within_uae') if usd_within_uae_step and usd_within_uae_step.answer_id == 'button_yes': payment_info.append(PaymentInfo.UAE.value) agreement.payment_info.extend(payment_info) agreement.put() if version == BUY_TOKENS_FLOW_V3_PAUSED: return None utility_bill_step = get_step(steps, 'message_utility_bill') if utility_bill_step: azzert(utility_bill_step.answer_id == FormTO.POSITIVE) url = utility_bill_step.get_value() deferred.defer(save_utility_bill, url, TffProfile.create_key(get_iyo_username(user_details[0]))) tag = { '__rt__.tag': INVEST_FLOW_TAG, 'investment_id': agreement.id } flow_params = { 'token': agreement.token, 'amount': agreement.amount, 'currency': agreement.currency } result = FlowCallbackResultTypeTO(flow=FLOW_CONFIRM_INVESTMENT, tag=json.dumps(tag).decode('utf-8'), force_language=None, flow_params=json.dumps(flow_params)) return FlowMemberResultCallbackResultTO(type=TYPE_FLOW, value=result) except Exception as e: logging.exception(e) return create_error_message()
def get_tff_profile(username): # type: (unicode) -> TffProfile profile = TffProfile.create_key(username).get() if not profile: raise HttpNotFoundException('tff_profile_not_found', {'username': username}) if not profile.kyc: profile.kyc = KYCInformation(status=KYCStatus.UNVERIFIED.value, updates=[], applicant_id=None) return profile
def get_extra_profile_fields(self, profile): tff_profile = TffProfile.create_key( profile.username).get() # type: TffProfile if not tff_profile: logging.debug('No TffProfile found for profile %s', profile) return [] kyc_status = (tff_profile.kyc and tff_profile.kyc.status) or KYCStatus.UNVERIFIED.value return [ search.NumberField('kyc_status', kyc_status), search.TextField('app_email', tff_profile.app_user.email().lower()) ]
def list_nodes_by_status(status=None): # type: (unicode) -> list[UserNodeStatusTO] if status: qry = TffProfile.list_by_node_status(status) else: qry = TffProfile.list_with_node() tff_profiles = qry.fetch() # type: list[TffProfile] profiles = { profile.username: profile for profile in ndb.get_multi( [Profile.create_key(p.username) for p in tff_profiles]) } results = [] for tff_profile in tff_profiles: for node in tff_profile.nodes: if node.status == status or not status: results.append( UserNodeStatusTO( profile=profiles.get(tff_profile.username).to_dict() if tff_profile.username in profiles else None, node=node.to_dict())) return sorted(results, key=lambda k: k.profile and k.profile['info']['firstname'])
def trans(): code = params.get("code").lower() pp = ProfilePointer.get_by_user_code(code) if not code or not pp: raise ApiCallException(u'Unknown invitation code received') username = get_iyo_username(user_detail) if username == pp.username: raise ApiCallException(u'You can\'t use your own invitation code') my_profile = TffProfile.create_key(username).get() # type: TffProfile if not my_profile: raise ApiCallException(u'We were unable to find your profile') if my_profile.referrer_user: raise ApiCallException(u'You already set your referrer') referrer_profile = TffProfile.create_key(pp.username).get() if not referrer_profile: raise ApiCallException( u'We were unable to find your referrer\'s profile') my_profile.referrer_user = referrer_profile.app_user my_profile.referrer_username = pp.username my_profile.put() deferred.defer(_setup_permissions, username, user_detail, _transactional=True) deferred.defer(store_referral_in_user_data, my_profile.key, _transactional=True) deferred.defer(notify_new_referral, username, referrer_profile.app_user, _transactional=True)
def add_nodes_to_profile(iyo_username, nodes): profile = TffProfile.create_key(iyo_username).get() existing_ids = [n.id for n in profile.nodes] for node in nodes: if node['id'] not in existing_ids: profile.nodes.append(NodeInfo(**node)) profile.put() user, app_id = get_app_user_tuple(profile.app_user) data = {'nodes': [n.to_dict() for n in profile.nodes]} deferred.defer(system.put_user_data, get_rogerthat_api_key(), user.email(), app_id, data, _transactional=True) return profile
def _profiles_with_referrer(): return TffProfile.query()
def _get_all_tff_profiles(): return TffProfile.query()
def set_node_id_on_profile(app_user, node_id): username = get_iyo_username(app_user) profile = TffProfile.create_key(username).get() profile.node_id = node_id profile.put()
def _get_profiles_with_node(): return TffProfile.list_with_node()