示例#1
0
def campaign_create():
    payload = request.get_json() or {}
    sender = payload.get('sender') or None
    spreadsheet_url = payload.get('source')
    target = payload.get('target') or None
    wallet_pass = payload.get('wallet_pass') or None
    campaign_pass = payload.get('campaign_pass') or None

    if not spreadsheet_url:
        return jsonify({'error': 'Sheet url not specified'}), HTTP_400_BAD_REQUEST

    spreadsheet_or_error = get_spreadsheet(spreadsheet_url)

    if isinstance(spreadsheet_or_error, dict):
        return jsonify(spreadsheet_or_error), HTTP_400_BAD_REQUEST
    elif not isinstance(spreadsheet_or_error, Spreadsheet):
        return jsonify({'error': 'Internal API error'}), HTTP_500_INTERNAL_SERVER_ERROR

    recipients = parse_recipients(spreadsheet_or_error)
    if not recipients:
        return jsonify({'error': 'Recipient list is empty'}), HTTP_400_BAD_REQUEST

    total_cost = sum(info['amount'] for info in recipients.values())
    total_fee = 0.02 * len(recipients)
    campaign_cost = total_cost + total_fee

    campaign_wallet = generate_and_save_wallet()
    campaign = PushCampaign.create(
        wallet_link_id=campaign_wallet.link_id,
        status='open',
        cost_pip=str(to_pip(campaign_cost)),
        company=sender,
        password=campaign_pass)

    for info in recipients.values():
        balance = str(to_pip(info['amount'] + 0.01))
        wallet = generate_and_save_wallet(
            sender=sender, recipient=info['name'], password=wallet_pass,
            campaign_id=campaign.id, virtual_balance=balance)
        info['token'] = wallet.link_id

    Recipient.bulk_create([Recipient(
        email=email, campaign_id=campaign.id,
        name=info['name'], amount_pip=str(to_pip(info['amount'])),
        wallet_link_id=info['token'], target=target
    ) for email, info in recipients.items()])

    return jsonify({
        'campaign_id': campaign.id,
        'address': campaign_wallet.address,
        'deeplink': create_deeplink(campaign_wallet.address, campaign_cost),
        'total_bip': campaign_cost
    })
示例#2
0
def create_campaign(recipients,
                    sender,
                    cost,
                    campaign_pass=None,
                    wallet_pass=None,
                    target=None,
                    customization_id=None):
    campaign_pass_hash = pbkdf2_sha256.hash(
        campaign_pass) if campaign_pass is not None else None
    campaign_wallet = generate_and_save_wallet()
    campaign = PushCampaign.create(wallet_link_id=campaign_wallet.link_id,
                                   status='open',
                                   cost_pip=str(to_pip(cost)),
                                   company=sender,
                                   password_hash=campaign_pass_hash,
                                   customization_setting_id=customization_id)

    for info in recipients.values():
        balance = str(to_pip(info['amount'] + 0.01))
        wallet = generate_and_save_wallet(
            sender=sender,
            recipient=info['name'],
            password=wallet_pass,
            campaign_id=campaign.id,
            virtual_balance=balance,
            customization_setting_id=customization_id)
        info['token'] = wallet.link_id

    Recipient.bulk_create([
        Recipient(email=email,
                  campaign_id=campaign.id,
                  name=info['name'],
                  amount_pip=str(to_pip(info['amount'])),
                  wallet_link_id=info['token'],
                  target_shop=target) for email, info in recipients.items()
    ],
                          batch_size=100)
    return campaign, campaign_wallet