def get(self, table_id=None):
        if not table_id:
            return "You need to have a table_id"

        try:
            table = Table.get_by_id(int(table_id))
            if not table:
                raise Exception("No table retrieved")
        except Exception as e:
            logging.exception(e)
            return "invalid table_id specified"

        result = ""
        if table.hasResolved:
            for participant in table.users:
                if participant.user == users.get_current_user().email():
                    result = participant.result

        return {
            'name': table.name,
            'participants': table.users,
            'resolved': table.hasResolved,
            'table_id': table_id,
            'result': result
        }
    def get(self, table_id=None):
        if not table_id:
            return "You need to have a table_id"

        try:
            table = Table.get_by_id(int(table_id))
            if not table:
                raise Exception("No table retrieved")
        except Exception as e:
            logging.exception(e)
            return "invalid table_id specified"

        if table.hasResolved:
            self.redirect_to('details',table_id=table_id)

        return {
            'name': table.name
        }
    def post(self, table_id=None):
        if not table_id:
            self.response.write("You need to have a table_id")
            return

        try:
            table = Table.get_by_id(int(table_id))
            if not table:
                raise Exception("No table retrieved")
        except Exception as e:
            logging.exception(e)
            self.response.write("invalid table_id specified")
            return 

        cash = int(self.request.get("cash", None))
        credit = self.request.get("credit", None)
        everything = self.request.get("everything", None)
        cost = int(self.request.get("cost", None))

        if not cash:
            self.response.write("You must specify how much cash you have")
            return 

        if not cost:
            self.response.write("You must specify how much your meal costed")
            return 

        @ndb.transactional
        def add_participant(table_key, cash, credit, everything, cost):
            table = table_key.get()
            table.users.append(Participant(
                user=users.get_current_user().email(),
                cash=cash,
                hasCredit=True if credit else False,
                wantsToPayForEverything=True if everything else False,
                costOfMeal=cost,
                result=""))
            table.put()

        add_participant(table.key, cash, credit, everything, cost)
                
        self.redirect_to('details',table_id=table_id)
    def get(self, table_id=None):
        if not table_id:
            return "You need to have a table_id"

        try:
            table = Table.get_by_id(int(table_id))
            if not table:
                raise Exception("No table retrieved")
        except Exception as e:
            logging.exception(e)
            return "invalid table_id specified"

        @ndb.transactional
        def resolve(table_key):
            table = table_key.get()
            
            # First, calculate the total cost:
            cost = 0
            for participant in table.users:
                cost += participant.costOfMeal

            # and the tip
            tip = cost * 0.2
            total_cost = tip + cost
            

            generous_users = []
            # Next, see if anyone wants to pay for everything
            for participant in table.users:
                if participant.wantsToPayForEverything:
                    if participant.hasCredit or participant.cash >= cost:
                        generous_users.append(participant.user)

            if len(generous_users) >= 2:
                random.shuffle(generous_users)
                cost_payer = generous_users[0]
                tip_payer = generous_users[1]

                for participant in table.users:
                    if participant.user == cost_payer:
                        participant.result = "You pay for everyone's meal. It costs {}".format(
                            cost)
                    elif participant.user == tip_payer:
                        participant.result = "You pay the tip. It costs {}".format(
                            tip)
                    else:
                        participant.result = "You don't have to pay. Thanks, {} and {}!".format(
                            cost_payer,
                            tip_payer)

            elif len(generous_users) == 1:
                random.shuffle(generous_users)
                cost_payer = generous_users[0]

                for participant in table.users:
                    if participant.user == cost_payer:
                        participant.result = "You pay for everyone's meal and the tip. It costs {}".format(
                            total_cost)
                    else:
                        participant.result = "You don't have to pay. Thanks, {}!".format(
                            cost_payer)

            else:
                for participant in table.users:
                    if participant.cash > participant.costOfMeal:
                        participant.result = "You pay for your own meal with cash"
                    elif participant.hasCredit:
                        participant.result = "You pay for your own meal with credit"
                    else:
                        participant.result = "You're broke! You end up doing dishes all night."

            table.hasResolved = True
            table.put()

        resolve(table.key)
        self.redirect_to('details',table_id=table_id)