Skip to content
Snippets Groups Projects
Commit f681c4f5 authored by Tayeeb Islam's avatar Tayeeb Islam
Browse files

Commit 4

parent 5697686c
No related branches found
No related tags found
No related merge requests found
Showing
with 95 additions and 110 deletions
......@@ -6,9 +6,6 @@ from flask_login import LoginManager
app = Flask(__name__)
app.config['SECRET_KEY'] = 'edd9229339e5754427a7792fc79b51318395e93569165fa4'
# Note: type 'app.config[..]' as one line, it is split into
# multiple lines in this document to fit on the page
# {USERNAME} and {YOUR_DATABASE} are your Cardiff username
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://c2003538:Newport123@csmysql.cs.cf.ac.uk:3306/c2003538_Online_Shop'
db = SQLAlchemy(app)
login_manager = LoginManager()
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -16,13 +16,13 @@ class RegistrationForm(FlaskForm):
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first()
if user:
raise ValidationError('Oh no! This username has already been taken. Please choose a different one.')
raise ValidationError('This username has already been taken. Please choose a different one.')
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user:
raise ValidationError('Oh no! This email is already registered. Please choose a different one.')
raise ValidationError('This email is already registered. Please choose a different one.')
class LoginForm(FlaskForm):
......@@ -32,19 +32,14 @@ class LoginForm(FlaskForm):
class SearchForm(FlaskForm):
choices = [('Rose', 'Rose'), ('Lily', 'Lily'), ('Daisy', 'Daisy'), ('Sunflower', 'Sunflower'), ('Carnation', 'Carnation'), ('Freesia', 'Freesia'), ('Jasmine', 'Jasmine'), ('Peony', 'Peony'), ('Amaryllis', 'Amaryllis')]
select = SelectField('Search flower:', choices=choices)
choices = [('Pen', 'Pen'), ('Pencil', 'Pencil'), ('Rubber', 'Rubber'), ('USB', 'USB'), ('Rubber', 'Rubber'), ('Calculator', 'Calculator')]
select = SelectField('Search item:', choices=choices)
search = StringField('')
class CheckoutForm(FlaskForm):
forename = StringField('Forename', validators=[DataRequired('Please enter your forename'), Length(min=3, max=15)])
surname = StringField('Surname', validators=[DataRequired('Please enter your surname'), Length(min=3, max=15)])
address = StringField('Address',
validators=[DataRequired('Please enter your billing address')])
card_no = PasswordField('card_no', validators=[DataRequired('Please enter your 16-digit card number'), Length(min=16, max=16)])
cvc = PasswordField('cvc', validators=[DataRequired('Please enter your 3-digit CVC'), Length(min=3, max=3)])
submit = SubmitField('Checkout')
FisrtName = StringField('First Name', validators=[DataRequired('Please enter your First Name'), Length(min=3, max=15)])
LastName = StringField('Last Name', validators=[DataRequired('Please enter your Last Name'), Length(min=3, max=15)])
Adress = StringField('Address', validators=[DataRequired('Please enter your Address')])
Card_No = PasswordField('Card_No', validators=[DataRequired('Please enter your 16-digit card number'), Length(min=16, max=16)])
CVC = PasswordField('CVC', validators=[DataRequired('Please enter your 3-digit CVC'), Length(min=3, max=3)])
Submit = SubmitField('Checkout')
......@@ -7,7 +7,6 @@ class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50),nullable=False)
description = db.Column(db.String(300), nullable=False)
#publication_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
price = db.Column(db.Numeric(10,2), nullable=False)
image_file = db.Column(db.String(50), nullable=False, default='default.jpg')
......@@ -40,7 +39,7 @@ class User(UserMixin, db.Model):
def load_user(user_id):
return User.query.get(int(user_id))
class Cart(db.Model):
class Basket(db.Model):
id = db.Column(db.Integer, primary_key=True)
quantity = db.Column(db.Integer)
item_id = db.Column(db.Integer, db.ForeignKey('item.id'), nullable=False)
......@@ -5,6 +5,7 @@ from shop.models import Item, User
from shop.forms import RegistrationForm, LoginForm, SearchForm, CheckoutForm
from flask_login import login_user, current_user, logout_user, login_required
@app.route("/", methods= ['GET', 'POST'])
@app.route("/home", methods= ['GET', 'POST'])
......@@ -23,18 +24,18 @@ def about():
return render_template('about.html', title='About')
@app.route("/orderasc")
def orderasc():
@app.route("/Asc")
def Asc():
return render_template('home.html', items = Item.query.order_by("price"), title='Home')
@app.route("/orderdesc")
def orderdesc():
@app.route("/Desc")
def Desc():
return render_template('home.html', items = Item.query.order_by("price")[::-1], title='Home')
@app.route("/item/<int:item_id>")
def item(item_id):
item = Item.query.get_or_404(item_id)
return render_template('item.html', item=item) #title=book.title
return render_template('item.html', item=item)
@app.route("/register", methods=['GET', 'POST'])
def register():
......@@ -54,8 +55,7 @@ def register():
def checkout():
form = CheckoutForm()
if form.validate_on_submit():
#user = User(forename=form.forename.data,surname=form.surname.data)
session["cart"] = []
session["basket"] = []
return redirect(url_for('thankyou'))
return render_template('checkout.html', title='Checkout', form=form)
......@@ -82,22 +82,22 @@ def logout():
logout_user()
return redirect(url_for('home'))
@app.route("/add_to_cart/<int:item_id>")
def add_to_cart(item_id):
if "cart" not in session:
session["cart"] = []
session["cart"].append(item_id)
flash("The item is added to your shopping cart!")
return redirect("/cart")
@app.route("/cart", methods=['GET', 'POST'])
def cart_display():
if "cart" not in session:
flash('There is nothing in your cart.')
return render_template("cart.html", display_cart = {}, total = 0)
@app.route("/add_to_basket/<int:item_id>")
def add_to_basket(item_id):
if "basket" not in session:
session["basket"] = []
session["basket"].append(item_id)
flash("The item is added to your shopping basket!")
return redirect("/basket")
@app.route("/basket", methods=['GET', 'POST'])
def basket_display():
if "basket" not in session:
flash('There is nothing in your basket.')
return render_template("basket.html", display_basket = {}, total = 0)
else:
items = session["cart"]
cart = {}
items = session["basket"]
basket = {}
total_price = 0
total_quantity = 0
......@@ -105,29 +105,29 @@ def cart_display():
item = Item.query.get_or_404(item)
total_price += item.price
if item.id in cart:
cart[item.id]["quantity"] += 1
if item.id in basket:
basket[item.id]["quantity"] += 1
else:
cart[item.id] = {"quantity":1, "title": item.title, "price":item.price}
total_quantity = sum(item['quantity'] for item in cart.values())
basket[item.id] = {"quantity":1, "title": item.title, "price":item.price}
total_quantity = sum(item['quantity'] for item in basket.values())
return render_template("cart.html", title='Your Shopping Cart', display_cart = cart, total = total_price, total_quantity = total_quantity)
return render_template("basket.html", title='Your Shopping basket', display_basket = basket, total = total_price, total_quantity = total_quantity)
return render_template('cart.html')
return render_template('basket.html')
@app.route("/delete_item/<int:item_id>", methods=['GET', 'POST'])
def delete_item(item_id):
if "cart" not in session:
session["cart"] = []
if "basket" not in session:
session["basket"] = []
session["cart"].remove(item_id)
session["basket"].remove(item_id)
flash("The item has been removed from your shopping cart!")
flash("The item has been removed from your shopping basket!")
session.modified = True
return redirect("/cart")
return redirect("/basket")
@app.route("/add_to_wishlist/<int:item_id>")
def add_to_wishlist(item_id):
......@@ -158,7 +158,7 @@ def wishlist_display():
wishlist[item.id] = {"quantity":1, "title": item.title, "price":item.price}
total_quantity_wishlist = sum(item_id['quantity'] for item_id in wishlist.values())
return render_template("wishlist.html", title= "Your Shopping Cart", display_wishlist = wishlist, total = total_price, total_quantity_wishlist = total_quantity_wishlist)
return render_template("wishlist.html", title= "Your Shopping Basket", display_wishlist = wishlist, total = total_price, total_quantity_wishlist = total_quantity_wishlist)
@app.route("/delete_item_wishlist/<int:item_id>", methods=['GET', 'POST'])
def delete_item_wishlist(item_id):
......
body {
font-family:helvetica, sans-serif;
font-family:Arial, sans-serif;
background-color: white;
}
h1 {
padding-left: 20px;
font-family:sans-serif;
font-family:san-serif;
color:black;
}
......
{% extends "layout.html" %}
{% block content %}
<h1>This is the About Us Page</h1>
<p>Needs some new staionary? this place sells all of it </p>
<p>Needs some new staionary? StationaryFix sells all of it </p>
{% endblock content %}
{% extends "layout.html" %}
{% block content %}
<h1>{{ title }}</h1>
<table class="">
<tr><th>Product</th><th>Quantity</th><th>Price</th><th>Total</th></tr>
{% for key, value in display_cart.items() %}
<tr>
<!-- <td>{{ key }}</td> -->
<td>{{ value["title"] }}</td>
<td>{{ value["quantity"] }}</td>
<td>£{{ value["price"] }}</td>
<td>£{{ (value["quantity"] * value["price"]) }}</td>
<td>
<form action="{{ url_for('delete_item', item_id=key|int) }}" method="POST">
<input class="" type="submit" value="Delete">
</form>
</td>
</tr>
{% endfor %}
</table>
<h3>Total: £{{ total }} </h3>
<h3>Number of items: {{ total_quantity }} </h3>
{% if current_user.is_authenticated %}
<a href = "{{ url_for('checkout') }}">Checkout</a>&nbsp
{% else %}
<a href = "{{ url_for('register') }}">Create an account to checkout</a>
{% endif %}
{% endblock content %}
{% extends "layout.html" %}
{% block content %}
<h1>This is About Us page</h1>
<h1> StationaryFix </h1>
<div class="search-container">
<form method="POST">
<input type="text" placeholder="Search..." name="home">
<button type="submit">Search</button>
</form>
</div>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Sort By
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="Asc">Low-High</a>&nbsp|
<a class="dropdown-item" href="Desc">High-Low</a>&nbsp|
</div>
{% for item in items %}
<div style="float:left; margin:40px; width: 295px; padding: 40px;">
<a href="{{ url_for('item', item_id=item.id) }}">
<img src = "{{ url_for('static', filename='img/' + item.image_file) }}" width="300" height="300"></a>
<!--<a href="{{ url_for('item', item_id=item.id) }}">-->
<p>"{{ item.title }}"</p></a>
<p>Price (£): {{ item.price }}</p>
<p>Description: {{item.description}}</p>
<a class="" href="/add_to_basket/{{item.id}}">Add to Basket</a>&nbsp<br/>
<a class="" href="/add_to_wishlist/{{item.id}}">Add to Wishlist</a>&nbsp
</div>
{% endfor %}
</div>
{% endblock content %}
......@@ -3,10 +3,10 @@
<img src = "{{ url_for('static', filename='img/' + item.image_file) }}" width="300" height="300">
<p>"{{ item.title }}" </p>
<p>{{ item.title }} </p>
<p>Price: {{ item.price }}</p>
<p>Description: {{item.description}}</p>
<a class="" href="/add_to_cart/{{item.id}}">Add to Cart</a>&nbsp|
<a class="" href="/add_to_wishlist/{{item.id}}">Add to Wishlist</a>&nbsp|
<a class="" href="/add_to_basket/{{item.id}}">Add to Basket</a>&nbsp
<a class="" href="/add_to_wishlist/{{item.id}}">Add to Wishlist</a>&nbsp
{% endblock content %}
......@@ -14,17 +14,17 @@
<body>
<div id="header">
<a href="{{ url_for('home') }}" class="header">Home</a>&nbsp|
<a href="{{ url_for('about') }}" class="header">About Us</a>&nbsp|
<a href="{{ url_for('home') }}" class="header">Home</a>&nbsp
<a href="{{ url_for('about') }}" class="header">About Us</a>&nbsp
{% if current_user.is_authenticated %}
Hello, {{ current_user.username }}!
<a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a>&nbsp|
<a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a>&nbsp
{% else %}
Hello, Guest! &nbsp| <a href="{{ url_for('login') }}">Login&nbsp;</a>
or <a href="{{ url_for('register') }}">Register</a>&nbsp|
Hello, Guest! &nbsp <a href="{{ url_for('login') }}">Login&nbsp;</a>
or <a href="{{ url_for('register') }}">Register</a>&nbsp
{% endif %}
<a href="{{ url_for('cart_display') }}" class="header">Cart ({{total_quantity}} items) </a>&nbsp|
<a href="{{ url_for('wishlist_display') }}" class="header">My Wishlist ({{total_quantity_wishlist}} items)</a>&nbsp|
<a href="{{ url_for('basket_display') }}" class="header">Basket ({{total_quantity}}) </a>&nbsp
<a href="{{ url_for('wishlist_display') }}" class="header">My Wishlist ({{total_quantity_wishlist}})</a>&nbsp
</div>
......
{% extends "layout.html" %}
{% block content %}
<h1>Thank You for shopping with us We look forward to seeing you again!</h1>
<h1>Thank You for shopping with StationaryFix We look forward to seeing you again!</h1>
<a class="" href="/home">Home</a>
{% endblock content %}
from flask_admin.contrib.sqla import ModelView
import flask_login as login
from shop.models import User
from shop.models import User, Item
class AdminView(ModelView):
def is_accessible(self):
if login.current_user.is_authenticated:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment