-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflaskapp.py
231 lines (187 loc) · 7.57 KB
/
flaskapp.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
#Imports
#------------------------------------------------------------------------------#
import os
from flask import Flask, render_template, request, redirect, session, flash
import urllib.request
from flask import send_file
import sqlite3
from flask import g
import hashlib
#------------------------------------------------------------------------------#
#Initial Setups
#------------------------------------------------------------------------------#
salt = "TwinFuries"
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
app.secret_key = os.urandom(24)
#------------------------------------------------------------------------------#
#Database Setup and functions
#------------------------------------------------------------------------------#
DATABASE = os.path.join(APP_ROOT,'Database/database.db')
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
#db.row_factory = sqlite3.Row
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))
@app.route('/init_db')
def init_db():
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
def execute_db(query):
cur = get_db()
cur.execute(query)
cur.commit()
cur.close()
#------------------------------------------------------------------------------#
#Website routes
#------------------------------------------------------------------------------#
#Login Page(Everyone)
#------------------------------------------------------------------------------#
@app.route('/', methods = ['POST' , 'GET'])
def index():
message = ""
if request.method == 'POST':
user = request.form['username']
pswdun = request.form['password']+salt
pswd = hashlib.md5(pswdun.encode())
password = query_db('select password from Details where Username="'+user+'"')
if password:
if password[0][0] == pswd.hexdigest():
session['user'] = user
return redirect('/home')
else:
message = "Invalid Username or Password"
return render_template('login.html', confirm = message)
#------------------------------------------------------------------------------#
#Registration Page(Everyone)
#------------------------------------------------------------------------------#
@app.route('/register', methods = ['GET', 'POST'])
def register():
failed = ""
if request.method == 'POST':
username = request.form['username']
college = request.form['college']
email = request.form['email']
passwordun = request.form['pwd1']+salt
password = hashlib.md5(passwordun.encode())
chkmail = query_db('select Username from Details where Email="'+email+'"')
if chkmail:
failed = "Email already registered"
else:
execute_db('insert into Details values("'+username+'","'+email+'","'+college+'","'+password.hexdigest()+'")')
return redirect('/')
return render_template('register.html', message = failed)
#------------------------------------------------------------------------------#
@app.route('/db_add')
def adddata():
pass
#Home Page(Logged in)
#------------------------------------------------------------------------------#
@app.route('/home')
def home():
if 'user' in session:
return render_template('home.html')
flash("Log in to view this page")
return redirect('/')
#------------------------------------------------------------------------------#
#Upload file(Logged in)
#------------------------------------------------------------------------------#
@app.route('/upload', methods = ['GET', 'POST'])
def upload_file():
message = ""
if 'user' in session:
if request.method == 'POST':
target = os.path.join(APP_ROOT, 'Uploaded_Notes/')
if not os.path.isdir(target):
os.mkdir(target)
f = request.files['file']
fname = f.filename
filenamels = fname.split(".")
validfiles = ["doc" , "docx" , "pdf", "epub"]
if filenamels[1] in validfiles:
destination = "/".join([target,fname])
if(os.path.isfile(destination)):
message = "We already have those notes"
else:
f.save(destination)
message = "File Uploaded Successfully"
else:
message = "Invalid File Format"
return render_template('upload.html',confirm = message)
flash("Log in to view this page")
return redirect('/')
#------------------------------------------------------------------------------#
#Upload link(Logged in)
#------------------------------------------------------------------------------#
@app.route('/link', methods = ['GET', 'POST'])
def download_link():
if 'user' in session:
if request.method == 'POST':
target = os.path.join(APP_ROOT, 'Uploaded_Notes/')
url = request.form['link']
fname = url[url.rfind("/")+1:]
destination = "/".join([target,fname])
urllib.request.urlretrieve("http://"+url,destination)
message = "File will be added if available"
return render_template('linksend.html',confirm = message)
flash("Log in to view this page")
return redirect('/')
#------------------------------------------------------------------------------#
#Download Uploaded_Notes(Logged in)
#------------------------------------------------------------------------------#
@app.route('/download')
def download():
if 'user' in session:
file_list = []
target = os.path.join(APP_ROOT, 'Uploaded_Notes/')
for root, dirs, files in os.walk(target):
for filename in files:
file_list.append(filename)
return render_template('download.html', fnames = file_list)
flash("Log in to view this page")
return redirect('/')
@app.route('/download/<fname>', methods = ['GET' , 'POST'])
def download_file(fname):
if 'user' in session:
target = os.path.join(APP_ROOT, 'Uploaded_Notes/')
destination = "/".join([target,fname])
return send_file(destination, as_attachment=True)
flash("Log in to view this page")
return redirect('/')
#------------------------------------------------------------------------------#
#uploadoptions(Logged in)
#------------------------------------------------------------------------------#
@app.route('/uploadoptions')
def downloadoptions():
if 'user' in session:
return render_template('downloadoptions.html')
flash("Log in to view this page")
return redirect('/')
#Logout
#------------------------------------------------------------------------------#
@app.route('/logout')
def logout():
session.pop('user',None)
return redirect('/')
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
if __name__ == '__main__':
app.run(debug = True)