-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
295 lines (257 loc) · 7.61 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from flask import Flask
from flask import request
from flask import render_template
from flask import jsonify
import os
import json
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
from flask_sqlalchemy import SQLAlchemy
from flask import send_from_directory
import random, string
UPLOAD_FOLDER = 'uploaded_files'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['host'] = '0.0.0.0'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
db = SQLAlchemy(app)
class Room(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
price = db.Column(db.Integer)
feet = db.Column(db.Integer)
images = db.relationship('Image', backref='room', lazy='dynamic')
panos = db.relationship('Pano', backref='room', lazy='dynamic')
def __repr__(self):
return 'Room'
@property
def serialize(self):
return{
'id':self.id,
'name':self.name,
'price':self.price,
'feet':self.feet
}
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(128))
room_id = db.Column(db.Integer, db.ForeignKey('room.id'))
def __init__(self, filename):
self.filename = filename
def __repr__(self):
return '<Slide %r>' % self.filename
@property
def serialize(self):
return{
'id':self.id,
'filename':self.filename,
'url':url_for('uploaded_file', filename=self.filename)
}
class Pano(db.Model):
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(128))
room_id = db.Column(db.Integer, db.ForeignKey('room.id'))
def __init__(self, filename):
self.filename = filename
def __repr__(self):
return '<Slide %r>' % self.filename
@property
def serialize(self):
return{
'id':self.id,
'filename':self.filename,
'url':url_for('uploaded_file', filename=self.filename)
}
@app.route("/")
def index():
return render_template('index.html')
@app.route("/admin")
def admin():
return render_template('admin.html')
@app.route('/upload_image',methods=['GET', 'POST', 'OPTIONS'])
def upload_image():
print request.method
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
print 'no files to upload'
return redirect(request.url)
file = request.files['file']
room_id = request.form['room_id']
print('fsds:'+room_id)
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#we'll dumbly add a hash if this file already exists
if os.path.isfile( os.path.join(app.config['UPLOAD_FOLDER'], filename) ):
print "EXISTS!"
name_fragments = filename.rsplit('.', 1)
name_fragments[0] = name_fragments[0].replace('.', '')
name_fragments[1] = '.' + name_fragments[1]
name_fragments[0] = name_fragments[0] + '_' + randomword(8)
filename = ''.join(name_fragments)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
new_slide = Image(filename)
new_slide.room_id = room_id
db.session.add(new_slide)
db.session.commit()
return redirect(url_for('admin'), code=303)
#if GET
return render_template('edit_slides.html')
@app.route('/upload_pano',methods=['GET', 'POST', 'OPTIONS'])
def upload_pano():
print request.method
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
print 'no files to upload'
return redirect(request.url)
file = request.files['file']
room_id = request.form['room_id']
print('fsds:'+room_id)
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#we'll dumbly add a hash if this file already exists
if os.path.isfile( os.path.join(app.config['UPLOAD_FOLDER'], filename) ):
print "EXISTS!"
name_fragments = filename.rsplit('.', 1)
name_fragments[0] = name_fragments[0].replace('.', '')
name_fragments[1] = '.' + name_fragments[1]
name_fragments[0] = name_fragments[0] + '_' + randomword(8)
filename = ''.join(name_fragments)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
new_slide = Pano(filename)
new_slide.room_id = room_id
db.session.add(new_slide)
db.session.commit()
return redirect(url_for('admin'), code=303)
#if GET
return render_template('edit_slides.html')
@app.route('/api/<version>/get/<data_set>')
def api_get(version, data_set):
if version is None:
return jsonify(
{status:"invalid_api_version"})
if data_set is None:
return jsonify({
'status':'invalid_data_set'
})
#ROOMS
if data_set == 'rooms':
stats = Room.query.all()
if len(stats) is 0:
return jsonify({
'status':'bad',
'query':[
{'id':1, 'name':'wegsdfsdfsdfsf', 'value':4},
{'id':0, 'name':'hhhhh', 'value':1}
]
})
return jsonify({
'status':'good',
'query':[item.serialize for item in stats]
})
#IMAGES
if data_set == 'images':
room_id = request.args.get('room_id','')
stats = Image.query.filter_by(room_id=room_id).all()
if len(stats) is 0:
return jsonify({
'status':'bad',
'query':[
{'id':1, 'name':'wegsdfsdfsdfsf', 'value':4},
{'id':0, 'name':'hhhhh', 'value':1}
]
})
return jsonify({
'status':'good',
'query':[item.serialize for item in stats]
})
#PANOS
if data_set == 'panos':
room_id = request.args.get('room_id','')
stats = Pano.query.filter_by(room_id=room_id).all()
if len(stats) is 0:
return jsonify({
'status':'bad',
'query':[
{'id':1, 'name':'wegsdfsdfsdfsf', 'value':4},
{'id':0, 'name':'hhhhh', 'value':1}
]
})
return jsonify({
'status':'good',
'query':[item.serialize for item in stats]
})
#ERROR
return jsonify({
"status":"unkown_error"
})
@app.route('/api/<version>/update/<data_set>', methods=['POST'])
def api_update(version, data_set):
if version is None:
return jsonify(
{status:"invalid_api_version"})
if data_set is None:
return jsonify({
'status':'invalid_data_set'
})
status = 'bad'
#ROOM
if data_set == 'rooms':
room_name = json.loads(request.data)['name']
price = json.loads(request.data)['price']
feet = json.loads(request.data)['feet']
id = json.loads(request.data)['id']
if id == -1:
stat = Room()
stat.name = room_name
db.session.add(stat)
db.session.commit()
else:
room = Room.query.get(id)
room.price = price
room.feet = feet
db.session.commit()
status = 'good'
#IMAGE
if data_set == 'images':
room_name = json.loads(request.data)['name']
price = json.loads(request.data)['price']
id = json.loads(request.data)['id']
if id == -1:
stat = Room()
stat.name = room_name
db.session.add(stat)
db.session.commit()
else:
room = Room.query.get(id)
room.price = price
db.session.commit()
status = 'good'
return jsonify({
'status':status
})
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def randomword(length):
return ''.join(random.choice(string.lowercase) for i in range(length))
#this serves uplaoded files when requested
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == "__main__":
app.run(host='0.0.0.0')