Skip to content
Snippets Groups Projects
Commit 31f86579 authored by Teemu Autto's avatar Teemu Autto
Browse files

Item selling, updating, deleting.

parent 1a94bd80
No related branches found
No related tags found
1 merge request!20Merge of the changes done in propaedeutic.
from datetime import datetime, timedelta
from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for
)
......@@ -8,6 +9,19 @@ from .models import Item
bp = Blueprint('items', __name__)
def get_item(id):
try:
item = Item.objects.get_or_404(id=id)
except Exception as exc:
print("Error getting item:", exc)
abort(404)
if item.seller == g.user:
return item
abort(403)
@bp.route('/')
def index():
items = Item.objects.all()
......@@ -16,6 +30,81 @@ def index():
)
@bp.route('/update')
def update():
...
\ No newline at end of file
@bp.route('/sell', methods=('GET', 'POST'))
@login_required
def sell():
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
starting_bid = int(request.form['starting_bid'])
error = None
if not title:
error = 'Title is required.'
if not starting_bid or starting_bid < 1:
error = 'Starting bid must be greater than 0.'
if error is None:
try:
item = Item(
title=title,
description=description,
starting_bid=starting_bid,
seller=g.user,
closes_at=datetime.utcnow() + timedelta(days=1)
)
item.save()
except Exception as exc:
error = f"Error creating item: {exc!s}"
else:
return redirect(url_for('items.index'))
print(error)
flash(error)
return render_template('items/sell.html')
@bp.route('/item/<id>/update', methods=('GET', 'POST'))
@login_required
def update(id):
item = get_item(id)
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
error = None
if not title:
error = 'Title is required.'
try:
item.title = title
item.description = description
item.save()
except Exception as exc:
error = f"Error updating item: {exc!s}"
else:
flash("Item updated successfully!")
return redirect(url_for('items.index'))
print(error)
flash(error)
return render_template('items/update.html', item=item)
@bp.route('/item/<id>/delete', methods=('POST',))
@login_required
def delete(id):
item = get_item(id)
try:
item.delete()
except Exception as exc:
error = f"Error deleting item: {exc!s}"
print(error)
flash(error)
else:
flash("Item deleted successfully!")
return redirect(url_for('items.index'))
......@@ -36,4 +36,4 @@ class Item(db.Document):
seller = ReferenceField(User, required=True)
created_at = DateTimeField(required=True, default=datetime.utcnow)
closed_at = DateTimeField()
closes_at = DateTimeField()
......@@ -23,7 +23,7 @@
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
<a class="nav-link" href="{{ url_for('items.sell') }}">Sell</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
......
......@@ -26,7 +26,7 @@
<td>
{{ item.title }}
{% if g.user == item.seller %}
<a class="action btn btn-primary" href="{{ url_for('item.update', id=item['id']) }}">Edit</a>
<a class="action btn btn-primary" href="{{ url_for('items.update', id=item['id']) }}">Edit</a>
{% endif %}
</td>
<td>{{ item.description }}</td>
......
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Sell an Item{% endblock %}</h1>
{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card p-4">
<h3 class="text-center mb-4">Add Item</h3>
<form method="post" action="{{ url_for('items.sell') }}">
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" class="form-control" value="{{ request.form['title'] }}">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" class="form-control">{{ request.form['body'] }}</textarea>
</div>
<div class="form-group">
<label for="starting_bid">Starting Bid</label>
<input type="number" name="starting_bid" id="starting_bid" min="0" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block">Add to listing</button>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Update an Item{% endblock %}</h1>
{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card p-4">
<h3 class="text-center mb-4">Update Item</h3>
<form method="post" id="delete-form" action="{{ url_for('items.delete', id=item.id) }}"></form>
<form method="post" id="update-form" action="{{ url_for('items.update', id=item.id) }}">
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" class="form-control" value="{{item.title}}">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" class="form-control">{{item.description}}</textarea>
</div>
<div class="form-group">
<label for="starting_price">Starting Price</label>
<input type="number" name="starting_bid" readonly id="starting_bid" min="0" class="form-control" value={{item.starting_bid}}>
</div>
<div class="form-group">
<div class="d-flex justify-content-end">
<button name="action" value="update" class="btn btn-primary border-dark">Update listing</button>
<button name="action" form="delete-form" value="delete" class="btn btn-danger" onclick="return confirm('Are you sure?');">Delete</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
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