-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
274 lines (234 loc) · 7.86 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
import bcrypt
from functools import wraps
from flask import Flask, redirect, url_for, abort, request, render_template, json, g, session, flash
from werkzeug.utils import secure_filename
from flask_googlemaps import GoogleMaps
from flask_googlemaps import Map
import sqlite3
import geocoder
#Application Object
app = Flask(__name__)
db_location = 'var/database.db'
app.secret_key = 'a_really_secret_key'
GoogleMaps(app, key="AIzaSyAdclqNA7O-THQMxSSJEGvM00SNBvannoI")
@app.route("/")
def index():
return render_template("base.html"), 200
# Start of DB stuff
###################
def get_db():
db = getattr(g, 'db', None)
if db is None:
db = sqlite3.connect(db_location)
g.db = db
return db
@app.teardown_appcontext
def close_db_connection(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
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()
# End of DB stuff
#################
# Validate login
def validate(email, password):
conn = sqlite3.connect('var/database.db')
with conn:
cur = conn.cursor()
cur.execute('SELECT * FROM users WHERE email=(?)', (email,))
rows = cur.fetchall()
for row in rows:
dbEmail = row[0]
dbPass = row[1]
#print(dbEmail, email, dbPass, password)
if dbEmail == email and dbPass == password:
# CHECK HASHED PW
#if (email == dbEmail and password == bcrypt.hashpw(password.encode('utf-8'), password)):
return True
else:
return False
# REQUIRES LOGIN STUFF #
########################
def requires_login(f):
@wraps(f)
def decorated(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('You need to login')
return redirect(url_for('login'))
return decorated
@app.route("/logout")
@requires_login
def logout():
session['logged_in'] = False
flash("You were logged out")
return redirect(url_for('index'))
@app.route("/members")
@requires_login
def members():
if session['logged_in']:
#return "link from here to create profile Page"
return render_template("members.html")
else:
return redirect(url_for('index'))
# INDIVIDUAL PROFILE PAGE#
##########################
@app.route("/members/<email>")
@requires_login
def my_profile(email):
conn = sqlite3.connect('var/database.db')
with conn:
cur = conn.cursor()
cur.execute('SELECT * FROM profiles WHERE email = (?)', (email,))
rows = cur.fetchall()
for row in rows:
username = row[1]
return render_template('myProfile.html', rows=rows)
# USER LOGIN #
##############
@app.route("/login", methods=['GET', 'POST'])
def login():
error=None
success=None
if request.method == 'POST':
email = request.form['user_email']
password = request.form['user_password']
if validate(email, password) or validate_prem(email, password):
session['logged_in'] = True
flash('You have succesfully logged in')
return redirect(url_for('my_profile', email=email))
else:
flash('Wrong details, try again!')
return render_template("login.html")
@app.route("/basic")
def basic():
return render_template("signUp.html")
# signUp.html brings you here
@app.route('/adduser',methods = ['POST', 'GET'])
def adduser():
if request.method == 'POST':
username = request.form['username']
email = request.form['user_email']
password = request.form['user_password']
# Try to hash and salt password
#valid_pwhash = bcrypt.hashpw(password, bcrypt.gensalt())
db = get_db()
db.cursor().execute("INSERT INTO users (email,password,username) VALUES(?,?,?)", (email,password,username) )
db.commit()
return redirect(url_for('login',username=username))
else:
return render_template("signUp.html")
# CREATE PROFILE ROUTE
@app.route("/profile/create",methods = ['GET', 'POST'])
def create_profile():
error = None
msg = None
if request.method == 'POST':
email = request.form['email']
username = request.form['username']
location = request.form['location']
bio = request.form['bio']
gender = request.form['gender']
#g = geocoder.google(location)
#coords = g.latlng
## HANDLE IMAGES ##
f = request.files['datafile']
new_file = f.filename
if new_file == "":
# Make page for line below if time!!
return "Please select a file to upload"
else:
f.save('static/img/' + new_file)
db = get_db()
db.cursor().execute("INSERT INTO profiles(email,username,location,bio,gender,prof_img) VALUES (?,?,?,?,?,?)",(email,username,location,bio,gender,new_file))
db.commit()
flash("Profile Created!")
return redirect(url_for('my_profile', email=email, location=location))
else:
return render_template('newProfile.html')
# UPDATE PROFILE ROUTE #
@app.route("/profile/update", methods = ['GET', 'POST'])
def update_profile():
error = None
msg = None
if request.method == 'POST':
email = request.form['email']
username = request.form['username']
location = request.form['location']
bio = request.form['bio']
gender = request.form['gender']
f = request.files['datafile']
new_file = f.filename
if new_file == "":
# Make page for line below if time!!
return "Please select a file to upload"
else:
f.save('static/img/' + new_file)
db = get_db()
db.cursor().execute("UPDATE profiles SET email=?, username=?,location=?,bio=?, gender=?, prof_img=? WHERE email= ?" ,(email,username,location,bio,gender,new_file,email))
#cur.execute("UPDATE profiles SET email=?,username=?,location=?,bio=?,gender=?,prof_img=? WHERE email=?",(email,username,location,bio,gender,prof_img))
db.commit()
flash("Your profile is updated")
return redirect(url_for('my_profile', email=email))
else:
return render_template('update.html')
# DELETE PROFILE #
#def delete_profile():
# db = get_db()
# db.cursor().execute("DELETE * FROM profiles WHERE email = (?)', (email,))
# db.commit()
@app.route("/members/view")
def view_members():
conn = sqlite3.connect('var/database.db')
session['logged_in'] = False
with conn:
cur = conn.cursor()
cur.execute('SELECT * FROM profiles')
rows = cur.fetchall()
return render_template('allMembers.html', rows=rows)
@app.route("/premium")
def premium():
return render_template("premium.html")
@app.route("/addpremium", methods = ['GET', 'POST'])
def add_premium():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
email = request.form['email']
cc_number = request.form['cc_number']
sec_code = request.form['sec_code']
db = get_db()
db.cursor().execute("INSERT INTO premium (username,password,email,cc_number,sec_code) VALUES (?,?,?,?,?)",(username,password,email,cc_number,sec_code) )
db.commit()
return redirect(url_for('login'))
else:
return render_template("premium.html")
# Validate login
def validate_prem(email, password):
conn = sqlite3.connect('var/database.db')
with conn:
cur = conn.cursor()
cur.execute('SELECT * FROM premium WHERE email=(?)', (email,))
rows = cur.fetchall()
for row in rows:
dbEmail = row[0]
dbPass = row[1]
#print(dbEmail, email, dbPass, password)
if dbEmail == email and dbPass == password:
# CHECK HASHED PW
#if (email == dbEmail and password == bcrypt.hashpw(password.encode('utf-8'), password)):
return True
else:
return False
#custom 404 Route
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
if __name__ =='__main__':
app.run(host='0.0.0.0', debug=True)