def get_total_investment_value(app_user): statuses = (InvestmentAgreement.STATUS_PAID, InvestmentAgreement.STATUS_SIGNED) total_token_count = get_total_token_count(app_user, InvestmentAgreement.list_by_status_and_user(app_user, statuses)) tokens = total_token_count.keys() stats = dict(zip(tokens, ndb.get_multi([GlobalStats.create_key(token) for token in tokens]))) total_usd = 0 for token, token_count in total_token_count.iteritems(): total_usd += token_count * stats[token].value logging.debug('The tokens of %s are worth $%s', app_user, total_usd) return total_usd
def create_itft_amendment_1_pdf(app_user): from plugins.tff_backend.bizz.investor import get_total_token_count agreements = InvestmentAgreement.list_by_status_and_user(app_user, (InvestmentAgreement.STATUS_PAID, InvestmentAgreement.STATUS_SIGNED)) azzert(agreements) agreements.sort(key=lambda a: a.sign_time) purchase_amounts = '' sign_dates = '' for i, agreement in enumerate(agreements): if i: purchase_amounts += '<br>' sign_dates += '<br>' purchase_amounts += '%s %s' % (agreement.amount, agreement.currency) sign_dates += _get_effective_date(agreement.sign_time) old_count = get_total_token_count(app_user, agreements)[TOKEN_ITFT] new_count = old_count * 100.0 purchase_amount_in_usd = old_count * 5.0 fmt = lambda x: '{:.2f}'.format(x) template_variables = { 'logo_path': 'assets/logo.jpg', 'agreement': _get_effective_date, 'full_name': agreements[0].name, 'purchase_amounts': purchase_amounts, 'sign_dates': sign_dates, 'old_count': fmt(old_count), 'new_count': fmt(new_count), 'purchase_amount_in_usd': fmt(purchase_amount_in_usd), 'title': 'iTFT Purchase Agreement - Amendment I<br>iTFT Token Price & Volume Adjustment' } md = JINJA_ENVIRONMENT.get_template('itft_amendment_1.md').render(template_variables) markdown_to_html = markdown.markdown(md, extensions=['markdown.extensions.tables']) template_variables['markdown_to_html'] = markdown_to_html.replace('<th', '<td') return _render_pdf_from_html('token_itft.html', template_variables)
def send_signed_investments_messages(app_user): agreements = InvestmentAgreement.list_by_status_and_user(app_user, InvestmentAgreement.STATUS_SIGNED) for agreement in agreements: deferred.defer(send_payment_instructions, app_user, agreement.id, '')
def _order_node(order_key, user_email, app_id, steps): logging.info('Receiving order of Zero-Node') app_user = create_app_user_by_email(user_email, app_id) overview_step = get_step(steps, 'message_overview') if overview_step and overview_step.answer_id == u"button_use": api_key = get_rogerthat_api_key() user_data_keys = [ 'name', 'email', 'phone', 'billing_address', 'address', 'shipping_name', 'shipping_email', 'shipping_phone', 'shipping_address' ] user_data = system.get_user_data(api_key, user_email, app_id, user_data_keys) billing_info = ContactInfo(name=user_data['name'], email=user_data['email'], phone=user_data['phone'], address=user_data['billing_address'] or user_data['address']) if user_data['shipping_name']: shipping_info = ContactInfo(name=user_data['shipping_name'], email=user_data['shipping_email'], phone=user_data['shipping_phone'], address=user_data['shipping_address']) else: shipping_info = billing_info updated_user_data = None else: name = get_step_value(steps, 'message_name') email = get_step_value(steps, 'message_email') phone = get_step_value(steps, 'message_phone') billing_address = get_step_value(steps, 'message_billing_address') updated_user_data = { 'name': name, 'email': email, 'phone': phone, 'billing_address': billing_address, } billing_info = ContactInfo(name=name, email=email, phone=phone, address=billing_address) same_shipping_info_step = get_step(steps, 'message_choose_shipping_info') if same_shipping_info_step and same_shipping_info_step.answer_id == u"button_yes": shipping_info = billing_info else: shipping_name = get_step_value(steps, 'message_shipping_name') shipping_email = get_step_value(steps, 'message_shipping_email') shipping_phone = get_step_value(steps, 'message_shipping_phone') shipping_address = get_step_value(steps, 'message_shipping_address') updated_user_data.update({ 'shipping_name': shipping_name, 'shipping_email': shipping_email, 'shipping_phone': shipping_phone, 'shipping_address': shipping_address, }) shipping_info = ContactInfo(name=shipping_name, email=shipping_email, phone=shipping_phone, address=shipping_address) socket_step = get_step(steps, 'message_socket') socket = socket_step and socket_step.answer_id.replace('button_', '') # Only one node is allowed per user, and one per location if NodeOrder.has_order_for_user_or_location( app_user, billing_info.address) and not DEBUG: logging.info('User already has a node order, sending abort message') msg = u'Dear ThreeFold Member, we sadly cannot grant your request to host an additional ThreeFold Node:' \ u' We are currently only allowing one Node to be hosted per ThreeFold Member and location.' \ u' This will allow us to build a bigger base and a more diverse Grid.' subject = u'Your ThreeFold Node request' send_message_and_email(app_user, msg, subject) return # Check if user has invested >= 120 tokens paid_orders = InvestmentAgreement.list_by_status_and_user( app_user, InvestmentAgreement.STATUS_PAID) total_tokens = sum([o.token_count_float for o in paid_orders]) can_host = total_tokens >= REQUIRED_TOKEN_COUNT_TO_HOST def trans(): logging.debug('Storing order in the database') order = NodeOrder(key=order_key, app_user=app_user, tos_iyo_see_id=None, billing_info=billing_info, shipping_info=shipping_info, order_time=now(), status=NodeOrderStatus.APPROVED if can_host else NodeOrderStatus.WAITING_APPROVAL, socket=socket) order.put() if can_host: logging.info( 'User has invested more than %s tokens, immediately creating node order PDF.', REQUIRED_TOKEN_COUNT_TO_HOST) deferred.defer(_create_node_order_pdf, order_key.id(), _transactional=True) else: logging.info( 'User has not invested more than %s tokens, an admin needs to approve this order manually.', REQUIRED_TOKEN_COUNT_TO_HOST) deferred.defer(_inform_support_of_new_node_order, order_key.id(), _transactional=True) deferred.defer(set_hoster_status_in_user_data, order.app_user, False, _transactional=True) if updated_user_data: deferred.defer(put_user_data, app_user, updated_user_data, _transactional=True) ndb.transaction(trans)