-
Notifications
You must be signed in to change notification settings - Fork 1
/
roommeet.py
326 lines (210 loc) · 9.2 KB
/
roommeet.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from flask import (
Blueprint, Flask, flash, g, redirect, render_template, request, url_for, session, current_app
)
from werkzeug.exceptions import abort
from werkzeug.utils import secure_filename
from datetime import datetime, date
import os
from flaskr.auth import login_required
from flaskr.db import get_db
from flaskr.__init__ import create_app
bp = Blueprint('roommeet', __name__)
app = create_app()
app.config["PROFILE_UPLOADS"] = "flaskr/static/images/profiles"
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.config["ALLOWED_EXTENSIONS"] = ALLOWED_EXTENSIONS
@bp.route('/')
def index():
if (g.user == None):
return render_template("roommeet/home.html")
return render_template('roommeet/index.html')
@bp.route('/createprofile', methods=('GET', 'POST'))
@login_required
def create_profile():
user_id = session.get('user_id')
db = get_db()
error = None
if request.method == 'POST':
fname = request.form['fname']
mname = request.form['mname']
lname = request.form['lname']
dob = request.form['dob']
occupation = request.form['occupation']
description = request.form['description']
gender = request.form['gender']
genderPref = request.form['genderPref']
minage = request.form['minage']
maxage = request.form['maxage']
minprice = request.form['minprice']
maxprice = request.form['maxprice']
city = request.form['city']
state = request.form['state']
zipcode = request.form['zipcode']
pets = request.form['pets']
looking = request.form['looking']
if not fname:
error = 'First Name is required.'
elif not lname:
error = 'Last Name is required.'
if not mname:
mname = ""
if not occupation:
error = 'Occupation is required'
if not description:
description = ""
if not dob:
error = "Need date of birth"
if get_age(dob) == -1:
error = "You must be at least 17 years. "
if ((not minage) or (not maxage)):
error = 'Please enter age values. Must be more than 17 and Maximum Age should be greater'
if ((int(minage) < 17) or (int(minage) >= int(maxage))):
error = 'Please enter age values. Must be more than 17 and Maximum Age should be greater'
if ((not minprice) or (not maxprice)):
error = 'Please enter price values. Maximum price should be greater'
if (int(minprice) < 0) or (int(minprice) > int(maxprice)):
error = 'Please enter price values. Maximum price should be greater'
if gender == "":
error = 'Gender is required'
if genderPref == "":
error = 'Gender Preferences are required'
if city is None:
error = 'City is required'
if state is None:
error = 'State is required'
if zipcode is None:
error = 'Zipcode is required'
if pets == "":
error = 'Pet preference is required'
if looking == "":
error = 'Search preferences are required.'
if 'photo' not in request.files:
error = 'Missing Image'
photo = request.files['photo']
photoname = ''
if photo.filename == '':
error ='No selected file'
if photo and allowed_file(photo.filename):
photoname = secure_filename(photo.filename)
photo.save(os.path.join(app.config['PROFILE_UPLOADS'], photoname))
if error is None:
db.execute(
'INSERT INTO profile (user_id, first_name, middle_name, last_name, photo, dob, occupation, description, gender,'
'genderPref, ageMin,ageMax,priceMin,priceMax, city, state, zipcode, pets, looking) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
(user_id, fname, mname, lname, photoname, dob, occupation, description, gender, genderPref, int(minage), int(maxage), int(minprice), int(maxprice), city, state, int(zipcode), pets, looking)
)
db.execute(
'UPDATE user SET verified = ?'
'WHERE id = ?', (1, user_id)
)
db.commit()
return redirect(url_for('roommeet.index'))
flash(error)
return render_template('profile/createprofile.html')
def get_age(bdate):
dt = datetime.strptime(bdate, '%Y-%m-%d')
d = dt.date()
today = date.today()
age = today.year - d.year - ((today.month, today.day) < (d.month, d.day))
if age >= 17:
return age
return -1
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_profile(id):
profile = get_db().execute(
'SELECT * FROM profile WHERE user_id = ?', (id,)
).fetchone()
if profile is None:
abort(404, f"Profile doesn't exist. Create a profile.")
return profile
@bp.route('/changeprofile', methods=('GET', 'POST'))
@login_required
def change_profile():
user_id = session.get('user_id')
profile = get_profile(user_id)
photopath = 'images/profiles/'+profile['photo']
db = get_db()
error = None
if request.method == 'POST':
fname = request.form['fname']
mname = request.form['mname']
lname = request.form['lname']
dob = request.form['dob']
occupation = request.form['occupation']
description = request.form['description']
gender = request.form['gender']
genderPref = request.form['genderPref']
minage = request.form['minage']
maxage = request.form['maxage']
minprice = request.form['minprice']
maxprice = request.form['maxprice']
city = request.form['city']
state = request.form['state']
zipcode = request.form['zipcode']
pets = request.form['pets']
looking = request.form['looking']
if not fname:
error = 'First Name is required.'
elif not lname:
error = 'Last Name is required.'
if not mname:
mname = ""
if not occupation:
error = 'Occupation is required'
if not description:
description = ""
if not dob:
error = "Need date of birth"
if get_age(dob) == -1:
error = "You must be at least 17 years. "
if ((not minage) or (not maxage) or (minage == ' ') or (maxage == ' ')):
error = 'Please enter age values. Must be more than 17 and Maximum Age should be greater'
if (int(minage) < 17) or (minage >= maxage):
error = 'Please enter age values. Must be more than 17 and Maximum Age should be greater'
if ((not minprice) or (not maxprice)):
error = 'Please enter price values. Maximum price should be greater'
if (int(minprice) < 0) or (minprice > maxprice):
error = 'Please enter price values. Maximum price should be greater'
if gender == "":
error = 'Gender is required'
if genderPref == "":
error = 'Gender Preferences are required'
if city is None:
error = 'City is required'
if state is None:
error = 'State is required'
if zipcode is None:
error = 'Zipcode is required'
if pets == "":
error = 'Pet preference is required'
if looking == "":
error = 'Search preferences are required.'
if 'photo' not in request.files:
error = 'Missing Image'
photo = request.files['photo']
photoname = None
if photo.filename is None:
error = 'No selected file'
if photo and allowed_file(photo.filename):
photoname = secure_filename(photo.filename)
photo.save(os.path.join(app.config['PROFILE_UPLOADS'], photoname))
if error is None:
db.execute(
'UPDATE profile SET first_name = ?, middle_name = ?, last_name = ?, dob = ?, photo=?, occupation= ?, description= ?, gender= ?, genderPref= ?, ageMin= ?,ageMax= ?,priceMin= ?,priceMax= ?, city= ?, state=?, zipcode=?, pets=?, looking=?'
'WHERE user_id = ?', (fname, mname, lname, dob, photoname, occupation, description, gender, genderPref, int(minage), int(maxage), int(minprice), int(maxprice), city, state, int(zipcode), pets, looking, user_id)
)
db.commit()
return redirect(url_for('roommeet.index'))
error = 'upload the right file'
flash(error)
return render_template('profile/changeprofile.html', profile=profile, photopath=photopath)
@bp.route('/viewprofile', defaults={'profid': None})
@bp.route('/viewprofile/<int:profid>')
@login_required
def view_profile(profid):
profile = get_profile(profid)
age = get_age(profile['dob'])
photopath = 'images/profiles/'+profile['photo']
return render_template('profile/viewprofile.html', profile=profile, photopath=photopath, age=age)