Skip to content
Snippets Groups Projects
Commit acbaab77 authored by Arno Wunderlich's avatar Arno Wunderlich
Browse files

added items are now saved to db

parent a22e174c
No related branches found
No related tags found
1 merge request!10Add item backend
......@@ -10,23 +10,23 @@
</header>
<main>
<form class="container-fluid" action="#" method="GET">
<form class="container-fluid" action="{{ url_for('views.add_item') }}" method="POST">
<div class="container">
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp">
<input type="email" class="form-control" id="email" name="email" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="nItem" class="form-label">Item's name</label>
<input type="text" class="form-control" id="nItem" aria-describedby="emailHelp">
<input type="text" class="form-control" id="nItem" name="nItem">
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" maxlength="700"></textarea>
<textarea class="form-control" id="description" name="description" maxlength="700"></textarea>
</div>
<div class="mb-3">
<label for="sPrice" class="form-label">Starting price</label>
<input type="number" class="form-control" id="sPrice" min="0" max="100000000">
<input type="number" class="form-control" id="sPrice" name="sPrice" min="0" max="100000000">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
......
......@@ -3,7 +3,10 @@ Basic views for Application
===========================
"""
from flask import Blueprint, render_template, send_from_directory
import random
from datetime import datetime, timedelta
from flask import Blueprint, render_template, send_from_directory, request, flash, url_for, redirect
from .models import Item, User
# Main blueprint.
bp = Blueprint('views', __name__)
......@@ -29,10 +32,7 @@ def test_item_adding():
Test item is added
"""
from .models import Item, User
# for test purpose create random number (unique mail address required)
import random
ran_int = str(random.randint(0,10000))
# Create a new user
......@@ -51,12 +51,56 @@ def test_item_adding():
return "OK"
@bp.route("/addItem")
def addItem() -> str:
@bp.route("/addItem", methods=('GET', 'POST'))
def add_item():
"""
AddItem page.
"""
if request.method == 'POST':
nItem = request.form.get('nItem')
print('nItem: ', nItem)
description = request.form.get('description')
starting_price = int(request.form.get('sPrice'))
error = None
print("here: 66")
if not nItem:
error = 'Item name is required.'
if not starting_price or starting_price < 1:
error = 'Starting price must be greater than 0.'
if error is None:
# Placeholder for user as long as login function is not defined
ran_int = str(random.randint(0,10000))
user = User()
user.email = "test" + ran_int + "@gmail.com"
user.password = "placeholder"
user.save()
try:
item = Item(
title=nItem,
description=description,
starting_bid=starting_price,
seller=user,
closes_at=datetime.utcnow() + timedelta(days=1)
)
item.save()
print("item saved")
except Exception as exc:
error = f"Error creating item: {exc!s}"
else:
return redirect(url_for('views.adding_successful'))
print(error)
flash(error)
item = render_template("addItem.html")
return item
@bp.route("/adding_successful")
def adding_successful():
return "Item added successfully"
Item = render_template("addItem.html", title="Adding items")
return Item
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment