def put(self):
        ticket_type = self.get_argument("type", "normal")
        showtime_id = self.get_argument("showtime_id", None)
        user_id = self.get_secure_cookie("user_id", None)

        if user_id is None:
            return self.error(403, "Must include the user cookie")

        # Now grab the reservation
        reservation = yield get_reservation_for_user(user_id)
        if reservation is None:
            return self.error(403, "There is no reservation for this account.")

        confirmation_code = str(uuid.uuid1())
        if ticket_type == "shitty":
            # Confirm a shitty ticket_type
            if reservation['showtime_id'] == showtime_id:
                yield confirm_ticket_reservation(
                    reservation['id'], confirmation_code, True)
            else:
                yield self.change_showtime(showtime_id, reservation,
                                           confirmation_code)
        else:
            # TODO: check the access_tokens, make sure we have enough.
            yield confirm_ticket_reservation(
                reservation['id'], confirmation_code, False)
        user = yield get_user(user_id)
        yield send_confirmation(user['email'], user['name'], confirmation_code)
        self.clear_cookie('user_id')
        self.api_response({'confirmation_code': confirmation_code})
    def put(self):
        ticket_type = self.get_argument("type", "normal")
        showtime_id = self.get_argument("showtime_id", None)
        user_id = self.get_secure_cookie("user_id", None)

        if user_id is None:
            return self.error(403, "Must include the user cookie")

        # Now grab the reservation
        reservation = yield get_reservation_for_user(user_id)
        if reservation is None:
            return self.error(403, "There is no reservation for this account.")

        confirmation_code = str(uuid.uuid1())
        if ticket_type == "shitty":
            # Confirm a shitty ticket_type
            if reservation['showtime_id'] == showtime_id:
                yield confirm_ticket_reservation(reservation['id'],
                                                 confirmation_code, True)
            else:
                yield self.change_showtime(showtime_id, reservation,
                                           confirmation_code)
        else:
            # TODO: check the access_tokens, make sure we have enough.
            yield confirm_ticket_reservation(reservation['id'],
                                             confirmation_code, False)
        user = yield get_user(user_id)
        yield send_confirmation(user['email'], user['name'], confirmation_code)
        self.clear_cookie('user_id')
        self.api_response({'confirmation_code': confirmation_code})
    def change_showtime(self, showtime_id, reservation, confirmation_code):
        showtime = yield get_showtime(showtime_id)
        if showtime is None:
            return self.error(404, "Showtime not found!")

        if not (yield self.isShowTimeAvailable(showtime, True)):
            return self.error(400, "Ticket is not available any more.")

        yield change_reservation_showtime(reservation['id'], showtime_id)

        yield confirm_ticket_reservation(reservation['id'], confirmation_code,
                                         True)
    def change_showtime(self, showtime_id, reservation, confirmation_code):
        showtime = yield get_showtime(showtime_id)
        if showtime is None:
            return self.error(404, "Showtime not found!")

        if not (yield self.isShowTimeAvailable(showtime, True)):
            return self.error(400, "Ticket is not available any more.")

        yield change_reservation_showtime(
            reservation['id'], showtime_id)

        yield confirm_ticket_reservation(
            reservation['id'], confirmation_code, True)