-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
169 lines (151 loc) · 5.4 KB
/
Copy pathapp.py
File metadata and controls
169 lines (151 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from ast import Not
from curses import flash
from datetime import date
import os
import re
from urllib import request
from xml.etree.ElementTree import tostring
from flask import Flask, redirect, render_template , flash , session ,url_for,request,g,url_for
import sqlite3
import os
app = Flask(__name__)
app.secret_key = os.urandom(24);
conn = sqlite3.connect("bookshop.db",check_same_thread=False)
currentlocation = os.path.dirname(os.path.abspath(__file__))
@app.route('/',methods=["POST","GET"])
def index():
cur = conn.cursor()
query = "SELECT * FROM book;"
cur.execute(query)
data = cur.fetchall()
return render_template("/index.html",book = data)
@app.route('/login',methods=['POST','GET'])
def login():
session.clear()
if request.method == "GET":
return render_template("/login.html")
else:
cur = conn.cursor()
username = request.form['username']
password = request.form['userpass']
queryexist = "SELECT * FROM users WHERE username = '{a}' AND userpass = '{b}';".format(a=username,b=password)
if not username:
flash("enter Username", "403")
return render_template("/singup.html")
elif not password:
flash("Enter password", "info")
return render_template("/login.html")
cur.execute(queryexist)
data = cur.fetchone()
if not data:
flash("User or password not found")
return render_template("/login.html")
if data:
session['user']=username
conn.close
return redirect("/")
@app.route("/singup",methods=["POST","GET"])
def singup():
if request.method == "GET":
session.clear()
return render_template("/singup.html")
else:
username = request.form['username']
password = request.form['userpass']
confirmpassword = request.form['conuserpass']
cur = conn.cursor()
queryexist = "SELECT * FROM users WHERE username = '{a}';".format(a=username)
queryadd = "INSERT INTO users(username,userpass) VALUES('{a}','{b}');".format(a=username,b=password)
if not username:
flash("enter Username", "403")
return render_template("/singup.html")
elif not password:
flash("Enter password", "info")
return render_template("/singup.html")
elif not confirmpassword:
flash("Enter password confirmation", "info")
return render_template("/singup.html")
elif confirmpassword != password:
flash("the password not be same", "info")
return render_template("/singup.html")
cur.execute(queryexist)
data = cur.fetchone()
if data:
flash("the user already exist")
return render_template("/singup.html")
try:
cur.execute(queryadd)
except:
flash("ERROR")
return render_template("/singup.html")
conn.commit()
session['user']=username
return redirect("/")
@app.route('/addbook',methods=["POST","GET"])
def addbook():
if request.method == "GET":
return render_template("/addbook.html")
else:
bookname = request.form['bookname']
author = request.form['author']
description= request.form['description']
book_url = request.form['url']
cur = conn.cursor()
if not bookname:
flash("enter name of book")
return render_template("/addbook.html")
elif not author:
flash("enter name of author")
return render_template("/addbook.html")
elif not author:
flash("enter name of author")
return render_template("/addbook.html")
elif session.get('user') is None:
flash("FIRST LOGIN")
return render_template("/addbook.html")
elif session.get('user') is not None:
user = session.get('user')
queryadd = 'INSERT INTO book(name,author,description,image,user) VALUES("{a}","{b}","{c}","{d}","{e}");'.format(a=bookname,b=author,c=description,d=book_url,e=user)
try:
cur.execute(queryadd)
except:
flash("ERROR")
return render_template("/addbook.html")
conn.commit()
return render_template("/tnks.html")
@app.route('/tnks')
def tnks():
return render_template("/tnks.html")
@app.route('/info/<id>',methods=["POST","GET"])
def info(id):
cur = conn.cursor()
queryadd = "SELECT * FROM book WHERE id = '{a}';".format(a = id)
cur.execute(queryadd)
data = cur.fetchone()
conn.commit()
return render_template("/info.html",book=data)
@app.route('/aboutus')
def aboutus():
return render_template("/aboutus.html")
@app.route('/history')
def history():
if session.get('user') is None:
flash("FIRST LOGIN")
return redirect("/login")
user = session.get('user')
cur = conn.cursor()
queryadd = "SELECT * FROM book WHERE user = '{a}';".format(a = user)
cur.execute(queryadd)
data = cur.fetchall()
conn.commit()
return render_template("/history.html",data=data)
@app.route('/<id>',methods=["POST","GET"])
def revbook(id):
cur = conn.cursor()
queryadd = "DELETE FROM book WHERE id = '{a}';".format(a = id)
cur.execute(queryadd)
data = cur.fetchall()
conn.commit()
return redirect("/")
if __name__ == '__main__':
app.run()