Esempio n. 1
0
def market_index(request, pk):
    
    market_id = int(pk)
    mkt = get_object_or_404(Market, id=market_id)
    acc = mkt.primary_account(request.user)

    if request.method == 'POST' and acc != None:
        # user wants to post a bid
        form = MarketForm(mkt, acc, post=request.POST) # Bind data from request.POST into a form

        # parse the position from the POST data
        form.position = mkt.parse_bid(request.POST)

        # place the order
        ord = acc.place_order(mkt, form.position)
        # adds the newly created order to the form before displaying it. 
        # ONLY if the order was successfully created (i.e. non-empty)
        if ord:
            form.orders.append(ord.get_data(form.outcomes))
    else:   # just display the market page
        form = MarketForm(mkt, acc)

 
    return render(request, 'market/index.html', {
        'form': form,
        })