def get(self): # inefficient databse query, figure out how to perform this query in # db. # remove all expired tickets yield remove_expired_tickets() showtimes = yield get_showtimes() reservations = yield get_reservations() showtime_map = {} # Tuple (normal_tickets, shitty_tickets) for reservation in reservations: ticket_id = reservation["showtime_id"] ticketTuple = showtime_map.get(ticket_id, (0, 0)) if reservation['is_shitty']: ticketTuple = (ticketTuple[0], ticketTuple[1]+1) else: ticketTuple = (ticketTuple[0]+1, ticketTuple[1]) showtime_map[ticket_id] = ticketTuple result = [] for showtime in showtimes: showid = showtime["id"] dateString = showtime["date_str"] if showid in showtime_map: available_tickets = \ showtime["max_normal_booking"]\ - showtime_map[showid][0] if available_tickets < 0: available_tickets = 0 available_shitty_tickets = \ showtime["max_shitty_booking"]\ - showtime_map[showid][1] if available_shitty_tickets < 0: available_shitty_tickets = 0 else: available_tickets = showtime["max_normal_booking"] available_shitty_tickets = showtime["max_shitty_booking"] result.append({ "id": showid, "date": dateString, "available_tickets": available_tickets, "shitty_tickets": available_shitty_tickets }) self.api_response(result)
def get(self): # inefficient databse query, figure out how to perform this query in # db. # remove all expired tickets yield remove_expired_tickets() showtimes = yield get_showtimes() reservations = yield get_reservations() showtime_map = {} for reservation in reservations: ticket_id = reservation["showtime_id"] if ticket_id in showtime_map: showtime_map[ticket_id] += 1 else: showtime_map[ticket_id] = 1 result = [] timezone = tz.gettz(CONFIG.get('timezone')) timeformat = "%A %d, %B - %I:%M%p" for showtime in showtimes: showid = showtime["id"] dateString = showtime["date"]. \ astimezone(timezone). \ strftime(timeformat) if showid in showtime_map: available_tickets = \ showtime["max_booking"] - showtime_map[showid] if available_tickets < 0: available_tickets = 0 else: available_tickets = showtime["max_booking"] result.append({ "id": showid, "date": dateString, "available_tickets": available_tickets, }) self.api_response(result)