Skip to content
Permalink
Browse files
feature 4 implemented
  • Loading branch information
leesj4 committed Nov 23, 2021
1 parent acb77a3 commit 725f8bece821aae504b76bfdec56008e1f5585ee
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
BIN +0 Bytes (100%) auction/database.db
Binary file not shown.
@@ -12,6 +12,7 @@ class Item(db.Model): # class that stores info about each item on the system
date_only = db.Column(db.Date, server_default=func.date()) # just the date the item was made
date = db.Column(db.DateTime(timezone=True), server_default=func.now()) # defaults the datetime to now

sold = db.Column(db.Boolean(), default=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # must pass a valid id of an existing user to this object, 1 to many

class User(db.Model, UserMixin): # creates each user in the database
@@ -144,6 +144,7 @@ def item_delete(item_id): # allows a user to delete their item from the website
user=current_user
item = Item.query.filter_by(item_id=item_id).first() # finds the item they clicked on

# prevents another user deleting items they don't own
if item.user_id == user.id: # if the current user is the items owner
Item.query.filter_by(item_id=item_id).delete() # deletes the item
db.session.commit()
@@ -152,4 +153,20 @@ def item_delete(item_id): # allows a user to delete their item from the website
else:
flash("Unauthorised user", category="warning") # tells the user if they try to delete someone elses item

return redirect(url_for('routes.home_page')) # redirects to the home page
return redirect(url_for('routes.home_page')) # redirects to the home page

@routes.route("/sold/<int:item_id>", methods=['GET', 'POST'])
@login_required
def sold(item_id): # allows a user to mark an item as sold
user=current_user
item = Item.query.filter_by(item_id=item_id).first() # finds the item they want to mark

# prevents another user marking items they don't own
if item.user_id == user.id: # if the current user is the items owner
item.sold = True
db.session.commit()

else:
flash("Unauthorised user", category="warning") # tells the user if they try to delete someone elses item

return redirect(url_for('routes.my_items')) # redirects to the home page
BIN +60.8 KB (220%) auction/static/images/3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -8,6 +8,7 @@
<!-- displays a list of all the items for sale on the website -->
<div class="row">
{% for item in items %} <!-- for loop prints information about each item -->
{% if item.sold == False %}
<div class="col-12 col-md-6 col-lg-4 mb-4"> <!-- moves cards underneath eachother when tab is small -->

<!-- creates a card that displays a picture and info about the item-->
@@ -17,18 +18,19 @@
<img class="card-img-top border-bottom" src="/static/images/{{item.item_id}}.jpg">
<div class="card-body">
<h5 class="card-title">{{item.item_name}}</h5>
<p class="card-text">£{{item.start_bid}}</p>
<main class="card-text">£{{item.start_bid}}</main>

{% for user in users %}
{% if user.id == item.user_id%}
<p class="card-text">Added by {{user.username}} on {{item.date_only}}</p>
<p><small class="card-text text-muted">Added by {{user.username}} on {{item.date_only}}</small></p>
{% endif %}
{% endfor %}

<a href="{{ url_for('routes.item_info', item_id=item.item_id) }}">More information</a>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>

@@ -1,7 +1,6 @@
{% extends "base.html"%} <!-- 'inherits' from the base.html file using jinga -->

{% block body %}

<h3 align="center">{{user.username}}'s items for sale:</h3>

<!-- displays a list of all the items put up by the current user,
@@ -15,15 +14,51 @@ but just displays the items the current user has up for sale -->
<div class="col-12 col-md-6 col-lg-4 mb-4"> <!-- moves cards underneath eachother when tab is small -->
<!-- creates a card that displays a picture and info about the item-->
<div class="card" style="width: 18rem;">

<!-- gets the relavent image from the static/images folder under the item_id.jpg -->
<img class="card-img-top border-bottom" src="/static/images/{{item.item_id}}.jpg">
<div class="card-body">
<h5 class="card-title">{{item.item_name}}</h5>
<p class="card-text">£{{item.start_bid}}</p>
<p class="card-text">Added {{item.date_only}}</p>
<a href="{{ url_for('routes.item_info', item_id=item.item_id) }}" class="card-link">More information</a>
<a href="{{ url_for('routes.item_delete', item_id=item.item_id) }}" class="card-link text-danger">Delete item</a>

<!-- if statement changes card contents, including sold badge and actions buttons depending on sold value -->
{% if item.sold == True %}

<!-- shows a sold badge if the item has been marked as sold -->
<h5 class="card-title">{{item.item_name}} <span class="badge bg-success text-light">Sold</span></h5>
<main class="card-text">£{{item.start_bid}}</main>
<p><small class="card-text text-muted">Added on {{item.date_only}}</small></p>

<!-- dropdown with user actions regarding the item -->
<div class="dropdown">
<a class="btn btn-dark dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-expanded="false">
Actions
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="{{ url_for('routes.item_info', item_id=item.item_id)}}">More information</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item text-danger" href="{{ url_for('routes.item_delete', item_id=item.item_id)}}">Delete item</a></li>
</ul>
</div>

{% else %}

<h5 class="card-title">{{item.item_name}}</h5>
<main class="card-text">£{{item.start_bid}}</main>
<p><small class="card-text text-muted">Added on {{item.date_only}}</small></p>

<!-- dropdown with user actions regarding the item -->
<div class="dropdown">
<a class="btn btn-dark dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-expanded="false">
Actions
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="{{ url_for('routes.item_info', item_id=item.item_id)}}">More information</a></li>
<li><a class="dropdown-item" href="{{ url_for('routes.sold', item_id=item.item_id)}}">Mark as sold</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item text-danger" href="{{ url_for('routes.item_delete', item_id=item.item_id)}}">Delete item</a></li>
</ul>
</div>

{% endif %}
</div>
</div>
</div>

0 comments on commit 725f8be

Please sign in to comment.