Source

omw / omw / omw.py

try:
    import json
except ImportError:
    import simplejson as json

from flask import Flask
app = Flask(__name__)

from flask import abort, render_template, request

import redis
r = redis.Redis(host='localhost')

from util import new_id

@app.route('/')
def hello_world():
    return render_template('index.html')

@app.route('/trip/<tid>/')
def trip(tid):
    trip = r.hgetall('trip:%s' % tid)
    if not trip:
        abort(404)
    
    locs = [map(float, l.split(','))
            for l in r.zrevrange('trip:%s:locs' % tid, 0, 1)]
    current = locs[0]
    return render_template('trip.html', trip=trip, current=current)


@app.route('/api/trip/', methods=['POST'])
def api_trip():
    tid = new_id()
    r.hset('trip:%s' % tid, 'description', request.json['description'])
    return json.dumps({'status': 'OK', 'tid': tid})

@app.route('/api/trip/<tid>/locs/', methods=['GET', 'POST'])
def api_trip_locs(tid):
    locs = r.zrevrange('trip:%s:locs' % tid, 0, 1)
    return json.dumps(list(reversed(locs)))


@app.route('/api/trip/<tid>/')
def api_trip_get(tid):
    pass


if __name__ == '__main__':
    from flaskext.lesscss import lesscss
    lesscss(app)

    app.debug = True
    app.run()