From 5118fbb2ef01fe3694eb413c2f2acdfb0cddf154 Mon Sep 17 00:00:00 2001 From: sipp11 Date: Sun, 14 Dec 2014 05:45:56 +0700 Subject: [PATCH] Add /group/:id GET/POST to database * init /theater/:id GET/POST --- flasky.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/flasky.py b/flasky.py index 242e69c..73093f2 100644 --- a/flasky.py +++ b/flasky.py @@ -1,4 +1,4 @@ -from flask import Flask, make_response, request, current_app +from flask import Flask, make_response, request, current_app, abort from simplejson import dumps from pymongo import MongoClient, DESCENDING # ASCENDING import datetime @@ -67,10 +67,10 @@ def hello_world(): return 'This comes from Flask ^_^' -@app.route('/movies/', methods=['GET']) +@app.route('/movies/', methods=['GET'], defaults={'lang': 'en'}) @app.route('/movies//', methods=['GET']) @crossdomain(origin='*') -def movie_list(lang='en'): +def movie_list(lang): result = db.movies.find().sort('release_date', DESCENDING) movies = [] for i in result: @@ -94,10 +94,10 @@ def movie_list(lang='en'): return r -@app.route('/movie//', methods=['GET']) +@app.route('/movie//', methods=['GET'], defaults={'lang': 'en'}) @app.route('/movie///', methods=['GET']) @crossdomain(origin='*') -def movie_item(mid, lang='en'): +def movie_item(mid, lang): lang = lang if lang in ('en', 'th') else 'en' try: movie = db.movies.find_one({'tmdb_id': int(mid)}) @@ -123,7 +123,16 @@ def movie_item(mid, lang='en'): @app.route('/groups/', methods=['GET']) @crossdomain(origin='*') def groups(): - known_groups = ['sf', 'major'] + 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)}) + known_groups.append({'name': str(i)}) + for i in result: + known_groups.append(i) + json_dict = { 'meta': { 'total_count': len(known_groups) @@ -135,6 +144,28 @@ def groups(): return r +@app.route('/group//', methods=['GET', 'POST']) +@crossdomain(origin='*') +def group(group_id): + if request.method == 'GET': + result = db.groups.find_one({'_id': bson.objectid.ObjectId(group_id)}) + r = make_response(dumps(result, default=miscObjHandler)) + r.mimetype = 'application/json' + return r + + ## This is POST -- updating with information provided + if not request.json or not 'name' in request.json: + abort(400) + try: + key = {'_id': bson.objectid.ObjectId(group_id)} + result = db.groups.update(key, {'$set': request.json}, False) + r = make_response(dumps(result, default=miscObjHandler)) + r.mimetype = 'application/json' + return r + except bson.errors.InvalidId: + abort(500) + + @app.route('/theaters/', methods=['GET']) @app.route('/theaters//', methods=['GET']) @crossdomain(origin='*') @@ -155,6 +186,32 @@ def list_theaters(group=None): return r +@app.route('/theater//', methods=['GET', 'POST']) +@crossdomain(origin='*') +def get_theater(theater_id): + if request.method == 'GET': + result = db.theater.find_one({'_id': bson.objectid.ObjectId(theater_id)}) + r = make_response(dumps(result, default=miscObjHandler)) + r.mimetype = 'application/json' + return r + + ## This is POST -- updating with information provided + if not request.json or \ + not 'name' in request.json or \ + not 'group' in request.json or not 'code' in request.json: + abort(400) + try: + key = {'_id': bson.objectid.ObjectId(theater_id)} + ## TODO: better verify if group/code is still the same + # otherwise things could get worse w/o any proof before updating + result = db.theater.update(key, {'$set': request.json}, False) + r = make_response(dumps(result, default=miscObjHandler)) + r.mimetype = 'application/json' + return r + except bson.errors.InvalidId: + abort(500) + + @app.route('/showtimes//', methods=['GET']) @app.route('/showtimes///', methods=['GET']) @crossdomain(origin='*')