Web701 19

May 14, 2019
Web701 Python Django Auction system

Web Development 701 ~ Auction System (cont.)

In the last blog we covered authentication with the TradeMe API. After some playing around, I hit a wall with the list an item API method. Instead of wasting time trying to figure it out I am going to implement a simple dummy auction system. This is partly because I’d rather have something for an auction system than nothing and also partly because I have more important projects to work on.

The dummy auction system is going to be a dictionary which contains some dummy data. The dummy database (dictionary) is as follows.

dummy_auction_items = {
    "Items": [
        {
            "name": "strawberries",
            "quantity": 100,
            "price": 1.35,
            "bidders": []
        },
        {
            "name": "blueberries",
            "quantity": 70,
            "price": 2.10,
            "bidders": []
        },
        {
            "name": "blackberries",
            "quantity": 130,
            "price": 3.99,
            "bidders": []
        },
        {
            "name": "kiwiberries",
            "quantity": 50,
            "price": 6.82,
            "bidders": []
        },
    ]
}

As stated above it’s going to be a simple auction system, so I am not going to go overboard with the complexity of the data. These are the required pieces of information (I believe) that would be required for a basic auction system.

The first thing we need to do is to create a new app, which we will do via django-admin startapp auction, then add auction to the INSTALLED_APPS list in thesettings.py file of the web701 directory.

We now need to add the following to the urls.py file in the web701 directory.

path('', include('auction.urls'))

Next to create a file called urls.py in the newly made auction app and add the following.

path('auction', views.auction, name='auction'),
path('bid_item', views.bid_item, name='bid_item'),
path('sell_item', views.sell_item, name='sell_item')

Now to create the views in views.py via the following.

def auction(request):
    context = {'listings': dummy_auction_items}
    return render(request, 'auction/auction.html', context=context)
def bid_item(request):
    if request.method == 'POST':
        form_data = request.POST.dict()

        bidder = form_data.get('user')
        quantity = int(form_data.get('quantity'))
        listing = form_data.get('listing')
        price = float(form_data.get('price'))
        total_price = price * quantity

        for val in dummy_auction_items.values():
            for item in val:
                if listing == item['name']:
                    item['bidders'].append({
                                        'name': bidder, 
                                        'quantity': quantity, 
                                        'price': total_price})
                    item['quantity'] -= quantity
        return redirect('auction')

    return redirect('auction')
def sell_item(request):
    if request.method == 'POST':
        form_data = request.POST.dict()

        item_data = {
            'name': form_data.get('name'),
            'quantity': int(form_data.get('quantity')),
            'price': float(form_data.get('price')),
            'bidders': []
        }

        dummy_auction_items["Items"].append(item_data)
        return redirect('auction')

    return render(request, 'auction/sell.html')

Now the auction.html file (do note that it will be in the auction directory under the templates directory which we will need to make). The auction.html file will look as follows.

{% extends "skeleton.html" %}

{% block title %}Auction{% endblock %}

{% block body %}
<h1>Auction | <a href="{% url 'sell_item' %}">Sell Item</a></h1>
<hr>
<h3>Auction Listings</h3>
<ol>
    {% for listing in listings.Items %}
    <li>
        Name: {{ listing.name }}
        <br>
        Quantity: {{ listing.quantity }}
        <br>
        Price: {{ listing.price }}
        <br>
        <br>
        {% if listing.bidders %}
            {% for bidder in listing.bidders %}
                Bidder: {{ bidder.name }}
                &nbsp;
                Quantity: {{ bidder.quantity }}
                &nbsp;
                Price: {{ bidder.price }}
                <br>
            {% endfor %}
        {% endif %}
        <br>
        <form method="POST" action="{% url 'bid_item' %}">
            {% csrf_token %}
            Quantity:&nbsp;
            <input type="number" name="quantity" min="1" max="{{listing.quantity}}" value="1">
            <br>
            <input type="hidden" name="price" value="{{ listing.price }}">
            <input type="hidden" name="listing" value="{{ listing.name }}">
            <input type="hidden" name="user" value="{{ request.user.username }}">
            <input type="submit" value="bid">
        </form>
        <br>
        <br>
    </li>
    {% endfor %}
</ol>
{% endblock %}

Now the sell.html template.

{% extends "skeleton.html" %}

{% block title %}Sell Item{% endblock %}

{% block body %}
<h1>Sell Item</h1>
<form method="POST" action="{% url 'sell_item' %}">
        {% csrf_token %}

        Name:&nbsp; <input type="input" name="name">
        <br>
        Price:&nbsp; <input type="number" step="0.01" name="price">
        <br>
        Quantity:&nbsp; <input type="number" step="1" name="quantity">
        <br>
        <br>
        <input type="submit" value="register">
    </form>
{% endblock %}

With that users now have the ability to sell and bid on items in our (dummy) auction system.

Web701 22

June 15, 2019
Web701 Serverless OpenFaas Docker CLI Python Digital Ocean

Web701 21

May 21, 2019
Web701 VirtualBox Serverless OpenFaas Docker CLI Python

Web701 20

May 20, 2019
Web701 Python Django Heroku Web Hosting
comments powered by Disqus