forked from naturenet/naturenet-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
567 lines (489 loc) · 17.8 KB
/
api.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import os
from flask import Flask
from flask import request
from flask import Response
from flask import render_template
from flask import make_response, current_app
from flask_bootstrap import Bootstrap
from flask import jsonify
from flask.json import JSONEncoder
from functools import update_wrapper
from db_def import db
from db_def import app
from db_def import Account
from db_def import Note
from db_def import Context
from db_def import Media
from db_def import Feedback
from db_def import Site
from datetime import datetime
from datetime import timedelta
import calendar
import cloudinary
import cloudinary.api
import cloudinary.uploader
from cloudinary.utils import cloudinary_url
cloudinary.config(
cloud_name = 'university-of-colorado',
api_key = '893246586645466',
api_secret = '8Liy-YcDCvHZpokYZ8z3cUxCtyk'
)
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
import json
import psycopg2
Bootstrap(app)
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, datetime):
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(
calendar.timegm(obj.timetuple()) * 1000 +
obj.microsecond / 1000
)
return millis
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
app.json_encoder = CustomJSONEncoder
def success(data):
return jsonify({"status_code": 200, "status_txt": "OK",
"data": data})
def error(msg):
return jsonify({"status_code": 400, "status_txt": msg}), 400
@app.route('/api')
def api():
return render_template('apis.html')
#
# Account
#
@app.route('/api/accounts/count')
def api_accounts_count():
n = Account.query.count()
return jsonify({'success' : True, 'data' : n})
@app.route('/api/account/new/<username>', methods = ['POST','GET'])
def api_account_new(username):
if request.method == 'POST':
f = request.form
if username and 'email' in f and 'name' in f and 'consent' in f and 'password' in f:
account = Account.query.filter_by(username=username).first()
if not account:
newAccount = Account(username)
newAccount.name = f['name']
newAccount.email = f['email']
newAccount.consent = f['consent']
newAccount.password = f['password']
newAccount.created_at = datetime.now()
newAccount.icon_url = f.get('icon_url', newAccount.icon_url)
db.session.add(newAccount)
db.session.commit()
return success(newAccount.to_hash())
return error("Username %s is already taken" % username)
return error("Username is not specified")
else:
return error("the request to add [%s] must be done through a post" % username)
@app.route('/api/account/<query>')
@crossdomain(origin='*')
def api_account_get(query):
field = request.args.get("field","username")
if field == 'username':
account = Account.query.filter_by(username=query).first()
else:
account = Account.query.get(query)
if account:
return success(account.to_hash())
else:
return error("user does not exist")
@app.route('/api/account/<username>/notes')
@crossdomain(origin='*')
def api_account_get_notes(username):
account = Account.query.filter_by(username=username).first()
return success([x.to_hash() for x in account.notes])
@app.route('/api/account/<username>/feedbacks')
@crossdomain(origin='*')
def api_account_get_feedbacks(username):
account = Account.query.filter_by(username=username).first()
return success([x.to_hash() for x in account.feedbacks])
@app.route('/api/accounts')
@crossdomain(origin='*')
def api_accounts_list():
accounts = Account.query.all()
return success([x.to_hash() for x in accounts])
#
# Note
#
@app.route('/api/note/<id>')
@crossdomain(origin='*')
def api_note_get(id):
note = Note.query.get(id)
return success(note.to_hash())
@app.route('/api/notes')
@crossdomain(origin='*')
def api_note_list():
format = request.args.get('format', 'full')
n = request.args.get('n',1000)
notes = Note.query.limit(n)
return success([x.to_hash(format) for x in notes])
@app.route('/api/note/<id>/feedbacks')
@crossdomain(origin='*')
def api_note_get_feedbacks(id):
note = Note.query.filter_by(id=id).first()
feedbacks = Feedback.query.filter_by(table_name='Note', row_id=id).all()
return success([x.to_hash() for x in feedbacks])
@app.route('/api/note/<id>/update', methods = ['POST'])
def api_note_update(id):
obj = request.form
username = obj.get('username', '')
account = Account.query.filter_by(username=username).first()
note = Note.query.get(id)
if note and account:
note.content = obj.get('content', note.content)
note.kind = obj.get('kind', note.kind)
if 'context' in obj:
c = Context.query.filter_by(name=obj['context']).first()
if c == None:
return error("context %s does not exist" % obj['context'])
note.context = c
note.modified_at = datetime.now()
db.session.commit()
return success(note.to_hash())
return error("some parameters are missing")
@app.route('/api/note/new/<username>', methods = ['POST', 'GET'])
def api_note_create(username):
if request.method == 'POST':
obj = request.form
if username and obj and 'content' in obj and 'context' in obj and 'kind' in obj:
content = obj['content']
context = obj['context']
kind = obj['kind']
a = Account.query.filter_by(username=username).first()
c = Context.query.filter_by(name=context).first()
if a and c:
note = Note(a.id, c.id, kind, content)
if 'longitude' in obj and 'latitude' in obj:
note.longitude = obj['longitude']
note.latitude = obj['latitude']
db.session.add(note)
db.session.commit()
return success(note.to_hash())
return error("some parameters are missing")
else:
return error("the request must be a post")
#
# Media
#
@app.route('/api/medias')
def api_media_list():
medias = Media.query.all()
return success([x.to_hash() for x in medias])
@app.route('/api/media/<id>')
@crossdomain(origin='*')
def api_media_get(id):
media = Media.query.get(id)
if media:
return success(media.to_hash())
else:
return error("media object does not exist")
@app.route('/api/media/<id>/feedbacks')
@crossdomain(origin='*')
def api_media_get_feedbacks(id):
feedbacks = Feedback.query.filter_by(table_name='Media', row_id=id).all()
return success([x.to_hash() for x in feedbacks])
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/api/note/<id>/new/photo', methods = ['POST','GET'])
def api_media_create(id):
if request.method == 'POST':
print "files: %s" % request.files
print "form: %s" % request.form
link = request.form.get("link","")#["link"] or request.form["link"] or ""
title = request.form.get("title","")#files["title"] or request.form["title"] or ""
kind = "Photo"
note = Note.query.get(id)
print "note: %s" % note
if note:
media = Media(note.id, kind, title, link)
file = request.files.get("file",None)
print "file: %s" % file
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#print "saving locally to " + filename
response = cloudinary.uploader.upload(file, public_id = media.id)
print "uploading to cloudinary .."
if response:
print response['url']
media.link = response['url']
db.session.add(media)
db.session.commit()
return success(media.to_hash())
else:
return error("note id %d is invalid" % id);
else:
return error("adding a media object to note {%s}, this request must be a post." % id)
#
# Context
#
@app.route('/api/contexts')
@crossdomain(origin='*')
def api_context_list_all():
contexts = Context.query.all()
return success([x.to_hash() for x in contexts])
@app.route('/api/context/<id>')
@crossdomain(origin='*')
def api_context_get(id):
context = Context.query.get(id)
return success(context.to_hash())
@app.route('/api/context/<id>/notes')
@crossdomain(origin='*')
def api_context_get_all_notes(id):
context = Context.query.get(id)
if context:
items = context.notes
return success([x.to_hash() for x in items])
@app.route('/api/context/activities')
@crossdomain(origin='*')
def api_context_get_all_activities():
items = Context.query.filter_by(kind='Activity').all()
return success([x.to_hash() for x in items])
@app.route('/api/context/landmarks')
@crossdomain(origin='*')
def api_context_get_all_landmarks():
items = Context.query.filter_by(kind='Landmark').all()
return success([x.to_hash() for x in items])
#
# Feedback
#
@app.route('/api/feedbacks')
@crossdomain(origin='*')
def api_feedbacks_list_all():
feedbacks = Feedback.query.all()
return success([x.to_hash() for x in feedbacks])
@app.route('/api/feedback/<id>')
@crossdomain(origin='*')
def api_feedback_get(id):
feedback = Feedback.query.get(id)
return success(feedback.to_hash())
@app.route('/api/feedback/<id>/update', methods = ['POST'])
def api_feedback_update(id):
obj = request.form
username = obj.get('username', '')
account = Account.query.filter_by(username=username).first()
feedback = Feedback.query.get(id)
if feedback and account:
feedback.content = obj.get('content', feedback.content)
feedback.kind = obj.get('kind', feedback.kind)
feedback.modified_at = datetime.now()
db.session.commit()
return success(feedback.to_hash())
return error("some parameters are missing")
@app.route('/api/feedback/new/<kind>/for/<model>/<id>/by/<username>',
methods = ['POST', 'GET'])
def api_feedback_add_to_note(kind,model,id,username):
if request.method == 'POST':
account = Account.query.filter_by(username=username).first()
target = Feedback.resolve_target(model,id)
print "adding feedback [%s] about [%s]" % (kind, target)
if target and account:
content = request.form.get('content','')
table_name = target.__class__.__name__
row_id = id
feedback = Feedback(account.id, kind, content, table_name, row_id)
db.session.add(feedback)
db.session.commit()
return success(feedback.to_hash())
return error("something wrong")
else:
return error("add feedback to note [%s] by [%s], this operation must be done through a post" %
(id, username))
@app.route('/api/media/<id>/feedback/<username>/new/comment',
methods = ['POST','GET'])
def api_feedback_add_to_media(id,username):
if request.method == 'POST':
media = Media.query.get(id)
account = Account.query.filter_by(username=username).first()
if media and account and 'content' in request.form:
kind = "Comment"
content = request.form['content']
table_name = "Media"
row_id = id
feedback = Feedback(account.id, kind, content, table_name, row_id)
db.session.add(feedback)
db.session.commit()
return success(feedback.to_hash())
return success({'success': False})
else:
return error("add feedback to media [%s] by [%s], this operation must be done through a post" %
(id, username))
#
# Site
#
@app.route('/api/site/<name>')
@crossdomain(origin='*')
def api_site_get(name):
site = Site.query.filter_by(name=name).first()
if site:
return success(site.to_hash())
else:
return error("site does not exist")
@app.route('/api/site/<name>/long')
@crossdomain(origin='*')
def api_site_get_long(name):
site = Site.query.filter_by(name=name).first()
if site:
h = site.to_hash()
h['contexts'] = [c.to_hash() for c in site.contexts]
return success(h)
else:
return error("site does not exist")
@app.route('/api/site/<name>/notes')
@crossdomain(origin='*')
def api_site_get_notes(name):
site = Site.query.filter_by(name=name).first()
if site:
notes = []
for c in site.contexts:
notes += c.notes
return success([x.to_hash() for x in notes])
else:
return error("site does not exist")
@app.route('/api/site/<name>/notes/<username>')
@crossdomain(origin='*')
def api_site_get_notes_user(name,username):
site = Site.query.filter_by(name=name).first()
account = Account.query.filter_by(username=username).first()
if site and account:
all_notes = []
for c in site.contexts:
notes = Note.query.filter_by(account_id=account.id, context_id=c.id).all()
all_notes += notes
return success([x.to_hash() for x in all_notes])
else:
return error("site does not exist")
@app.route('/api/sites')
@crossdomain(origin='*')
def api_site_list():
sites = Site.query.all()
return success([x.to_hash() for x in sites])
@app.route('/api/site/<name>/contexts')
@crossdomain(origin='*')
def api_site_list_contexts(name):
site = Site.query.filter_by(name=name).first()
if site:
return success([x.to_hash() for x in site.contexts])
else:
return error("site does not exist")
#
# Sync
#
@app.route('/api/sync/accounts/created/since/<year>/<month>/<date>/<hour>/<minute>')
def api_sync_account_since_minute(year,month,date,hour,minute):
since_date = datetime(int(year),int(month),int(date),int(hour),int(minute))
accounts = Account.query.filter(Account.created_at >= since_date).all()
return sync_success([x.to_hash() for x in accounts])
@app.route('/api/sync/accounts/created/since/<year>/<month>/<date>/<hour>/<minute>/at/<site>')
def api_sync_site_account_since_minute(site,year,month,date,hour,minute):
since_date = datetime(int(year),int(month),int(date),int(hour),int(minute))
accounts = Account.query.filter(Account.created_at >= since_date).all()
site = Site.query.filter_by(name=site).first()
site_accounts = []
for a in accounts:
if any(n.context.site.id == site.id for n in a.notes):
site_accounts.append(a)
return sync_success([x.to_hash() for x in site_accounts])
@app.route('/api/sync/notes/created/since/<year>/<month>/<date>/<hour>/<minute>')
def api_sync_notes_since_minute(year,month,date,hour,minute):
since_date = datetime(int(year),int(month),int(date),int(hour),int(minute))
notes = Note.query.filter(Note.created_at >= since_date).all()
return sync_success([x.to_hash() for x in notes])
@app.route('/api/sync/notes/created/since/<year>/<month>/<date>/<hour>/<minute>/at/<site>')
def api_sync_site_notes_since_minute(year,month,date,hour,minute,site):
since_date = datetime(int(year),int(month),int(date),int(hour),int(minute))
notes = Note.query.filter(Note.created_at >= since_date).all()
site = Site.query.filter_by(name=site).first()
context_ids = [c.id for c in site.contexts]
notes = [x for x in notes if x.context_id in context_ids]
return sync_success([x.to_hash() for x in notes])
@app.route('/api/sync/feedbacks/created/since/<year>/<month>/<date>/<hour>/<minute>/at/<site>')
def api_sync_site_feedback_since_minute(site,year,month,date,hour,minute):
since_date = datetime(int(year),int(month),int(date),int(hour),int(minute))
items = Feedback.query.filter(Feedback.created_at >= since_date).all()
site_items = []
for x in items:
if x.table_name == 'Note':
n = Note.query.get(x.row_id)
if n and n.context.site.name == site:
site_items.append(x)
elif x.table_name == 'Context':
c = Context.query.get(x.row_id)
if c and c.site.name == site:
site_items.append(x)
return sync_success([x.to_hash() for x in site_items])
@app.route('/api/sync/feedbacks/created/since/<year>/<month>/<date>/<hour>/<minute>')
def api_sync_feedback_since_minute(year,month,date,hour,minute):
since_date = datetime(int(year),int(month),int(date),int(hour),int(minute))
items = Feedback.query.filter(Feedback.created_at >= since_date).all()
return sync_success(add_timestamp_txt([x.to_hash() for x in items]))
@app.route('/api/sync/accounts/created/recent/<n>')
def api_sync_account_recent(n):
accounts = Account.query.filter().order_by(Account.created_at.desc()).limit(n)
return sync_success([x.to_hash() for x in accounts])
@app.route('/api/sync/notes/created/recent/<n>')
def api_sync_note_recent(n):
notes = Note.query.filter().order_by(Note.created_at.desc()).limit(n)
return sync_success([x.to_hash() for x in notes])
@app.route('/api/sync/feedbacks/created/recent/<n>')
def api_sync_feedback_recent(n):
feedbacks = Feedback.query.filter().order_by(Feedback.created_at.desc()).limit(n)
return sync_success([x.to_hash() for x in feedbacks])
def sync_success(x):
return success(add_timestamp_txt(x))
def add_timestamp_txt(items):
for item in items:
ts = item['created_at']
item['created_at_debug'] = ts.strftime('%Y/%m/%d/%H/%M')
return items
if __name__ == '__main__':
app.run(debug = True, host='0.0.0.0')