示例#1
0
def create_transfer_account_user(first_name=None,
                                 last_name=None,
                                 preferred_language=None,
                                 phone=None,
                                 email=None,
                                 public_serial_number=None,
                                 uuid=None,
                                 organisation: Organisation = None,
                                 token=None,
                                 blockchain_address=None,
                                 transfer_account_name=None,
                                 use_precreated_pin=False,
                                 use_last_4_digits_of_id_as_initial_pin=False,
                                 existing_transfer_account=None,
                                 roles=None,
                                 is_self_sign_up=False,
                                 business_usage=None,
                                 initial_disbursement=None):

    user = User(first_name=first_name,
                last_name=last_name,
                preferred_language=preferred_language,
                blockchain_address=blockchain_address,
                phone=phone,
                email=email,
                uuid=uuid,
                public_serial_number=public_serial_number,
                is_self_sign_up=is_self_sign_up,
                business_usage=business_usage)

    precreated_pin = None
    is_activated = False

    try:
        transfer_card = TransferCard.get_transfer_card(public_serial_number)
    except Exception as e:
        transfer_card = None

    if use_precreated_pin:
        precreated_pin = transfer_card.PIN
        is_activated = True

    elif use_last_4_digits_of_id_as_initial_pin:
        precreated_pin = str(public_serial_number or phone)[-4:]
        is_activated = False

    user.set_pin(precreated_pin, is_activated)

    if roles:
        for role in roles:
            user.set_held_role(role[0], role[1])
    else:
        user.remove_all_held_roles()

    if not organisation:
        organisation = Organisation.master_organisation()

    user.add_user_to_organisation(organisation, is_admin=False)

    db.session.add(user)

    if existing_transfer_account:
        transfer_account = existing_transfer_account
        user.transfer_accounts.append(existing_transfer_account)
    else:
        transfer_account = TransferAccount(
            bound_entity=user,
            blockchain_address=blockchain_address,
            organisation=organisation)

        top_level_roles = [r[0] for r in roles or []]
        is_vendor = 'VENDOR' in top_level_roles
        is_beneficiary = 'BENEFICIARY' in top_level_roles

        transfer_account.name = transfer_account_name
        transfer_account.is_vendor = is_vendor
        transfer_account.is_beneficiary = is_beneficiary

        if transfer_card:
            transfer_account.transfer_card = transfer_card

        if token:
            transfer_account.token = token

        if not is_self_sign_up:
            transfer_account.approve_and_disburse(
                initial_disbursement=initial_disbursement)

        db.session.add(transfer_account)

    user.default_transfer_account = transfer_account

    return user
示例#2
0
def create_transfer_account_user(first_name=None,
                                 last_name=None,
                                 preferred_language=None,
                                 phone=None,
                                 email=None,
                                 public_serial_number=None,
                                 organisation: Organisation = None,
                                 token=None,
                                 blockchain_address=None,
                                 transfer_account_name=None,
                                 lat=None,
                                 lng=None,
                                 use_precreated_pin=False,
                                 use_last_4_digits_of_id_as_initial_pin=False,
                                 existing_transfer_account=None,
                                 is_beneficiary=False,
                                 is_vendor=False,
                                 is_tokenagent=False,
                                 is_groupaccount=False,
                                 is_self_sign_up=False,
                                 business_usage=None,
                                 initial_disbursement=None):

    user = User(first_name=first_name,
                last_name=last_name,
                lat=lat,
                lng=lng,
                preferred_language=preferred_language,
                phone=phone,
                email=email,
                public_serial_number=public_serial_number,
                is_self_sign_up=is_self_sign_up,
                business_usage=business_usage)

    precreated_pin = None
    is_activated = False

    try:
        transfer_card = TransferCard.get_transfer_card(public_serial_number)
    except Exception as e:
        transfer_card = None

    if use_precreated_pin:
        precreated_pin = transfer_card.PIN
        is_activated = True

    elif use_last_4_digits_of_id_as_initial_pin:
        precreated_pin = str(public_serial_number or phone)[-4:]
        is_activated = False

    user.set_pin(precreated_pin, is_activated)

    if not is_vendor:
        vendor_tier = None
    elif existing_transfer_account:
        vendor_tier = 'vendor'
    else:
        vendor_tier = 'supervendor'

    user.set_held_role('VENDOR', vendor_tier)

    if is_tokenagent:
        user.set_held_role('TOKEN_AGENT', 'grassroots_token_agent')

    if is_groupaccount:
        user.set_held_role('GROUP_ACCOUNT', 'grassroots_group_account')

    if is_beneficiary:
        user.set_held_role('BENEFICIARY', 'beneficiary')

    if not organisation:
        organisation = Organisation.master_organisation()

    user.add_user_to_organisation(organisation, is_admin=False)

    db.session.add(user)

    if existing_transfer_account:
        transfer_account = existing_transfer_account
        user.transfer_accounts.append(existing_transfer_account)
    else:
        transfer_account = TransferAccount(
            bound_entity=user,
            blockchain_address=blockchain_address,
            organisation=organisation)

        transfer_account.name = transfer_account_name
        transfer_account.is_vendor = is_vendor
        transfer_account.is_beneficiary = is_beneficiary

        if transfer_card:
            transfer_account.transfer_card = transfer_card

        if token:
            transfer_account.token = token

        if not is_self_sign_up:
            transfer_account.approve_and_disburse(
                initial_disbursement=initial_disbursement)

        db.session.add(transfer_account)

    user.default_transfer_account = transfer_account

    return user