Exemple #1
0
def invest_complete(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):
    email = user_details[0].email
    app_id = user_details[0].app_id
    if 'confirm' in end_id:
        agreement_key = InvestmentAgreement.create_key(json.loads(tag)['investment_id'])
        deferred.defer(_invest, agreement_key, email, app_id, 0)
Exemple #2
0
 def trans():
     agreement = InvestmentAgreement.create_key(
         tag_dict['agreement_id']).get()  # type: InvestmentAgreement
     if answer_id != FormTO.POSITIVE:
         logging.info('Investment agreement sign aborted')
         return
     if agreement.status == InvestmentAgreement.STATUS_PAID:
         logging.warn(
             'Ignoring request to set InvestmentAgreement %s as paid because it is already paid',
             agreement.id)
         return
     agreement.status = InvestmentAgreement.STATUS_PAID
     agreement.paid_time = now()
     agreement.put()
     user_email, app_id, = get_app_user_tuple(agreement.app_user)
     deferred.defer(transfer_genesis_coins_to_user,
                    agreement.app_user,
                    TokenType.I,
                    long(agreement.token_count_float * 100),
                    _transactional=True)
     deferred.defer(update_investor_progress,
                    user_email.email(),
                    app_id,
                    INVESTMENT_TODO_MAPPING[agreement.status],
                    _transactional=True)
     deferred.defer(_send_tokens_assigned_message,
                    agreement.app_user,
                    _transactional=True)
Exemple #3
0
def invest_complete(status, answer_id, received_timestamp, member, message_key,
                    tag, acked_timestamp, parent_message_key, service_identity,
                    user_details):
    email = user_details[0].email
    app_id = user_details[0].app_id
    if answer_id == u'confirm':
        agreement_key = InvestmentAgreement.create_key(
            json.loads(tag)['investment_id'])
        deferred.defer(_invest, agreement_key, email, app_id, 0)
