#!/usr/bin/env python
# more polymorphic, less neanderthal

import sqlite3
from flask import Flask, request, make_response
import json
import time

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def slash():
    if request.method == 'POST':
        response = post()
    elif request.method == 'GET':
        response = get()

    response = make_response(response)
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
    response.headers['Access-Control-Max-Age'] = 86400
    return response


def get():
    # need osc path, use same 'osc' in request.args from post()
    db = request.args['db']

    conn = sqlite3.connect(db + '.sqlite')
    if 'id' in request.args:
        row = conn.execute(
            'SELECT geo FROM osmly WHERE id = ? LIMIT 1',
            [request.args['id']]
        )
    else:
        row = conn.execute(
            'SELECT geo FROM osmly WHERE problem="" AND done="" ORDER BY RANDOM() LIMIT 1'
        )
    row = row.fetchone()
    conn.commit()
    conn.close()
    return row


def post():
    # how do you execute a function named by a variable?
    # map .args['action'] directly to functions here rather than a growing list
    # request.args['action'] = 'problem' etc...
    # request.args['action']() -> problem()
    if 'action' in request.args:
        if request.args['action'] == 'problem':
            return problem()
        elif request.args['action'] == 'osc':
            return post_osc()
    else:
        return done()


def done():
    db = request.args['db']
    id = request.args['id']

    conn = sqlite3.connect(db + '.sqlite')
    c = conn.cursor()
    c.execute('UPDATE osmly SET done = ? WHERE id = ?', (log(), id))
    conn.commit()
    conn.close()
    return json.dumps({'id': request.args['id']})


def problem():
    db = request.args['db']
    id = request.args['id']
    problem = request.form['problem']

    conn = sqlite3.connect(db + '.sqlite')
    c = conn.cursor()
    c.execute(
        'UPDATE osmly SET problem = ?, done = ? WHERE id = ?',
        (problem, log(), id)
    )
    conn.commit()
    conn.close()
    return json.dumps({'id': request.args['id']})


def post_osc():
    # could do a uid check if needed
    db = request.args['db']
    id = request.args['id']
    osc = request.form['osc']
    print osc

    conn = sqlite3.connect(db + '.sqlite')
    c = conn.cursor()
    c.execute('UPDATE osmly SET osc = ? WHERE id = ?', (osc, id))
    conn.commit()
    conn.close()
    return json.dumps({'id': 'ugg'})


def log():
    if 'user' in request.form:
        user = request.form['user']
    else:
        user = -1
    return json.dumps({
        'user': user,
        'time': int(time.time())
    })


if __name__ == '__main__':
    app.run(debug=True)

@@ -667,37 +667,8 @@
 t()%0A
-    db = request.args%5B'db'%5D%0A%0A
     
@@ -682,34 +682,50 @@
 sqlite3.connect(
-db
+request.args%5B'db'%5D
  + '.sqlite')%0A  
@@ -1573,65 +1573,8 @@
 ():%0A
-    db = request.args%5B'db'%5D%0A    id = request.args%5B'id'%5D%0A%0A
     
@@ -1588,34 +1588,50 @@
 sqlite3.connect(
-db
+request.args%5B'db'%5D
  + '.sqlite')%0A  
@@ -1656,32 +1656,41 @@
 )%0A    c.execute(
+%0A        
 'UPDATE osmly SE
@@ -1714,16 +1714,24 @@
 id = ?',
+%0A       
  (log(),
@@ -1731,19 +1731,40 @@
 (log(), 
-id)
+request.args%5B'id'%5D)%0A    
 )%0A    co
@@ -1863,103 +1863,8 @@
 ():%0A
-    db = request.args%5B'db'%5D%0A    id = request.args%5B'id'%5D%0A    problem = request.form%5B'problem'%5D%0A%0A
     
@@ -1878,34 +1878,50 @@
 sqlite3.connect(
-db
+request.args%5B'db'%5D
  + '.sqlite')%0A  
@@ -2027,23 +2027,39 @@
        (
+request.form%5B'
 problem
+'%5D
 , log(),
@@ -2059,18 +2059,34 @@
  log(), 
-id
+request.args%5B'id'%5D
 )%0A    )%0A
@@ -2229,109 +2229,8 @@
 ded%0A
-    db = request.args%5B'db'%5D%0A    id = request.args%5B'id'%5D%0A    osc = request.form%5B'osc'%5D%0A    print osc%0A%0A
     
@@ -2252,18 +2252,34 @@
 connect(
-db
+request.args%5B'db'%5D
  + '.sql
@@ -2320,16 +2320,25 @@
 execute(
+%0A        
 'UPDATE 
@@ -2373,18 +2373,63 @@
  ?',
- (osc, id)
+%0A        (request.form%5B'osc'%5D, request.args%5B'id'%5D)%0A    
 )%0A  

Remove temporary vars