Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
817 additions
and
0 deletions.
There are no files selected for viewing
248
app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
#coding:utf-8 | ||
import os | ||
from datetime import date | ||
from flask import Flask,request, session, redirect, render_template,send_from_directory | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
app = Flask(__name__) | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.db') | ||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
app.config['SECRET_KEY'] = "ruz@2021" | ||
db = SQLAlchemy(app) | ||
|
||
|
||
class User(db.Model): | ||
id = db.Column(db.Integer, unique=True, primary_key=True) | ||
username=db.Column(db.String(255)) | ||
password= db.Column(db.String(255)) | ||
|
||
def __init__(self,username,password): | ||
self.username=username | ||
self.password=password | ||
|
||
def __repr__(self): | ||
return '<User %r>' % self.id | ||
|
||
class Book(db.Model): | ||
isbn = db.Column(db.Integer, unique=True, primary_key=True,autoincrement=False) | ||
name=db.Column(db.String(255)) | ||
author= db.Column(db.Text) | ||
pub_date= db.Column(db.Date) | ||
description =db.Column(db.Text) | ||
cover =db.Column(db.Text) | ||
trade_price =db.Column(db.Integer) | ||
retail_price =db.Column(db.Integer) | ||
stock = db.Column(db.Integer) | ||
|
||
def __init__(self,isbn,name,author,pub_date,description,cover,trade_price,retail_price,stock ): | ||
self.isbn = isbn | ||
self.name = name | ||
self.author = author | ||
self.pub_date = pub_date | ||
self.description = description | ||
self.cover = cover | ||
self.trade_price = trade_price | ||
self.retail_price = retail_price | ||
self.stock = stock | ||
|
||
def __repr__(self): | ||
return '<Book %r>' % self.isbn | ||
|
||
class Order(db.Model): | ||
id = db.Column(db.Integer, unique=True, primary_key=True,autoincrement=True) | ||
uid= db.Column(db.Integer,db.ForeignKey('user.id')) | ||
total = db.Column(db.Integer) | ||
postage = db.Column(db.Integer) | ||
|
||
def __init__(self,uid,total,postage): | ||
self.uid=uid | ||
self.total=total | ||
self.postage = postage | ||
|
||
def __repr__(self): | ||
return '<Order %r>' % self.id | ||
|
||
class Cart(db.Model): | ||
id = db.Column(db.Integer, unique=True, primary_key=True,autoincrement=True) | ||
uid= db.Column(db.Integer,db.ForeignKey('user.id')) | ||
bid= db.Column(db.Integer,db.ForeignKey('book.isbn')) | ||
num = db.Column(db.Integer) | ||
book = db.relationship('Book') | ||
|
||
def __init__(self,uid,bid,num): | ||
self.uid = uid | ||
self.bid = bid | ||
self.num = num | ||
|
||
def __repr__(self): | ||
return '<Cart %r>' % self.id | ||
|
||
DIRECTORY_PATH = 'media/' | ||
|
||
#Home Page | ||
@app.route('/') | ||
@app.route('/index', methods = ['GET']) | ||
def index(): | ||
if session.get('admin'): | ||
return render_template('home.html') | ||
else: | ||
res=Book.query.filter( Book.stock > 0 ).all() | ||
return render_template('index.html',res=res) | ||
|
||
@app.route('/mycart') | ||
def mycart(): | ||
res = Cart.query.filter_by(uid = session.get('uid')) | ||
total = sum([ i.book.retail_price*i.num for i in res ]) if res else 0 | ||
return render_template('cart.html', res=res,total = total ) | ||
|
||
|
||
@app.route('/add', methods = ['GET','POST']) | ||
def addstock(): | ||
if request.method == "POST": | ||
isbn = request.form['isbn'] | ||
name = request.form['name'] | ||
author = request.form['author'] | ||
pub_date = request.form['pub_date'] | ||
pub_date = date( int(pub_date.split('-')[0]),int(pub_date.split('-')[1]),int(pub_date.split('-')[2]) ) | ||
retail_price = request.form['retail_price'] | ||
trade_price = request.form['trade_price'] | ||
description = request.form['description'] | ||
quantity = request.form['quantity'] | ||
f = request.files['cover'] | ||
book = Book.query.filter_by(isbn = int(isbn)).first() | ||
if book: | ||
book.name = name | ||
book.author = author | ||
book.pub_date = pub_date | ||
book.retail_price = int(retail_price) | ||
book.trade_price = int(trade_price) | ||
book.description = description | ||
book.quantity = int(quantity) | ||
book.cover = '/static/img/'+f.filename | ||
upload_path = os.path.join(basedir,'/static/img/',f.filename) | ||
f.save(upload_path) | ||
else: | ||
cover = '/static/img/'+f.filename | ||
upload_path = basedir + cover | ||
f.save(upload_path) | ||
b = Book(isbn=int(isbn), name=name, author=author, pub_date=pub_date, description=description, cover=cover, trade_price=int(trade_price), retail_price=int(retail_price), stock = int(quantity) ) | ||
db.session.add(b) | ||
db.session.commit() | ||
return redirect('/stock') | ||
else: | ||
return render_template('add.html') | ||
|
||
|
||
@app.route('/stock') | ||
def stock(): | ||
books=Book.query.all() | ||
return render_template('stock.html',books=books) | ||
|
||
|
||
@app.route('/clearall') | ||
def doclear(): | ||
uid = session.get('uid') | ||
Cart.query.filter_by(uid=uid).delete() | ||
db.session.commit() | ||
return redirect('/index') | ||
|
||
@app.route('/gocheck') | ||
def docheck(): | ||
res = [] | ||
total_price = 0 | ||
postage = 0 | ||
shortage = [] | ||
items = Cart.query.filter_by(uid = session.get('uid')) | ||
for item in items: | ||
if item.num <= item.book.stock: | ||
res.append(item) | ||
postage += item.num | ||
total_price += item.num * item.book.retail_price | ||
else: | ||
shortage.append(item) | ||
postage = 3 if postage == 1 else postage+2 | ||
return render_template('check.html',res=res,total_price=total_price,postage=postage) | ||
|
||
|
||
@app.route('/pay') | ||
def dopay(): | ||
total_price = 0 | ||
postage = 0 | ||
items = Cart.query.filter_by(uid = session.get('uid')) | ||
for item in items: | ||
if item.num <= item.book.stock: | ||
book = Book.query.get(item.book.isbn) | ||
book.stock -= item.num | ||
db.session.delete(item) | ||
postage += item.num | ||
total_price += item.num * item.book.retail_price | ||
total = total_price+3 if postage == 1 else total_price + postage + 2 | ||
db.session.commit() | ||
return render_template('pay.html',total=total) | ||
|
||
@app.route('/delete', methods = ['GET']) | ||
def dodelete(): | ||
c = Cart.query.filter_by( id = int(request.args.get('cid')) ).first() | ||
db.session.delete(c) | ||
db.session.commit() | ||
return redirect('/mycart') | ||
|
||
|
||
|
||
@app.route('/addtocart',methods = ['GET']) | ||
def add(): | ||
isbn = int(request.args.get('isbn')) | ||
s = Cart.query.filter_by(uid=session.get('uid'), bid = isbn).first() | ||
if s: | ||
s.num += 1 | ||
db.session.commit() | ||
else: | ||
c = Cart(uid=session.get('uid'), bid = isbn, num = 1) | ||
db.session.add(c) | ||
db.session.commit() | ||
return redirect('/mycart') | ||
|
||
@app.route('/login',methods = ['GET', 'POST']) | ||
def login(): | ||
if request.method == 'POST': | ||
uname=request.form['username'] | ||
pwd=request.form['password'] | ||
user = User.query.filter_by(username=uname,password=pwd).first() | ||
if user: | ||
session['logged_in'] = True | ||
session['admin'] = True if uname == 'admin' else False | ||
session['username'] = uname | ||
session['uid'] = user.id | ||
return redirect('/index') | ||
else: | ||
return render_template('login.html',info="Incorrect username or password !") | ||
else: | ||
return render_template('login.html') | ||
|
||
|
||
#Sign out of session | ||
@app.route('/signout') | ||
def signout(): | ||
session.pop('username', None) | ||
session.pop('admin', None) | ||
session.pop('logged_in', None) | ||
return redirect('/login') | ||
|
||
|
||
@app.before_request | ||
def before_request(): | ||
if request.path in ['/login','/signout','/reg']: | ||
return None | ||
if session.get('logged_in'): | ||
return None | ||
return redirect('/login') | ||
|
||
@app.teardown_request | ||
def teardown_request(exception): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host='0.0.0.0', port=5000,debug=False ) | ||
|
BIN
+48 KB
data.db
Binary file not shown.
BIN
+19.5 KB
static/img/anne.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+47.1 KB
static/img/bad.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+30.2 KB
static/img/bitcoin.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+15.3 KB
static/img/game.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+49.7 KB
static/img/hp.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+44.1 KB
static/img/love.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+42.4 KB
static/img/meat.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+11.9 KB
static/img/python.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+21.1 KB
static/img/steve.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN
+13.1 KB
static/img/time.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Book Shop</title> | ||
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"> | ||
<link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-slider/9.9.1/css/bootstrap-slider.min.css" rel="stylesheet"> | ||
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> | ||
</head> | ||
<body> | ||
<nav class="navbar navbar-expand-lg bg-primary navbar-dark" role="navigation"> | ||
<a class = "navbar-brand ml-md-4" href="#" style="font-size: 22px;"> | ||
Book Shop | ||
</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse ml-md-4" id="navbarResponsive"> | ||
<ul class="navbar-nav"> | ||
<li class="nav-item mr-3 active"> | ||
<a class="nav-link" href="/">Home | ||
</a> | ||
</li> | ||
</ul> | ||
|
||
<ul class="navbar-nav ml-md-auto"> | ||
<li class="nav-item"> | ||
<a class="text-light mr-3" href="#">Hello, {{session.username}} </a> | ||
<a href="/signout" class="text-light" target="_blank"> | ||
Sign Out | ||
</a> | ||
|
||
</li> | ||
</ul> | ||
</div> | ||
</nav> | ||
|
||
<div class="container-fluid"> | ||
<div class="row"> | ||
|
||
<div class="col-md-6 offset-3"> | ||
<br> | ||
<br> | ||
<h3>Add Stock:</h3> | ||
<hr> | ||
<form action="/add" method="post" enctype="multipart/form-data"> | ||
<div class="form-group"> | ||
<label>Name:</label> | ||
<input name="name" class="form-control" type="text" maxlength="200" required> | ||
</div> | ||
<div class="form-group"> | ||
<label>Author:</label> | ||
<input name="author" class="form-control" type="text" maxlength="200" required> | ||
</div> | ||
<div class="form-group"> | ||
<label>Publication Date:</label> | ||
<input name="pub_date" class="form-control" type="date" required> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label>Cover:</label> | ||
<input name="cover" class="form-control" type="file" required> | ||
</div> | ||
<div class="form-group"> | ||
<label>ISBN-13:</label> | ||
<input name="isbn" class="form-control" type="text" maxlength="13" required> | ||
</div> | ||
<div class="form-group"> | ||
<label>Trade Price:</label>   | ||
<input id="ex1" name="trade_price" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="0"/>   | ||
<span id="ex1_val"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label>Retail Price:</label>   | ||
<input id="ex2" name="retail_price" data-slider-id='ex2Slider' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="0"/>   | ||
<span id="ex2_val"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label>Quantity:</label>   | ||
<input id="ex3" name="quantity" data-slider-id='ex3Slider' type="text" data-slider-min="0" data-slider-max="20" data-slider-step="1" data-slider-value="0"/>   | ||
<span id="ex3_val"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label>Description:</label> | ||
<textarea class="form-control" name="description" cols="50" rows="10" ></textarea> | ||
</div> | ||
<button class="btn btn-primary float-right" type="submit" > Submit </button> | ||
|
||
</form> | ||
</div> | ||
</div> | ||
<br> | ||
<hr> | ||
</div> | ||
<div class="row"> | ||
<br> | ||
<div class="col text-center">© 2021 Book Shop</div> | ||
<br> | ||
</div> | ||
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-slider/9.9.1/bootstrap-slider.min.js"></script> | ||
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" crossorigin="anonymous"></script> | ||
<script> | ||
$('#ex1').slider({ | ||
formatter: function(value) { | ||
$('#ex1_val').html( '£'+value); | ||
} | ||
}); | ||
$('#ex2').slider({ | ||
formatter: function(value) { | ||
$('#ex2_val').html( '£'+value); | ||
} | ||
}); | ||
$('#ex3').slider({ | ||
formatter: function(value) { | ||
$('#ex3_val').html(value); | ||
} | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.