def remove_orders(environ, start_response):
    global currentTurn
    global cache
    print("--Removing Orders")
    
    if environ['REQUEST_METHOD'].lower() == 'post':
        postdata = parse_qs(environ['wsgi.input'].read())

        session = environ.get('session')
        if 'uid' in session:
            host, port, username, password, now = session['uid']
            """Fix so it doesn't have to call updateCache each time."""
            conn, cacher = middleman.updateCache(host, port, username, password)
            
            cache = middleman.Orders(cacher).removeOrder(conn, int(postdata['id'][0]), int(postdata['order_position'][0])) 
            
            turn = {'time': int(conn.time().time), 'current': int(conn.time().turn_num)}
            data = {'auth': True, 'removed': True, 'turn': turn}
        else:
            data = {'auth': False}

        output = json.dumps(data, encoding='utf-8', ensure_ascii=False)

        start_response('200 OK', [('Content-Type', 'application/json')])
        return [output]
def update_orders(environ, start_response):
    global currentTurn
    global cache
    print("--Updating Orders--")
    
    if environ['REQUEST_METHOD'].lower() == 'post':
        postdata = parse_qs(environ['wsgi.input'].read())

        session = environ.get('session')
        if 'uid' in session:
            host, port, username, password, now = session['uid']
            conn, cacher = middleman.updateCache(host, port, username, password)

            if 'args' in postdata:
                args = postdata['args']
            else:
                args = None
                
            cache = middleman.Orders(cacher).updateOrder(conn, int(postdata['id'][0]), int(postdata['type'][0]), int(postdata['order_position'][0]), args) 

            turn = {'time': int(conn.time().time), 'current': int(conn.time().turn_num)}
            data = {'auth': True, 'sent': True, 'turn': turn}
        else:
            data = {'auth': False}

        output = json.dumps(data, encoding='utf-8', ensure_ascii=False)

        start_response('200 OK', [('Content-Type', 'application/json')])
        return [output]
def cache_update(environ, start_response):
    """Update cache"""
    global currentTurn
    global cache
    
    print("--Updating Cache--")
    session = environ.get('session')
    
    if 'uid' in session:
        
        host, port, username, password, now = session['uid']
        conn, cache = middleman.updateCache(host, port, username, password)
        middleman.cacheObjectPrintout(cache)
        currentTurn = cache.objects[0].__Informational.Year.value    
        
        turn = {'time': int(conn.time().time), 'current': int(conn.time().turn_num)}
        data = {'auth': True, 'cache': True, 'turn': turn}
        
    else:
        data = {'auth': False}
    
    output = json.dumps(data, encoding='utf-8', ensure_ascii=False)
    
    start_response('200 OK', [('Content-Type', 'application/json')])
    print("Cache updated")
    return [output]
def get_orders(environ, start_response):
    global currentTurn
    global cache
    print("--Getting Orders--")
    
    session = environ.get('session')
    if 'uid' in session:
        #print "Printing empty cache", cache.orders
        host, port, username, password, now = session['uid']
        
        conn, cache = middleman.updateCache(host, port, username, password)
        
        turn = {'time': int(conn.time().time), 'current': int(conn.time().turn_num)}
        data = {'auth': True, 'orders': middleman.Orders(cache).build(), 'turn': turn}
    else:
        data = {'auth': False}

    output = json.dumps(data, encoding='utf-8', ensure_ascii=False)

    start_response('200 OK', [('Content-Type', 'application/json')])
    return [output]