def _deref_contacts(self):
        user = users.get_user()

        try:
            user.update({'contacts': mongo_utils.deref_list(db, user['contacts'])})
        except KeyError:
            user['contacts'] = []
            
        return user
 def GET(self):
     try:
         contacts = mongo_utils.deref_list(db, users.get_user()['contacts'])
     except KeyError:
         contacts = []
     return render('/contacts/index.html', contacts=contacts)
 def connected(self, user):
     try:
         connected = user in mongo_utils.deref_list(db, users.get_user()['contacts'])
     except KeyError:
         connected = False
     return connected
    def GET(self):
        user = users.get_user()
        trans, balance, people_owe_you, you_owe_people = transactions.get_transaction_details(db, user, values_as_nums=True)

        nodes = {user['username']:0}
        paths = {}

        contacts = mongo_utils.deref_list(db, user['contacts'])
        contact_debts = {user['username']: (people_owe_you, you_owe_people)}

        for contact in contacts:
            nodes[contact['username']] = len(nodes)
            trans, balance, people_owe_you, you_owe_people = transactions.get_transaction_details(db, contact, values_as_nums=True)
            contact_debts[contact['username']] = (people_owe_you, you_owe_people)

        #Do some crazy graph shit
        for person, (owe_you, you_owe)  in contact_debts.iteritems():
            for (username, (user, value)) in owe_you.iteritems():                
                if (nodes[username], nodes[person]) not in paths:
                    paths[(nodes[username], nodes[person])] = value
                    
            for (username, (user, value)) in you_owe.iteritems():
                if (nodes[person], nodes[username]) not in paths:
                    paths[(nodes[person], nodes[username])] = value

        print "NODES"
        print nodes

        #Get rid of current user
        radius = 40
        nodes_list = {0:(user['username'], (radius,radius))}
        del nodes[user['username']]
                    
        #Work out where to put stuff with the graphs.
        degree = (2 * math.pi)/len(nodes)
        coords = [(round(math.sin(degree*num)*radius),round(math.cos(degree*num)*radius)) for num in range(len(nodes))]
        coords = map(lambda (x,y): (x+radius, abs(y-radius)), coords)

        #Set up the node coords
        for n,(k,v) in enumerate(nodes.iteritems()):
            nodes_list[v] = (k,(coords[n]))

        print "NODES LIST"
        print nodes_list
        print "PATHS"
        print paths
        

        #Now replace the line mappings with the actual coords
        paths_list = []
        for (person, person2), value in paths.iteritems():                    
            print "PERSON"
            print person
            print "PERSON2"
            print person2
            print "PERSON 1 NODE:"
            print nodes_list[person]
            print "PERSON 2 NODE:"
            print nodes_list[person2]
            if nodes_list[person][1][1] < nodes_list[person2][1][1]:                
                top, bottom = person, person2
            else:
                if nodes_list[person][1][0] < nodes_list[person2][1][0]:
                    top, bottom = person, person2
                else:
                    top, bottom = person2, person

            start_coords = list(nodes_list[top][1])
            height = nodes_list[bottom][1][1] - nodes_list[top][1][1]
            width = abs(nodes_list[bottom][1][0] - nodes_list[top][1][0])
            up_or_down = 'down' if nodes_list[top][1][0] >= nodes_list[bottom][1][0] else 'up'

            paths_list.append((start_coords, height, width, up_or_down, value))                
            #pass

        print paths_list

        return render('network.html', nodes=nodes_list, paths=paths_list, radius=radius)