Browse Source

/movie/:id POST

master
sipp11 10 years ago
parent
commit
3d9ef1cbe4
  1. 46
      flasky.py

46
flasky.py

@ -94,17 +94,21 @@ def movie_list(lang):
return r return r
@app.route('/movie/<mid>/', methods=['GET'], defaults={'lang': 'en'})
@app.route('/movie/<mid>/<lang>/', methods=['GET']) @app.route('/movie/<mid>/<lang>/', methods=['GET'])
@crossdomain(origin='*') @crossdomain(origin='*')
def movie_item(mid, lang): def movie_item(mid, lang):
'''
Compatibility with #ShowtimesTH on Android as of v1.4
'''
lang = lang if lang in ('en', 'th') else 'en' lang = lang if lang in ('en', 'th') else 'en'
try: try:
movie = db.movies.find_one({'tmdb_id': int(mid)}) movie = db.movies.find_one({'tmdb_id': int(mid)})
except ValueError: except ValueError:
movie = db.movies.find_one({'_id': bson.objectid.ObjectId(mid)}) movie = db.movies.find_one({'_id': bson.objectid.ObjectId(mid)})
if not movie: if not movie:
movie = {} movie = {}
else:
for k in ('title', 'tagline', 'director', 'cast', 'storyline'): for k in ('title', 'tagline', 'director', 'cast', 'storyline'):
if movie[k][lang]: if movie[k][lang]:
movie[k] = movie[k][lang] movie[k] = movie[k][lang]
@ -114,18 +118,56 @@ def movie_item(mid, lang):
del movie['indexes'] del movie['indexes']
except KeyError: except KeyError:
pass pass
r = make_response(dumps(movie, default=miscObjHandler))
r.mimetype = 'application/json'
return r
@app.route('/movie/<mid>/', 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 = make_response(dumps(movie, default=miscObjHandler))
r.mimetype = 'application/json' r.mimetype = 'application/json'
return r 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']) @app.route('/groups/', methods=['GET'])
@crossdomain(origin='*') @crossdomain(origin='*')
def groups(): def groups():
result = db.groups.find() result = db.groups.find()
known_groups = [] # ['sf', 'major', 'etc'] known_groups = [] # ['sf', 'major', 'etc']
print result.count()
if result.count() == 0: if result.count() == 0:
for i in ('sf', 'major', 'etc'): for i in ('sf', 'major', 'etc'):
db.groups.insert({'name': str(i)}) db.groups.insert({'name': str(i)})

Loading…
Cancel
Save