-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.py
146 lines (117 loc) · 3.89 KB
/
app.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
142
143
144
145
146
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from sqlite3 import connect
import json
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:https:///student_results.db"
db = SQLAlchemy(app)
CORS(app)
# schema definition
class StudentLoginModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
password = db.Column(db.String(10))
student = db.Column(db.String(10))
faculty = db.Column(db.String(10))
def __init__(self, id, password, student, faculty):
self.id = id
self.password = password
self.student = student
self.faculty = faculty
class StoreModel(db.Model):
rno = db.Column(db.Integer, primary_key=True)
phy = db.Column(db.Integer)
chem = db.Column(db.Integer)
maths = db.Column(db.Integer)
eng = db.Column(db.Integer)
def __init__(self, rno, phy, chem, maths, eng):
self.rno = rno
self.phy = phy
self.chem = chem
self.maths = maths
self.eng = eng
@app.route("/")
def index():
return "Hello From Flask Here"
@app.route("/check", methods=["POST", "GET"])
def check():
data = json.loads(request.get_json())
id = data["id"]
password = data["password"]
con = None
try:
con = connect("student_results.db")
cursor = con.cursor()
cursor.execute(
"insert into StudentLoginModel (id, password) values ('%d','%s')"
% (id, password)
)
con.commit()
return jsonify({"msg": "Details are successfully stored in the database..!"})
except Exception as e:
con.rollback()
return jsonify({"msg": "There's some issue: " + str(e)})
finally:
if con is not None:
con.close()
@app.route("/addresult", methods=["POST", "GET"])
def addresult():
data = json.loads(request.get_json())
facultyid = data["facultyid"]
type = data["type"]
studentid = data["studentid"]
physicsmarks = data["physicsmarks"]
chemmarks = data["chemmarks"]
mathsmarks = data["mathsmarks"]
englishmarks = data["englishmarks"]
con = None
try:
con = connect("student_results.db")
cursor = con.cursor()
if type != "faculty" and facultyid > 0:
return jsonify({"msg": "Access Denied"})
cursor.execute(
"Insert into StoreModel (rno, phy, chem, maths, eng) values ('%d','%d','%d','%d','%d')" % (studentid, physicsmarks, chemmarks, mathsmarks, englishmarks)
)
con.commit()
return jsonify({"msg": "Details are successfully stored in the database..!"})
except Exception as e:
con.rollback()
return jsonify({"msg": "There's some issue: " + str(e)})
finally:
if con is not None:
con.close()
@app.route("/return_result", methods=['POST'])
def return_result():
if request.method == 'GET':
return jsonify({"msg": "Use 'POST' method for this API."})
data = json.loads(request.get_json())
student_id = data["id"]
user_type = data["type"]
if user_type.lower() != 'student':
return jsonify({"msg": "Invalid user type"})
con = None
try:
con = connect("student_results.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM StoreModel WHERE rno = '%d'" % student_id)
data = cursor.fetchall()
if not data:
return jsonify({"msg": "Marks details not uploaded yet."})
return jsonify(
{
"id": data[0],
"sub1": data[1],
"sub2": data[2],
"sub3": data[3],
"sub4": data[4]
}
)
except Exception as e:
con.rollback()
return jsonify({"msg": "There's some issue: " + str(e)})
finally:
if con is not None:
con.close()
if __name__ == "__main__":
app.run(debug=True, port=5000)