Skip to content
Snippets Groups Projects
Commit 33b4c49b authored by Michael Drury's avatar Michael Drury
Browse files

adding to git

parents
Branches
No related tags found
No related merge requests found
Showing
with 436 additions and 0 deletions
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/coursework.iml" filepath="$PROJECT_DIR$/.idea/coursework.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
File added
run.py 0 → 100644
from shop import app, db
from shop.databases import User
from shop.databases import product_table
if __name__ == '__main__':
with app.app_context():
db.create_all()
if User.query.filter_by(username='michael').first() is None:
User.register('michael', 'michael1')
if User.query.filter_by(username='connor').first() is None:
User.register('connor', 'connor1')
if User.query.filter_by(username='alex').first() is None:
User.register('alex', 'alex1')
if product_table.query.filter_by(product_name = 'Fox 36').first() is None:
product_table.new_product('Fox 36', 1139.00, 0.7, 'fox36.jpg', 'fox', 'fork', 'A high-performance mountain bike suspension fork with 36mm stanchions')
if product_table.query.filter_by(product_name = 'Rockshox Pike').first() is None:
product_table.new_product('Rockshox Pike', 565.27, 0.5, 'pike.jpg', 'rockshox', 'fork', 'A durable and adjustable MTB suspension fork with 35mm stanchions')
if product_table.query.filter_by(product_name = 'Rockshox Yari').first() is None:
product_table.new_product('Rockshox Yari', 550.00, 0.5, 'yari.jpg', 'rockshox', 'fork', 'A robust and affordable MTB suspension fork with 35mm stanchions')
if product_table.query.filter_by(product_name = 'Fox 34').first() is None:
product_table.new_product('Fox 34', 749.00, 0.5, 'fox34.jpg', 'fox', 'fork', 'A versatile mountain bike suspension fork with 34mm stanchions')
if product_table.query.filter_by(product_name = 'SR Suntor').first() is None:
product_table.new_product('SR Suntor', 210.50, 0.5, 'suntor.jpg', 'suntor', 'fork', 'An entry-level mountain bike suspension fork with decent performance')
if product_table.query.filter_by(product_name='Kona Process 153').first() is None:
product_table.new_product('Kona Process 153', 1749.00, 2.3, 'process_153_frame.jpg', 'vitus', 'frame','A strong and agile aluminum MTB frame for trail riding' )
if product_table.query.filter_by(product_name='Santa Cruz Hightower').first() is None:
product_table.new_product('Santa Cruz Hightower', 3599.00, 2.2, 'SC_Hightower_frame.png', 'santa cruz', 'frame', 'A top-tier carbon fiber frame for all-mountain riding')
if product_table.query.filter_by(product_name='Yeti SB165').first() is None:
product_table.new_product('Yeti SB165', 2933.24, 2.1, 'SB165_frame.jpg', 'yeti', 'frame', 'A high-end carbon fiber frame for aggressive enduro riding')
app.run(debug = True)
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
app = Flask(__name__)
app.config['SECRET_KEY'] = 'password'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
lm = LoginManager(app)
lm.login_view = 'login'
app.app_context().push()
from . import routes
\ No newline at end of file
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from . import db
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(16), index=True, unique=True)
password_hash = db.Column(db.String(64))
cart = db.Column(db.String)
wishlist = db.Column(db.String)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def verify_password(self, password):
return check_password_hash(self.password_hash, password)
def new_cart(self):
self.cart = ''
def new_wishlist(self):
self.wishlist = ''
@staticmethod
def register(username, password):
user = User(username=username)
user.set_password(password)
user.new_cart()
user.new_wishlist()
db.session.add(user)
db.session.commit()
return user
def __repr__(self):
return '<User {0}>'.format(self.username)
class product_table(db.Model):
__tablename__ = 'products'
product_id = db.Column(db.Integer, primary_key=True)
product_name = db.Column(db.String)
product_price = db.Column(db.Float)
product_footprint = db.Column(db.Float)
product_image = db.Column(db.String)
product_brand = db.Column(db.String)
product_type = db.Column(db.String)
product_description = db.Column(db.String)
@staticmethod
def new_product(name_in, price_in, footprint_in, image_in, brand_in, type_in, description_in):
db.session.add(product_table(product_name = name_in, product_price = price_in, product_footprint = footprint_in,
product_image = image_in, product_brand = brand_in, product_type = type_in,
product_description = description_in))
db.session.commit()
class review_table(db.Model):
__tablename__ = 'reviews'
review_id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer)
review = db.Column(db.String)
review_author = db.Column(db.String)
@staticmethod
def new_review(review_in, product_id_in, author_in):
db.session.add(review_table(review = review_in, product_id = product_id_in, review_author = author_in))
db.session.commit()
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, IntegerField
from wtforms.validators import input_required, Length, EqualTo, NumberRange, Regexp
import datetime
class LoginForm(FlaskForm):
username = StringField('Username', validators=[input_required(), Length(1, 16)])
password = PasswordField('Password', validators=[input_required()])
remember_me = BooleanField('Remember me')
submit = SubmitField('Submit')
class SignupForm(FlaskForm):
username = StringField('Username', validators=[input_required(), Length(1, 16)])
password = PasswordField('Password', validators=[input_required()])
verifyPassword = PasswordField('Verify password', validators=[input_required(), EqualTo('password', message='Passwords must match')])
submit = SubmitField('Submit')
class orderByForm(FlaskForm):
order = SelectField(label = 'Order by', choices=['Default', 'Price Ascending', 'Price Descending'])
submit = SubmitField('Submit')
class addToCart(FlaskForm):
choice = SelectField(label='Add to', choices=['Cart', 'Wishlist'])
submit = SubmitField('Confirm')
class removeFormCart(FlaskForm):
item_id_remove = StringField('Enter item id of item that you wish to remove')
clear_all = BooleanField('Clear all?')
submit = SubmitField('Confirm')
class removeFormWishlist(FlaskForm):
item_id_remove = StringField('Enter item id of item that you wish to remove')
clear_all = BooleanField('Clear all?')
submit = SubmitField('Confirm')
class checkoutForm(FlaskForm):
security_pin = StringField('Security Pin', validators=[input_required(), Regexp(regex='^[+-]?[0-9]+$', message= 'only integers allowed'), Length(min=3, max=3)])
card_num = StringField('Card Number', validators=[input_required(), Regexp(regex='^[+-]?[0-9]+$', message = 'only integers allowed'), Length(min=16, max=16)])
month = IntegerField('Expiry Date', validators=[input_required(), NumberRange(min = 1, max = 12)])
year = IntegerField(validators=[input_required(), NumberRange(min = datetime.date.today().year, max = 9999)])
submit = SubmitField('Confirm')
class typeViewForm(FlaskForm):
type = SelectField(label='product type', choices=['All', 'Fork', 'Frame'])
submit1 = SubmitField('Submit')
class reviewForm(FlaskForm):
review = StringField('Enter a review:', validators=[input_required(), Length(min=1, max=300)])
submit1 = SubmitField('Submit')
from flask import render_template, redirect, url_for, request
from flask_login import login_required, login_user, logout_user, current_user
from .databases import User
from .forms import LoginForm
from . import app, lm
from .databases import product_table
from .databases import review_table
from .forms import orderByForm
from .forms import SignupForm
from .forms import addToCart
from . import db
from .forms import removeFormCart
from .forms import removeFormWishlist
from .forms import checkoutForm
from .forms import typeViewForm
from .forms import reviewForm
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.verify_password(form.password.data):
return redirect(url_for('login', **request.args))
login_user(user, form.remember_me.data)
return redirect(url_for('home'))
return render_template('login.html', form=form)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if form.validate_on_submit():
password_in = str(form.password.data)
username_in = str(form.username.data)
if User.query.filter_by(username=username_in).first() is None:
User.register(username_in, password_in)
return redirect(url_for('login'))
return render_template('signup.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('home'))
@app.route('/')
def home():
return render_template('home.html')
@app.route('/shopping', methods=['GET', 'POST'])
def shopping():
products_pass = product_table.query.all()
order_by_form = orderByForm()
type_form = typeViewForm()
if order_by_form.validate_on_submit() and order_by_form.is_submitted():
order_show = str(order_by_form.order.data)
if order_show == 'Price Ascending':
products_pass = product_table.query.order_by(product_table.product_price).all()
elif order_show == 'Price Descending':
products_pass = product_table.query.order_by(product_table.product_price.desc()).all()
elif order_show == 'Default':
products_pass = product_table.query.all()
elif type_form.validate_on_submit() and type_form.is_submitted():
type_in = type_form.type.data
if type_in == 'All':
product_table.query.all()
elif type_in == 'Fork':
products_pass = product_table.query.filter_by(product_type = 'fork')
elif type_in == 'Frame':
products_pass = product_table.query.filter_by(product_type = 'frame')
# controls the add to cart and add to wishlist buttons when pressed
elif request.method == 'POST':
try:
item_to_add_cart = request.form['item_id_cart']
for user_row in db.session.query(User).filter_by(id=current_user.get_id()):
user_row.cart = user_row.cart + item_to_add_cart + ' '
db.session.commit()
except:
item_to_add_wishlist = request.form['item_id_wishlist']
for user_row in db.session.query(User).filter_by(id=current_user.get_id()):
user_row.wishlist = user_row.wishlist + item_to_add_wishlist + ' '
db.session.commit()
return render_template('shopping.html', product_table = products_pass, order_by_form = order_by_form, type_form = type_form)
@app.route('/cart', methods=['GET', 'POST'])
@login_required
def cart():
cart_string = current_user.cart
cart_id_list = cart_string.split()
cart_list = []
for item_id in cart_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
cart_list.append(item)
remove_form = removeFormCart()
if remove_form.validate_on_submit() and remove_form.validate():
remove_item_id = remove_form.item_id_remove.data
clear_all = remove_form.clear_all.data
if remove_item_id in cart_id_list:
cart_id_list.remove(remove_item_id)
new_cart_string = ''
for item_id in cart_id_list:
new_cart_string = new_cart_string + item_id + ' '
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.cart = new_cart_string
db.session.commit()
if clear_all:
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.cart = ''
db.session.commit()
total_price = get_total_cart_price()
return render_template('cart.html', cart_list = cart_list, cart_size = len(cart_list), total_price = total_price, remove_form = remove_form)
@app.route('/wishlist', methods=['GET', 'POST'])
@login_required
def wishlist():
wishlist_string = current_user.wishlist
wishlist_id_list = wishlist_string.split()
wishlist_list = []
for item_id in wishlist_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
wishlist_list.append(item)
total_price = 0
for item in wishlist_list:
total_price += item.product_price
remove_form = removeFormWishlist()
if remove_form.validate_on_submit():
remove_item_id = remove_form.item_id_remove.data
clear_all = remove_form.clear_all.data
if remove_item_id in wishlist_id_list:
wishlist_id_list.remove(remove_item_id)
new_wishlist_string = ''
for item_id in wishlist_id_list:
new_wishlist_string = new_wishlist_string + item_id + ' '
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.wishlist = new_wishlist_string
db.session.commit()
if clear_all:
for c in db.session.query(User).filter_by(id=current_user.get_id()):
c.wishlist = ''
db.session.commit()
return render_template('wishlist.html', wishlist_list = wishlist_list, wishlist_size = len(wishlist_list), total_price = total_price, remove_form = remove_form)
@app.route('/item', methods=['GET', 'POST'])
def item_page():
id = request.args.get('id', default='1', type=int)
all_products = product_table.query.all()
for product in all_products:
if product.product_id == id:
product_in = product
cartForm = addToCart()
if cartForm.validate_on_submit():
for c in db.session.query(User).filter_by(id = current_user.get_id()):
if cartForm.choice.data == 'Cart':
c.cart = c.cart + str(product_in.product_id) + ' '
else:
c.wishlist = c.wishlist + str(product_in.product_id) + ' '
db.session.commit()
review_form = reviewForm()
if review_form.validate_on_submit():
review_in = review_form.review.data
for c in db.session.query(User).filter_by(id=current_user.get_id()):
current_user_username = c.username
review_table.new_review(review_in=review_in, product_id_in=product_in.product_id, author_in = current_user_username)
reviews = review_table.query.filter_by(product_id=product_in.product_id)
return render_template('item_page.html', product = product_in, cartForm = cartForm, reviewForm = review_form, reviews = reviews)
@app.route('/checkout', methods=['GET', 'POST'])
@login_required
def checkout():
form = checkoutForm()
# checks for validation of checkout form and if validated clears the cart for the current user and returns to the thank you page
if form.validate_on_submit():
for c in db.session.query(User).filter_by(id = current_user.get_id()):
c.cart = ''
db.session.commit()
return redirect(url_for('thank_you'))
# gets the total price of items using the get_total_price function
total_price = get_total_cart_price()
return render_template('checkout.html', total_price = total_price, form = form)
def get_total_cart_price():
cart_string = current_user.cart
cart_id_list = cart_string.split()
cart_list = []
# gets all of the items in the cart and adds them to a list
for item_id in cart_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
cart_list.append(item)
# loops through the previously created cart list and adds the price of each item to the total price to be passed in
total_price = 0
for item in cart_list:
total_price += item.product_price
return total_price
@app.route('/thank_you', methods=['GET', 'POST'])
@login_required
def thank_you():
return render_template('thank_you.html')
@lm.user_loader
def load_user(id):
return User.query.get(int(id))
shop/static/images/SB165_frame.jpg

252 KiB

shop/static/images/SC_Hightower_frame.png

145 KiB

shop/static/images/backrgoud_image.png

106 KiB

shop/static/images/cart.png

7.53 KiB

shop/static/images/cog.png

46.2 KiB

shop/static/images/dark_logo.png

35.3 KiB

shop/static/images/diments_disks.png

37.1 KiB

shop/static/images/footer_mtb.png

53.3 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment