From 3d9ef1cbe4753c8066de86f8d041f6a4e8775bbb Mon Sep 17 00:00:00 2001 From: sipp11 Date: Tue, 16 Dec 2014 02:16:44 +0700 Subject: [PATCH] /movie/:id POST --- flasky.py | 66 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/flasky.py b/flasky.py index 5915141..4710a66 100644 --- a/flasky.py +++ b/flasky.py @@ -94,38 +94,80 @@ def movie_list(lang): return r -@app.route('/movie//', methods=['GET'], defaults={'lang': 'en'}) @app.route('/movie///', methods=['GET']) @crossdomain(origin='*') def movie_item(mid, lang): + ''' + Compatibility with #ShowtimesTH on Android as of v1.4 + ''' lang = lang if lang in ('en', 'th') else 'en' try: movie = db.movies.find_one({'tmdb_id': int(mid)}) except ValueError: movie = db.movies.find_one({'_id': bson.objectid.ObjectId(mid)}) + if not movie: movie = {} - for k in ('title', 'tagline', 'director', 'cast', 'storyline'): - if movie[k][lang]: - movie[k] = movie[k][lang] - else: - movie[k] = movie[k]['th' if lang == 'en' else lang] - try: - del movie['indexes'] - except KeyError: - pass - + else: + for k in ('title', 'tagline', 'director', 'cast', 'storyline'): + if movie[k][lang]: + movie[k] = movie[k][lang] + else: + movie[k] = movie[k]['th' if lang == 'en' else lang] + try: + del movie['indexes'] + except KeyError: + pass r = make_response(dumps(movie, default=miscObjHandler)) r.mimetype = 'application/json' return r +@app.route('/movie//', methods=['GET', 'POST']) +def raw_movie_item(mid): + if request.method == 'GET': + try: + movie = db.movies.find_one({'tmdb_id': int(mid)}) + except ValueError: + movie = db.movies.find_one({'_id': bson.objectid.ObjectId(mid)}) + + r = make_response(dumps(movie, default=miscObjHandler)) + r.mimetype = 'application/json' + return r + + ## This is POST -- updating with information provided + if not request.json: + abort(400) + try: + key = {'_id': bson.objectid.ObjectId(mid)} + prev = db.movies.find_one(key) + if not prev: + abort(500) + is_allowed = True + ## simple check if dict is smaller, then we don't update that + posting_keys = request.json.keys() + prev_keys = prev.keys() + missing_fields = set(prev_keys) - set(posting_keys) + is_allowed = False if missing_fields else True + if is_allowed: + result = db.movies.update(key, {'$set': request.json}, False) + else: + result = { # mimicking mongo update result for consistency + 'updatedExisting': False, 'ok': 0, 'nModified': 0, + 'error': "missing_fields:%s" % (','.join(missing_fields)), + } + r = make_response(dumps(result, default=miscObjHandler)) + r.mimetype = 'application/json' + return r + except bson.errors.InvalidId: + abort(500) + + @app.route('/groups/', methods=['GET']) @crossdomain(origin='*') def groups(): result = db.groups.find() known_groups = [] # ['sf', 'major', 'etc'] - print result.count() if result.count() == 0: for i in ('sf', 'major', 'etc'): db.groups.insert({'name': str(i)})