Exemple #4
0
def investment_agreement_signed(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:
        user_detail = user_details[0]
        tag_dict = json.loads(tag)
        agreement = InvestmentAgreement.create_key(tag_dict['agreement_id']).get()  # type: InvestmentAgreement

        last_step = steps[-1]
        assert isinstance(last_step, FormFlowStepTO)
        if last_step.answer_id != FormTO.POSITIVE:
            logging.info('Investment agreement was canceled')
            agreement.status = InvestmentAgreement.STATUS_CANCELED
            agreement.cancel_time = now()
            agreement.put()
            return None

        logging.info('Received signature for Investment Agreement')

        sign_result = last_step.form_result.result.get_value()
        assert isinstance(sign_result, SignWidgetResultTO)
        iyo_username = get_iyo_username(user_detail)
        sign_see_document(iyo_username, agreement.iyo_see_id, sign_result, user_detail)

        logging.debug('Storing signature in DB')
        agreement.populate(status=InvestmentAgreement.STATUS_SIGNED,
                           signature=sign_result.payload_signature,
                           sign_time=now())
        agreement.put_async()

        deferred.defer(add_user_to_role, user_detail, RogerthatRoles.INVESTOR)
        intercom_tags = get_intercom_tags_for_investment(agreement)
        if intercom_tags:
            for i_tag in intercom_tags:
                deferred.defer(intercom_helpers.tag_intercom_users, i_tag, [iyo_username])
        deferred.defer(update_investor_progress, user_detail.email, user_detail.app_id,
                       INVESTMENT_TODO_MAPPING[agreement.status])
        deferred.defer(_inform_support_of_new_investment, iyo_username, agreement.id, agreement.token_count_float)
        if needs_utility_bill(agreement):
            logging.debug('Sending "utility bill received" message')
            deferred.defer(_send_utility_bill_received, agreement.app_user)
        else:
            logging.debug('Sending confirmation message')
            deferred.defer(send_payment_instructions, agreement.app_user, agreement.id, '')
            deferred.defer(send_hoster_reminder, agreement.app_user, _countdown=1)
        result = FlowCallbackResultTypeTO(flow=FLOW_INVESTMENT_CONFIRMED,
                                          tag=None,
                                          force_language=None,
                                          flow_params=json.dumps({'reference': agreement.reference}))
        return FlowMemberResultCallbackResultTO(type=TYPE_FLOW, value=result)
    except:
        logging.exception('An unexpected error occurred')
        return create_error_message()
def manual_fix():
    to_fix = [5667908084563968]
    mapping = {5667908084563968: 0.85}  # Incomplete
    to_put = []
    agreements = ndb.get_multi(
        [InvestmentAgreement.create_key(id) for id in to_fix])
    for agreement in agreements:  # type: InvestmentAgreement
        if agreement.currency == 'USD':
            conversion = 1
        else:
            conversion = mapping[agreement.id]
        token_count_float = agreement.amount / (conversion * 5)
        precision = 8 if agreement.currency == 'BTC' else 2
        agreement.token_count = long(token_count_float * pow(10, precision))
        agreement.token_precision = precision
        to_put.append(agreement)
    ndb.put_multi(to_put)
def search_investment_agreements(query=None, page_size=20, cursor=None):
    # type: (unicode, int, unicode) -> tuple[list[InvestmentAgreement], search.Cursor, bool]
    options = search.QueryOptions(
        limit=page_size,
        cursor=search.Cursor(cursor),
        ids_only=True,
        sort_options=search.SortOptions(expressions=[
            SortExpression(expression='creation_time',
                           direction=SortExpression.DESCENDING)
        ]))
    search_results = INVESTMENT_INDEX.search(
        search.Query(query, options=options))  # type: search.SearchResults
    results = search_results.results  # type: list[search.ScoredDocument]
    investment_agreements = ndb.get_multi([
        InvestmentAgreement.create_key(long(result.doc_id))
        for result in results
    ])
    return investment_agreements, search_results.cursor, search_results.cursor is not None
Exemple #7
0
def investment_agreement_signed(status, form_result, answer_id, member,
                                message_key, tag, received_timestamp,
                                acked_timestamp, parent_message_key,
                                result_key, service_identity, user_details):
    """
    Args:
        status (int)
        form_result (FormResultTO)
        answer_id (unicode)
        member (unicode)
        message_key (unicode)
        tag (unicode)
        received_timestamp (int)
        acked_timestamp (int)
        parent_message_key (unicode)
        result_key (unicode)
        service_identity (unicode)
        user_details(list[UserDetailsTO])

    Returns:
        FormAcknowledgedCallbackResultTO
    """
    try:
        user_detail = user_details[0]
        tag_dict = json.loads(tag)
        agreement = InvestmentAgreement.create_key(
            tag_dict['agreement_id']).get()  # type: InvestmentAgreement

        if answer_id != FormTO.POSITIVE:
            logging.info('Investment agreement was canceled')
            agreement.status = InvestmentAgreement.STATUS_CANCELED
            agreement.cancel_time = now()
            agreement.put()
            return None

        logging.info('Received signature for Investment Agreement')

        sign_result = form_result.result.get_value()
        assert isinstance(sign_result, SignWidgetResultTO)
        payload_signature = sign_result.payload_signature

        iyo_organization_id = get_iyo_organization_id()
        iyo_username = get_iyo_username(user_detail)

        logging.debug('Getting IYO SEE document %s', agreement.iyo_see_id)
        doc = get_see_document(iyo_organization_id, iyo_username,
                               agreement.iyo_see_id)
        doc_view = IYOSeeDocumentView(username=doc.username,
                                      globalid=doc.globalid,
                                      uniqueid=doc.uniqueid,
                                      **serialize_complex_value(
                                          doc.versions[-1],
                                          IYOSeeDocumenVersion, False))
        doc_view.signature = payload_signature
        keystore_label = get_publickey_label(sign_result.public_key.public_key,
                                             user_detail)
        if not keystore_label:
            return create_error_message(FormAcknowledgedCallbackResultTO())
        doc_view.keystore_label = keystore_label
        logging.debug('Signing IYO SEE document')
        sign_see_document(iyo_organization_id, iyo_username, doc_view)

        logging.debug('Storing signature in DB')
        agreement.populate(status=InvestmentAgreement.STATUS_SIGNED,
                           signature=payload_signature,
                           sign_time=now())
        agreement.put_async()

        deferred.defer(add_user_to_role, user_detail, RogerthatRoles.INVESTOR)
        intercom_tags = get_intercom_tags_for_investment(agreement)
        if intercom_tags:
            for i_tag in intercom_tags:
                deferred.defer(intercom_helpers.tag_intercom_users, i_tag,
                               [iyo_username])
        deferred.defer(update_investor_progress, user_detail.email,
                       user_detail.app_id,
                       INVESTMENT_TODO_MAPPING[agreement.status])
        deferred.defer(_inform_support_of_new_investment, iyo_username,
                       agreement.id, agreement.token_count_float)
        logging.debug('Sending confirmation message')
        prefix_message = u'Thank you. We successfully received your digital signature.' \
                         u' We have stored a copy of this agreement in your ThreeFold Documents.' \
                         u' We will contact you again when we have received your payment.' \
                         u' Thanks again for your purchase and your support of the ThreeFold Foundation!' \
                         u'\n\nWe would like to take this opportunity to remind you once again to keep a back-up of' \
                         u' your wallet in a safe place, by writing down the 29 words that can be used to restore it' \
                         u' to a different device.' \
                         u' As usual, if you have any questions, don\'t hesitate to contact us.\n\n'
        msg = u'%sReference: %s' % (prefix_message, agreement.reference)
        deferred.defer(send_payment_instructions, agreement.app_user,
                       agreement.id, prefix_message)

        message = MessageCallbackResultTypeTO()
        message.alert_flags = Message.ALERT_FLAG_VIBRATE
        message.answers = []
        message.branding = get_main_branding_hash()
        message.dismiss_button_ui_flags = 0
        message.flags = Message.FLAG_ALLOW_DISMISS | Message.FLAG_AUTO_LOCK
        message.message = msg
        message.step_id = u'investment_agreement_accepted'
        message.tag = None

        result = FormAcknowledgedCallbackResultTO()
        result.type = TYPE_MESSAGE
        result.value = message
        return result
    except:
        logging.exception('An unexpected error occurred')
        return create_error_message(FormAcknowledgedCallbackResultTO())