forked from laytonmusyoki/KABU-CS-HUB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
280 lines (233 loc) · 8.79 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from flask import Flask, render_template, request, redirect, url_for, flash,session,make_response
from flask_mysqldb import MySQL
from urllib.parse import quote
from io import BytesIO
from flask_mail import Mail, Message
app = Flask(__name__)
app.secret_key='futfygkbkjbkbdlkeougxx'
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT']=465
app.config['MAIL_USERNAME']='kabu.cs.hub@gmail.com'
app.config['MAIL_PASSWORD']='vhdzrcqhzehtqufd'
app.config['MAIL_USE_TLS']=False
app.config['MAIL_USE_SSL']=True
mail=Mail(app)
# connection=pymysql.connect(
# host='',
# user='kabucshub',
# password='kabucshub',
# database='kabu_hub'
# )
app.config['MYSQL_HOST']="db4free.net"
app.config['MYSQL_USER']="kabucshub"
app.config['MYSQL_PASSWORD']="kabucshub"
app.config['MYSQL_DB']="kabucshub"
mysql=MySQL(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/sem1-1')
def sem1_1():
semester="Semester1.1"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem1-1.html',notes=notes,papers=papers)
@app.route('/sem1-2')
def sem1_2():
semester="Semester1.2"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem1-2.html',notes=notes,papers=papers)
@app.route('/sem2-1')
def sem2_1():
semester="Semester2.1"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem2-1.html',notes=notes,papers=papers)
@app.route('/sem2-2')
def sem2_2():
semester="Semester2.2"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem2-2.html',notes=notes,papers=papers)
@app.route('/sem3-1')
def sem3_1():
semester="Semester3.1"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem3-1.html',notes=notes,papers=papers)
@app.route('/sem3-2')
def sem3_2():
semester="Semester3.2"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem3-2.html',notes=notes,papers=papers)
@app.route('/sem4-1')
def sem4_1():
semester="Semester4.1"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem4-1.html',notes=notes,papers=papers)
@app.route('/sem4-2')
def sem4_2():
semester="Semester4.2"
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM notes WHERE semester=%s",(semester,))
notes=cur.fetchall()
cur.execute("SELECT * FROM papers WHERE semester=%s",(semester,))
papers=cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('sem4-2.html',notes=notes,papers=papers)
@app.route('/send_mail', methods=['GET', 'POST'])
def send_mail():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
subject = request.form['subject']
description = request.form['description']
html_content = render_template('response.html', name=name, email=email, description=description)
try:
msg = Message(subject=subject, sender = email, html=html_content, recipients= ['laytonmatheka7@gmail.com'])
mail.send(msg)
flash(f"Thanks {name} your message has been send Successfully",'success')
return redirect(url_for('index'))
except Exception as error:
flash("Mail not sent please check your internet",'danger')
return redirect(url_for('index'))
@app.route('/news')
def news():
return render_template('check.html')
@app.route('/useful_links')
def useful_links():
return render_template('useful_links.html')
@app.route('/admin_authorization',methods=['POST','GET'])
def admin_validation():
if request.method=='POST':
username=request.form['username']
password=request.form['password']
code=request.form['code']
if username=='' or password=='' or code=='':
flash('All fields are required','danger')
return render_template('validate.html')
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM users WHERE username=%s AND password=%s AND auth_code=%s",(username,password,code))
user=cur.fetchone()
mysql.connection.commit()
cur.close()
if user is not None:
session['username']=user[1]
flash(f'Logged in as {username}','success')
return redirect(url_for('notes'))
else:
flash('Wrong credentials','warning')
return render_template('validate.html',username=username,password=password,code=code)
return render_template('validate.html')
@app.route('/notes',methods=['POST','GET'])
def notes():
if 'username' not in session:
flash('Please you need to be authorized','warning')
return redirect(url_for('index'))
else:
if request.method=='POST':
semester=request.form['semester']
code=request.form['code']
file=request.files['pdf']
cur=mysql.connection.cursor()
cur.execute("INSERT INTO notes(semester,code,file_name,pdf) VALUES(%s,%s,%s,%s)",(semester,code,file.filename,file.read(),))
mysql.connection.commit()
cur.close()
flash(f'{semester} {code} notes has been added','success')
return redirect(url_for('index'))
else:
return render_template('notes.html')
@app.route('/papers',methods=['POST','GET'])
def papers():
if 'username' not in session:
flash('Please you need to be authorized','warning')
return redirect(url_for('index'))
else:
if request.method=='POST':
semester=request.form['semester']
code=request.form['code']
file=request.files['pdf']
cur=mysql.connection.cursor()
cur.execute("INSERT INTO papers(semester,code,file_name,pdf) VALUES(%s,%s,%s,%s)",(semester,code,file.filename,file.read(),))
mysql.connection.commit()
cur.close()
print(semester)
flash(f'{semester} {code} papers has been added','success')
return redirect(url_for('index'))
else:
return render_template('papers.html')
@app.route('/download/<table>/<id>')
def download(table, id):
try:
cur = mysql.connection.cursor()
if table == "notes":
cur.execute("SELECT * FROM notes WHERE id=%s", (id,))
data = cur.fetchone()
else:
cur.execute("SELECT * FROM papers WHERE id=%s", (id,))
data = cur.fetchone()
if data:
response = make_response(data[4])
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = f"attachment; filename='{data[3]}'"
return response
else:
flash('File not found', 'danger')
return redirect(url_for('index'))
except Exception as e:
flash('An error occurred while downloading the file', 'danger')
print("Error:", e)
return redirect(url_for('index'))
finally:
if 'cur' in locals():
cur.close()
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/logout')
def logout():
if 'username' in session:
session.clear()
flash('You have been logged out','warning')
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)