Browse Source

Add /group/:id GET/POST to database

* init /theater/:id GET/POST
master
sipp11 10 years ago
parent
commit
5118fbb2ef
  1. 69
      flasky.py

69
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/<lang>/', 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/<mid>/', methods=['GET'])
@app.route('/movie/<mid>/', methods=['GET'], defaults={'lang': 'en'})
@app.route('/movie/<mid>/<lang>/', 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/<string:group_id>/', 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/<group>/', methods=['GET'])
@crossdomain(origin='*')
@ -155,6 +186,32 @@ def list_theaters(group=None):
return r
@app.route('/theater/<string:theater_id>/', 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/<group>/', methods=['GET'])
@app.route('/showtimes/<group>/<code>/', methods=['GET'])
@crossdomain(origin='*')

Loading…
Cancel
Save