-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
153 lines (123 loc) · 5.58 KB
/
main.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
147
148
149
150
from flask import Flask, render_template, request, redirect, url_for, jsonify, session
from operation import backend, admin, database
from calculator import cgpa
import os
app = Flask(__name__)
app.secret_key = os.urandom(24)
app.config['FILES_UPLOAD'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'upload')
SEMESTER, YEAR = admin().get_last_result()
@app.context_processor
def example():
return dict(enumerate=enumerate, list=list, len=len, int=int)
# HOME
@app.route('/', methods=['GET', 'POST'])
@app.route('/result', methods=['GET', 'POST'])
def home():
if request.method=='POST':
try:
js = backend().generate_API(request.form['regiInput'], request.form['select'])
return render_template("result.html", title='Result', info=js)
except Exception as e:
return render_template('includes/error.html', err=str(e))
return render_template('home.html', semester=SEMESTER, year=YEAR)
@app.route('/result/find', methods=['GET', 'POST'])
def find():
if request.method == 'POST':
try:
return jsonify({'reg_no': database().find_regi(request.form['batch'], request.form['name'])})
except:
return jsonify(database().find_name(request.form['batch']))
return render_template('find_student.html', title='Find yourself')
@app.route('/subscribe', methods=['POST'])
def email():
if database().add_email(request.form['reg_no'], request.form['email']):
return jsonify({'submitted':True, 'htmlValue': render_template('get_email.html')})
else:
return jsonify({'submitted': False})
# API
@app.route('/result/api/<string:semester>/<string:reg_no>', methods=['POST', 'GET'])
def api(reg_no, semester):
if request.method=='GET':
return jsonify(backend().generate_API(reg_no, semester))
# CALCULATOR
@app.route('/calculator', methods=['GET', 'POST'])
def calculator():
if request.method == 'POST':
gArr = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th']
gr, cr= [], []
for i in range(int(request.form.get('select'))):
gr.append(request.form[gArr[i]].upper())
cr.append(float(request.form[gArr[i]+'-c']))
json = {'result': cgpa(gr, cr), 'array': gArr, 'grades': gr, 'cradits': cr}
return render_template('calculator.html', title='Calculator', data=json)
return render_template('calculator.html', title='Calculator')
# LOGIN
@app.route('/login', methods=['GET', 'POST'])
def login():
session.pop('user', None)
if request.method == 'POST':
res = admin().login(request.form['email'], request.form['passwd'])
if res==True:
session['user'] = request.form['email'].split('@')[0]
return redirect(url_for('admin_panel'))
else:
return res
return render_template('admin/login.html', title='Admin-login')
# ADMIN PANEL
@app.route('/admin-panel')
def admin_panel():
if 'user' in session:
return render_template('admin/adminHome.html', title='Admin', user=session['user'])
else:
return redirect(url_for('login'))
@app.route('/new-admin/', methods=['POST', 'GET'])
def new_admin():
if 'user' in session:
if request.method == 'POST':
v = admin().add_new(request.form['email'], request.form['password'])
return jsonify({'success': True}) if v else jsonify({'success': False})
else:
return render_template('admin/new_admin.html', title='New admin', user=session['user'])
else:
return redirect(url_for('login'))
# upload file
@app.route('/admin/upload', methods=['GET', 'POST'])
def uploadTXT():
if 'user' in session:
if request.method == "POST":
semester = request.form.get('select')
year = request.form['year']
txt = request.files['inputFile']
txt.save(os.path.join(app.config['FILES_UPLOAD'], txt.filename))
backend().upload_results(txt.filename, semester, year, session['user'])
return jsonify({"success":True})
return render_template('admin/upload.html', title='Admin-Upload', user=session['user'])
else:
return redirect(url_for('login'))
@app.route('/admin/update-db', methods=['GET', 'POST'])
def updateFromNU():
if 'user' in session:
if request.method == 'POST':
batch = request.form['batch']
semester = request.form['semester']
xm_code = request.form['xm-code']
xm_year = request.form['xm-year']
return backend().scrap_results(batch, semester, xm_code, xm_year, session['user'])
return render_template('admin/updateFromNU.html', title='Update result', user=session['user'])
return redirect(url_for('login'))
@app.route('/admin/create-tables', methods=['POST', 'GET'])
def create_tables():
if 'user' in session:
if request.method == "POST":
backend().create_tables_in_database()
return render_template('admin/create_table.html', title='Admin- Create-tables', user=session['user'])
return redirect(url_for('login'))
@app.route('/admin/log')
def log():
if 'user' in session:
log = admin().get_log()
return render_template('admin/log.html', title='Log', data=log, user=session['user'])
else:
return redirect(url_for('login'))
if __name__=="__main__":
app.run(host='localhost', port=8080, debug=True)