def save_vote():
    '\n        Function accessed by AJAX from vote() to save the results of a Vote\n    '
    try:
        problem_id = request.args[0]
    except:
        raise HTTP(400)
    ptable = s3db.delphi_problem
    query = (ptable.id == problem_id)
    problem = db(query).select(ptable.group_id, limitby=(0, 1)).first()
    if (not problem):
        raise HTTP(404)
    duser = s3db.delphi_DelphiUser(problem.group_id)
    if (not duser.can_vote):
        auth.permission.fail()
    try:
        rankings = request.post_vars.keys()[0].split(',')
    except IndexError:
        status = current.xml.json_message(False, 400, 'No Options Ranked')
        raise HTTP(400, body=status)
    stable = s3db.delphi_solution
    query = (stable.problem_id == problem_id)
    solutions = db(query).select(stable.id)
    options = []
    for row in solutions:
        options.append(row.id)
    for ranked in rankings:
        if (int(ranked) not in options):
            status = current.xml.json_message(False, 400, "Option isn't valid!")
            raise HTTP(400, body=status)
    votes = []
    count = 1
    for ranked in rankings:
        votes.append(Storage(solution_id=int(ranked), rank=count))
        count += 1
    vtable = s3db.delphi_vote
    query = ((vtable.problem_id == problem_id) & (vtable.created_by == auth.user.id))
    old_votes = db(query).select(vtable.solution_id, vtable.rank)
    if old_votes:
        ranks = {}
        old_ranks = {}
        used = []
        for solution in solutions:
            s1 = solution.id
            ranks[s1] = 0
            old_ranks[s1] = 0
            for vote in votes:
                if (vote.solution_id == s1):
                    ranks[s1] = vote.rank
                    continue
            for vote in old_votes:
                if (vote.solution_id == s1):
                    old_ranks[s1] = vote.rank
                    continue
            for sol_2 in solutions:
                changed = False
                s2 = sol_2.id
                if (s2 == s1):
                    continue
                if ((s2, s1) in used):
                    continue
                ranks[s2] = 0
                old_ranks[s2] = 0
                for vote in votes:
                    if (vote.solution_id == s2):
                        ranks[s2] = vote.rank
                        continue
                for vote in old_votes:
                    if (vote.solution_id == s2):
                        old_ranks[s2] = vote.rank
                        continue
                if ((ranks[s1] > ranks[s2]) and (old_ranks[s1] < old_ranks[s2])):
                    changed = True
                elif ((ranks[s1] < ranks[s2]) and (old_ranks[s1] > old_ranks[s2])):
                    changed = True
                elif ((ranks[s1] == ranks[s2]) and (old_ranks[s1] != old_ranks[s2])):
                    changed = True
                elif ((ranks[s1] != ranks[s2]) and (old_ranks[s1] == old_ranks[s2])):
                    changed = True
                if changed:
                    db(stable.id.belongs((s1, s2))).update(changes=(stable.changes + 1))
                used.append((s1, s2))
    db(query).delete()
    count = 1
    for ranked in rankings:
        vtable.insert(problem_id=problem_id, solution_id=ranked, rank=count)
        count += 1
    status = current.xml.json_message(True, 200, 'Vote saved')
    return status
Function accessed by AJAX from vote() to save the results of a Vote
def save_vote():
    '\n        \n    '
    try:
        problem_id = request.args[0]
    except:
        raise HTTP(400)
    ptable = s3db.delphi_problem
    query = (ptable.id == problem_id)
    problem = db(query).select(ptable.group_id, limitby=(0, 1)).first()
    if (not problem):
        raise HTTP(404)
    duser = s3db.delphi_DelphiUser(problem.group_id)
    if (not duser.can_vote):
        auth.permission.fail()
    try:
        rankings = request.post_vars.keys()[0].split(',')
    except IndexError:
        status = current.xml.json_message(False, 400, 'No Options Ranked')
        raise HTTP(400, body=status)
    stable = s3db.delphi_solution
    query = (stable.problem_id == problem_id)
    solutions = db(query).select(stable.id)
    options = []
    for row in solutions:
        options.append(row.id)
    for ranked in rankings:
        if (int(ranked) not in options):
            status = current.xml.json_message(False, 400, "Option isn't valid!")
            raise HTTP(400, body=status)
    votes = []
    count = 1
    for ranked in rankings:
        votes.append(Storage(solution_id=int(ranked), rank=count))
        count += 1
    vtable = s3db.delphi_vote
    query = ((vtable.problem_id == problem_id) & (vtable.created_by == auth.user.id))
    old_votes = db(query).select(vtable.solution_id, vtable.rank)
    if old_votes:
        ranks = {}
        old_ranks = {}
        used = []
        for solution in solutions:
            s1 = solution.id
            ranks[s1] = 0
            old_ranks[s1] = 0
            for vote in votes:
                if (vote.solution_id == s1):
                    ranks[s1] = vote.rank
                    continue
            for vote in old_votes:
                if (vote.solution_id == s1):
                    old_ranks[s1] = vote.rank
                    continue
            for sol_2 in solutions:
                changed = False
                s2 = sol_2.id
                if (s2 == s1):
                    continue
                if ((s2, s1) in used):
                    continue
                ranks[s2] = 0
                old_ranks[s2] = 0
                for vote in votes:
                    if (vote.solution_id == s2):
                        ranks[s2] = vote.rank
                        continue
                for vote in old_votes:
                    if (vote.solution_id == s2):
                        old_ranks[s2] = vote.rank
                        continue
                if ((ranks[s1] > ranks[s2]) and (old_ranks[s1] < old_ranks[s2])):
                    changed = True
                elif ((ranks[s1] < ranks[s2]) and (old_ranks[s1] > old_ranks[s2])):
                    changed = True
                elif ((ranks[s1] == ranks[s2]) and (old_ranks[s1] != old_ranks[s2])):
                    changed = True
                elif ((ranks[s1] != ranks[s2]) and (old_ranks[s1] == old_ranks[s2])):
                    changed = True
                if changed:
                    db(stable.id.belongs((s1, s2))).update(changes=(stable.changes + 1))
                used.append((s1, s2))
    db(query).delete()
    count = 1
    for ranked in rankings:
        vtable.insert(problem_id=problem_id, solution_id=ranked, rank=count)
        count += 1
    status = current.xml.json_message(True, 200, 'Vote saved')
    return status