-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
executable file
·141 lines (98 loc) · 3.29 KB
/
views.py
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
from flask import Flask
from flask import Flask, flash, redirect, render_template, request, session, abort
import os
from sqlalchemy.orm import sessionmaker
from tabledef import *
import sqlite3 as sql
import sqlite3
engine = create_engine('sqlite:///tutorial.db', echo=True)
app = Flask(__name__)
@app.route('/')
def home():
if not session.get('logged_in'):
return render_template('login.html')
else:
return render_template('home.html')
@app.route('/login', methods=['POST'])
def do_admin_login():
POST_USERNAME = str(request.form['username'])
POST_PASSWORD = str(request.form['password'])
Session = sessionmaker(bind=engine)
s = Session()
query = s.query(User).filter(User.username.in_([POST_USERNAME]), User.password.in_([POST_PASSWORD]) )
result = query.first()
if result:
session['logged_in'] = True
else:
flash('wrong password!')
return home()
@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():
if request.method == 'POST':
try:
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
pin = request.form['pin']
with sql.connect("database.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO students (name,addr,city,pin) VALUES (?,?,?,?)",(nm,addr,city,pin) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("student.html",msg = msg)
con.close()
@app.route('/list')
def list():
con = sql.connect("database.db")
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("select * from students")
rows = cur.fetchall();
return render_template("list.html",rows = rows)
@app.route('/delete/<int:delete_id>')
def delete_entry(delete_id):
con = sql.connect("database.db")
con.row_factory = sql.Row
cur = con.cursor()
delete_id=int(delete_id)
cur.execute("delete from students where id=?", (delete_id,))
con.commit()
return redirect("http://194.135.89.2:4000/list")
@app.route('/edit/<int:edit_id>')
def edit_entry(edit_id):
con = sql.connect("database.db")
con.row_factory = sql.Row
cur = con.cursor()
edit_id=int(edit_id)
cur.execute("select * from students where id=?", (edit_id,))
#con.commit()
rows = cur.fetchall();
return render_template("edit.html",rows = rows)
@app.route('/update/<int:update_id>', methods=['POST'])
def update_entry(update_id):
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
pin = request.form['pin']
con = sql.connect("database.db")
con.row_factory = sql.Row
cur = con.cursor()
update_id=int(update_id)
cur.execute("update students set name=?, addr=?, city=?, pin=? where id=?", (nm, addr, city, pin, update_id,))
con.commit()
rows = cur.fetchall();
return redirect("http://194.135.89.2:4000/list")
@app.route('/student')
def student():
return render_template("student.html")
@app.route("/logout")
def logout():
session['logged_in'] = False
return home()
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(debug=True,host='0.0.0.0', port=4000)