Exemplo n.º 1
0
def api_order_create(request):
    items = request.data.get('items')
    shop_data = request.data.get('shop')
    shop = get_object_or_404(Shop, pk=shop_data.get('id'))
    user_pubkey = request.data.get('pubkey')

    wallet = Wallet.for_pub_key(user_pubkey)
    user = wallet.user

    # TODO Make transaction out of this entire function.
    order = Order.objects.create(
        shop=shop,
        user=user,
        wallet=wallet,
        status=Order.STATUS.pending,
        total_amount=0,
    )

    total_amount = 0

    for item in items:
        product_id = item.get('id')
        count = item.get('count')

        if not count:
            continue

        product = Product.objects.get(pk=product_id, shops__in=[shop])

        amount = count * product.price
        line_item = LineItem(
            product_ref=product,
            product_price=product.price,
            product_name=product.name,
            product_description=product.description,
            product_picture=product.picture,
            product_icon=product.icon,
            order=order,
            count=count,
            total_amount=amount,
        )

        line_item.save()
        total_amount += product.price * count

    order.total_amount = total_amount
    order.save()

    order.tx = blockchain.get_transfer_tx(wallet.pub_key, shop.pub_key,
                                          total_amount)

    return Response(OrderSerializer(order).data)
Exemplo n.º 2
0
def signup(request):
    pub_key = request.data.get('pub_key')

    try:
        wallet = Wallet.for_pub_key(pub_key)
    except Wallet.DoesNotExist:
        pub_hash = pub_key[-6:]
        user = User.objects.create(email='{}@cryptofest.net'.format(pub_hash))

        wallet = Wallet.objects.create(
            pub_key=pub_key,
            balance=0,
            user=user,
        )

        wallet.transfer_aeter(int(0.01 * 10e18))

    return Response(dict(id=wallet.user.id, ))
Exemplo n.º 3
0
def get_balance(request, pub):
    wallet = Wallet.for_pub_key(pub)
    balance = wallet.update_balance_from_blockchain()
    return JsonResponse({"balance": wallet.balance})