Commit 32632ac4 authored by Joel's avatar Joel
Browse files

Updated logout and clear cart feature

parent 8fa24dc3
from flask import Flask, request, session, redirect, url_for, abort, render_template, flash
from flask_sqlalchemy import SQLAlchemy
from flask_material import Material
from sqlalchemy import func
import datetime
from sqlalchemy import Table
......@@ -13,7 +14,7 @@ app.config['DEBUG'] = True
app.config['SECRET_KEY'] ='super-secret-key'
app.config['USERNAME'] = 'admin'
app.config['PASSWORD'] = '12345'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///booksmart.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///bookshoppe.db'
db=SQLAlchemy(app)
......@@ -27,7 +28,7 @@ class Login(db.Model):
self.password = password
def __repr__(self):
return '<Entry %r %r %r %r>' % (self.email, self.password,)
return '<Entry %r %r>' % (self.email, self.password,)
class CustOrders(db.Model):
# __tablename__ = 'users'
......@@ -54,19 +55,19 @@ class CurrentCart(db.Model):
# __tablename__ = 'users'
serial = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), db.ForeignKey('login.email'))
isbn = db.Column(db.String(20))
book = db.Column(db.String(20))
quantity = db.Column(db.Integer)
price = db.Column(db.Integer)
def __init__(self, email, datetime, isbn, quantity, price):
def __init__(self, email, datetime, book, quantity, price):
self.email = email
self.isbn = isbn
self.book = book
self.quantity = quantity
self.price = price
def __repr__(self):
return '<Entry %r %r %r %r>' % (
self.email, self.isbn, self.quantity, self.price)
self.email, self.book, self.quantity, self.price)
# Create the table
db.create_all()
......@@ -74,7 +75,7 @@ db.create_all()
def authenticate(e, p):
print(e)
details=Login.query.filter_by(email=e).filter_by(password=p).all()
details= Login.query.filter_by(email=e).filter_by(password=p).all()
print(details)
if(len(details)>0):
return True
......@@ -83,7 +84,25 @@ def authenticate(e, p):
@app.route('/')
def homepage():
return render_template('homepage.html', size=len(data['items']), data= data)
count=0
return render_template('homepage.html', size=len(data['items']), data= data, currentBook=count)
@app.route('/logout')
def logout():
if session['logged_in'] == True:
session['logged_in'] = False
return render_template('homepage.html',size=len(data['items']), data= data)
else:
return render_template('homepage.html',size=len(data['items']), data= data)
@app.route('/sign_up')
def signup():
return render_template('sign_up.html')
@app.route('/show_books')
def show_books():
return render_template('show_books.html', size=len(data['items']), data= data)
@app.route('/book/<int:id>/', methods=['GET','POST'])
def book(id):
......@@ -92,9 +111,9 @@ def book(id):
return render_template('book.html', id = id, data=data, flag=flag)
else:
quantity=request.form.get('comp_select')
isbn = 'ISBN' + str(data['items'][id]["volumeInfo"]["industryIdentifiers"][0]["identifier"])
cart=CurrentCart(email=session['log_email'], datetime=datetime.datetime.now(),isbn=isbn , quantity=quantity, price=data['items'][id]["saleInfo"]["listPrice"]["amount"])
detail = CurrentCart.query.filter_by(isbn=isbn).all()
book = str(data['items'][id]["volumeInfo"]["authors"][0])
cart=CurrentCart(email=session['log_email'], datetime=datetime.datetime.now(),book=book , quantity=quantity, price=data['items'][id]["saleInfo"]["listPrice"]["amount"])
detail = CurrentCart.query.filter_by(book=book).all()
sum=0
for d in detail:
sum+=(d.__dict__["quantity"])
......@@ -112,7 +131,6 @@ def book(id):
def login():
error = None
if request.method == 'POST':
if(authenticate(request.form['username'], request.form['password'])):
session['logged_in'] = True
session['log_email'] = request.form['username']
......@@ -122,12 +140,23 @@ def login():
error='Invalid credentials'
return render_template('login.html', error=error)
@app.route('/cart',methods=['GET'])
@app.route('/cart',methods=['GET','POST'])
def cart():
# lst = CurrentCart.query.filter_by(isbn=isbn).all()
# print(lst)
return render_template('cart.html',size=len(data['items']), data=data)
# lst = CurrentCart.query.filter_by(label=price).all()
# total=0
# for t in lst:
# total=total+t
return render_template('cart.html',size=len(data['items']), data=data, items= CurrentCart.query.all())
@app.route('/clear_cart',methods=['GET'])
def clear_cart():
total=0
cart_items= CurrentCart.query.all()
for cart_item in cart_items:
db.session.delete(cart_item)
db.session.commit()
flash("You just cleared the cart!!")
return render_template('cart.html',size=len(data['items']), data=data, items= CurrentCart.query.all(),total=total)
with open("json/catalog.json") as data_file:
data = json.loads(data_file.read())
......
File added
No preview for this file type
{% extends "layout.html" %}
{% block body%}
<br></br>
{% for i in range((size)//2) %}
<ul class="collection hoverable">
<li class="collection-item ">
<i class="material-icons circle"></i>
<b class="title text-lighten-5">{{ data['items'][i]["volumeInfo"]["title"] }}</b>
<p>{{ data['items'][i]["volumeInfo"]["authors"][0] }}</p>
<a href="#!" class="secondary-content"><i class="fas fa-plus-circle" style="font-size:30px"></i></a>
</li>
</ul>
{% endfor %}
<button class="btn teal right"><i class="fas fa-cart-plus"></i> Proceed to Checkout</button>
<a href="/clear_cart"><button class="btn teal left">Clear Cart</button></a>
<table class="center">
<thead>
<th>Serail No</th>
<th>Username</th>
<th>ISBN</th>
<th>Quantity</th>
<th>Price</th>
</thead>
{% for item in items %}
<tbody>
<tr>
<td>{{ item.serial }} </td>
<td>{{ item.email }} </td>
<td>{{ item.book }} </td>
<td>{{ item.quantity }}</td>
<td>₹ {{ item.price }} </td>
</tr>
</tbody>
{% endfor %}
</table>
<br></br>
<div>
<form><input type="text" name="name" placeholder="Name here..."><input type="text" name="phone" placeholder="Phone Number here..."></form><br>
<button class="btn teal right">₹ {{ bill }}<i class="fas fa-cart-plus"></i></button>
</div>
{% endblock %}
\ No newline at end of file
......@@ -8,8 +8,8 @@
}
</style>
<div class="row container">
{% with currentBook=1 %}
<center class="col s3 h-100 waves-effect" style="margin-top: 30%;"><button class="btn waves-effect" id="prevBtn" style="border-radius: 50%" onclick="prev()"><i class="fas fa-caret-left"></i></button></center>
{% with currentBook=0 %}
<center class="col s3 h-100" style="margin-top: 30%;"><button class="btn waves-effect" id="prevBtn" style="border-radius: 50%" onclick="{{currentBook}}"><i class="fas fa-caret-left"></i></button></center>
<div class="col s6 m6">
<div class="card-panel card-adjust">
<center><img class="card-image hoverable" src="{{ data['items'][currentBook]['volumeInfo']['imageLinks']['thumbnail'] }}" alt="book"></center>
......@@ -22,7 +22,7 @@
</blockquote>
</div>
</div>
<center class="col s3 h-100 waves-effect" style="margin-top: 30%"><button class="btn waves-effect" id="nextBtn" style="border-radius: 50%" onclick=""><i class="fas fa-caret-right"></i></button></center>
<center class="col s3 h-100" style="margin-top: 30%"><button class="btn waves-effect" id="nextBtn" style="border-radius: 50%" onclick="show()"><i class="fas fa-caret-right"></i></button></center>
{% endwith %}
</div>
{% endblock %}
\ No newline at end of file
......@@ -19,60 +19,22 @@
.item{
margin-bottom: 5%;
}
@font-face {
font-family: Montserrat-Regular;
src: url(/Montserrat-Regular.otf);
}
body{
font-family: Montserrat-Regular;
}
.badge {
display: inline-block;
padding: 0.25em 0.4em;
font-size: 75%;
font-weight: 700;
line-height: 1;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: 0.25rem;
}
.badge:empty {
display: none;
}
.btn .badge {
position: relative;
top: -1px;
}
.badge-pill {
padding-right: 0.6em;
padding-left: 0.6em;
border-radius: 10rem;
}
.badge-primary {
color: #fff;
background-color: #007bff;
}
</style>
</head>
<body>
<nav class="nav-wrapper teal waves-effect">
<div class="container">
<a class="brand-logo text-uppercase" href="/">BOOK SHOPPE</a>
<a class="brand-logo text-uppercase" href="/show_books">BOOK SHOPPE</a>
{% if not session.logged_in %}
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="#"><i class="fas fa-user-plus waves-effect"></i> Sign Up</a></li>
<li><a href="/login"><i class="fas fa-signin-alt waves-effect"></i> Login</a></li>
<li><a href="/login"><i class="fas fa-sign-in-alt waves-effect"></i> Login</a></li>
</ul>
{% else %}
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="/cart"><i class="fas fa-shopping-cart waves-effect"></i> Cart</a></li>
<li><a href="#"><i class="fas fa-user waves-effect"></i> User</a></li>
<li><a href="/logout"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
</ul>
{% endif %}
</div>
......
......@@ -12,13 +12,13 @@
<form>
<div class="input-group login-userinput">
<span class="input-group-addon"></span>
<input id="txtUser" type="text" class="form-control" name="username" placeholder="Username">
<input class="form-control" type="text" name="username" placeholder="Username">
</div>
<div class="input-group">
<span class="input-group-addon"></span>
<input id="txtPassword" type="password" class="form-control" name="password" placeholder="Password">
<input class="form-control" type="password" name="password" placeholder="Password">
</div><br>
<center><input class="btn btn-small" type="submit" value="SIGN IN"></input></center>
<center><input class="btn btn-small" type="submit" value="Sign In"></input></center>
</form>
</div>
</div>
......@@ -30,7 +30,6 @@
background: white no-repeat;
}
.login-form{width:390px;}
/*.login-title{font-family: 'Exo', sans-serif;text-align:center;color: white;}*/
.login-userinput{margin-bottom: 10px;}
.login-button{margin-top:10px;}
.login-options{margin-bottom:0px;}
......
{% extends "layout.html" %}
{% block body %}
<h4 style="margin-bottom: 5%;text-align: center"><b>SHOW BOOKS</b></h4>
<style>
.card-adjust{
height: 420px; /* Your height here */
overflow: hidden;
}
</style>
<div class="row container">
{% for currentBook in range(size) %}
<div class="col s6 m6 ">
<div class="card-panel card-adjust">
<center><img class="card-image hoverable" src="{{ data['items'][currentBook]['volumeInfo']['imageLinks']['thumbnail'] }}" alt="book"></center>
<blockquote class="card-body left" >
<h5 class="card-title">
<a href="book/{{ currentBook }}"> {{ data['items'][currentBook]["volumeInfo"]["title"] }}</a>
</h5>
<p href="book/{{ currentBook }}"><i class="fas fa-user"></i> {{ data['items'][currentBook]["volumeInfo"]["authors"][0] }}</p>
<p href="book/{{ currentBook }}"><i class="fas fa-tag"></i> {{ data['items'][currentBook]["volumeInfo"]["categories"][0]}}</p>
</blockquote>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
\ No newline at end of file
{% extends "layout.html" %}
{% block body %}
<br></br><br></br>
<center>
<h4 class="text-center"><b>CREATE YOUR ACCOUNT</b></h4>
</center>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}{% endif %}
<form action="{{ url_for('login') }}" method="post">
<div class="container login-form">
<div class="panel panel-default">
<div class="panel-body">
<form>
<div class="input-group login-userinput">
<span class="input-group-addon"></span>
<input class="form-control" type="text" name="uname" placeholder="Write your Username...">
</div>
<div class="input-group">
<span class="input-group-addon"></span>
<input class="form-control" type="password" name="pwd" placeholder="Write your Password...">
</div><br>
<center><input class="btn btn-small" type="submit" value="Sign In"></input></center>
</form>
</div>
</div>
</div>
</form>
<style>
body {
background: white no-repeat;
}
.login-form{width:390px;}
.login-userinput{margin-bottom: 10px;}
.login-button{margin-top:10px;}
.login-options{margin-bottom:0px;}
.login-forgot{float: right;}
</style>
{% endblock %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment