forked from Apolloscar/RoomeetProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhousing.py
308 lines (189 loc) · 8.23 KB
/
housing.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
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
import os
from flaskr.auth import login_required
from flaskr.db import get_db
from flaskr.__init__ import create_app
bp = Blueprint('housing', __name__)
app = create_app()
app.config["HOUSING_UPLOADS"] = "flaskr/static/images/houses"
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.config["ALLOWED_EXTENSIONS"] = ALLOWED_EXTENSIONS
@bp.route('/createhousing', methods=('GET', 'POST'))
@login_required
def create_housing():
user_id = session.get('user_id')
db = get_db()
error = None
if request.method == 'POST':
housing_number = request.form['housing_number']
apt_number = request.form['apt_number']
street = request.form['street']
city = request.form['city']
state = request.form['state']
zipcode = request.form['zipcode']
description = request.form['description']
rent = request.form['rent']
genderPref = request.form['genderPref']
minage = request.form['minage']
maxage = request.form['maxage']
pets = request.form['pets']
if housing_number is None:
error = 'Housing number is required'
if apt_number is None:
apt_number = ""
if street is None:
error = 'Street is 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 not description:
description = ""
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 genderPref == "":
error = 'Gender Preferences are required'
if (not rent):
error = 'Rent is required and must be greater than 0'
if (int(rent) <= 0):
error = 'Rent is required and must be greater than 0'
if pets == "":
error = 'Pet preference is 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['HOUSING_UPLOADS'], photoname))
if error is None:
db.execute(
'INSERT INTO housing (poster_id, rent, photo, description, genderPref, ageMin,ageMax, housing_number, apt_number, street, city, state, zipcode, pets) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
(user_id, int(rent), photoname, description, genderPref, int(minage), int(maxage), housing_number, apt_number, street, city, state, int(zipcode), pets)
)
db.commit()
return redirect(url_for('housing.housings'))
flash(error)
return render_template('housing/createhousing.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_housing(id):
housing = get_db().execute(
'SELECT * FROM housing WHERE housing_id = ?', (id,)
).fetchone()
if housing is None:
abort(404, f"Housing doesn't exist.")
return housing
@bp.route('/viewhousing', defaults={'houseid': None})
@bp.route('/viewhousing/<int:houseid>')
@login_required
def view_housing(houseid):
housing = get_housing(houseid)
photopath = 'images/houses/'+housing['photo']
return render_template('housing/viewhousing.html', housing=housing, photopath=photopath)
@bp.route('/changehousing', defaults={'houseid': None}, methods=('GET', 'POST'))
@bp.route('/changehousing/<int:houseid>', methods=('GET', 'POST'))
@login_required
def change_housing(houseid):
housing = get_housing(houseid)
photopath = 'images/houses/'+housing['photo']
user_id = session.get('user_id')
db = get_db()
error = None
if housing['poster_id'] != user_id:
return redirect(url_for('housing.housings'))
if request.method == 'POST':
housing_number = request.form['housing_number']
apt_number = request.form['apt_number']
street = request.form['street']
city = request.form['city']
state = request.form['state']
zipcode = request.form['zipcode']
description = request.form['description']
rent = request.form['rent']
genderPref = request.form['genderPref']
minage = request.form['minage']
maxage = request.form['maxage']
pets = request.form['pets']
if housing_number is None:
error = 'Housing number is required'
if apt_number is None:
apt_number = ""
if street is None:
error = 'Street is 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 not description:
description = ""
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 genderPref == "":
error = 'Gender Preferences are required'
if (not rent):
error = 'Rent is required and must be greater than 0'
if (int(rent) <= 0):
error = 'Rent is required and must be greater than 0'
if pets == "":
error = 'Pet preference is 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['HOUSING_UPLOADS'], photoname))
if error is None:
db.execute(
'UPDATE housing SET rent =?, photo=?, description=?, genderPref=?, ageMin=?, ageMax=?, housing_number=?, apt_number=?, street=?, city=?, state=?, zipcode=?, pets=?'
'WHERE housing_id = ?', (int(rent), photoname, description, genderPref, int(minage), int(maxage), housing_number, apt_number, street, city, state, int(zipcode), pets, houseid)
)
db.commit()
return redirect(url_for('housing.housings'))
error = 'upload the right file'
flash(error)
return render_template('housing/changehousing.html', housing=housing, photopath=photopath)
@bp.route('/deletehousing', defaults={'houseid': None}, methods=('GET', 'POST'))
@bp.route('/deletehousing/<int:houseid>', methods=('GET', 'POST'))
@login_required
def delete_housing(houseid):
housing = get_housing(houseid)
user_id = session.get('user_id')
db = get_db()
error = None
if housing['poster_id'] != user_id:
return redirect(url_for('roommeet.index'))
else:
db.execute('DELETE FROM housing WHERE housing_id = ?', (houseid,))
db.commit()
return redirect(url_for('housing.housings'))
@bp.route('/housings')
@login_required
def housings():
user_id = session.get('user_id')
housings = get_db().execute(
'SELECT housing_id FROM housing WHERE poster_id = ?', (user_id,)
).fetchall()
housinglist = []
for h in housings:
for i in h:
housinglist.append(i)
return render_template('housing/housings.html', hlist = housinglist)