def testAllocatedTicket(self): """ Test that allocated_ticket works. """ # Create a raffle prize. r = RoundSetting.objects.get(name="Round 1") prize = RafflePrize( title="Super prize!", description="A test prize", round=r, value=5, ) prize.save() # Test within context of a quest self.quest.unlock_conditions = "allocated_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest not in quests["available_quests"], "User should not be able to participate in this quest.") self.quest.unlock_conditions = "not allocated_ticket()" self.quest.completion_conditions = "allocated_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") self.quest.accept(self.user) # Add a raffle ticket and test that the user completed the quest. ticket = RaffleTicket(raffle_prize=prize, user=self.user) ticket.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def testTicketAllocation(self): """ Tests that a user can allocate a ticket. """ self.prize.round = RoundSetting.objects.get(name="Round 1") self.prize.save() profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Add a ticket to the prize self.assertEqual(RaffleTicket.available_tickets(self.user), 1, "User should have one raffle ticket.") self.prize.add_ticket(self.user) self.assertEqual(RaffleTicket.available_tickets(self.user), 0, "User should not have any raffle tickets.") self.assertEqual(self.prize.allocated_tickets(), 1, "1 ticket should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(self.user), 1, "1 ticket should be allocated by this user to this prize.") # Have another user add a ticket to the prize. user2 = User.objects.create_user("user2", "*****@*****.**", password="******") profile = user2.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Add a ticket to the prize self.prize.add_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 2, "2 tickets should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(user2), 1, "1 ticket should be allocated by this user to this prize.") # Add another ticket to the prize. profile.add_points(25, datetime.datetime.today(), "test") profile.save() self.prize.add_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 3, "3 tickets should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(user2), 2, "2 tickets should be allocated by this user to this prize.") # Remove a ticket from the prize. self.prize.remove_ticket(self.user) self.assertEqual(self.prize.allocated_tickets(), 2, "2 tickets should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(self.user), 0, "No tickets should be allocated by this user to this prize.") self.prize.remove_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 1, "1 ticket should be allocated to this prize.") self.assertEqual(self.prize.allocated_tickets(user2), 1, "1 ticket should be allocated by this user to this prize.")
def testAllocatedTicket(self): """ Test that allocated_ticket works. """ # Create a raffle prize. r = RoundSetting.objects.get(name="Round 1") prize = RafflePrize( title="Super prize!", description="A test prize", round=r, value=5, ) prize.save() # Test within context of a quest self.quest.unlock_conditions = "allocated_raffle_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue( self.quest not in quests["available_quests"], "User should not be able to participate in this quest.") self.quest.unlock_conditions = "not allocated_raffle_ticket()" self.quest.completion_conditions = "allocated_raffle_ticket()" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") self.quest.accept(self.user) # Add a raffle ticket and test that the user completed the quest. ticket = RaffleTicket(raffle_prize=prize, user=self.user) ticket.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def supply(request, page_name): """Supply the view_objects contents, which provides all raffle data.""" _ = page_name user = request.user today = datetime.datetime.today() current_round_info = challenge_mgr.get_round_info() if not current_round_info: # no in any round return {"today": today} deadline = current_round_info["end"] # Get the user's tickets. total_tickets = RaffleTicket.total_tickets(user) user_tickets = RaffleTicket.objects.filter( user=user).select_related("raffle_prize") allocated_tickets = user_tickets.count() available_tickets = total_tickets - allocated_tickets prizes = None if today < deadline: # Get the prizes for the raffle. prizes = RafflePrize.objects.filter( round__name=current_round_info["name"]).annotate( total_tickets=Count("raffleticket")).order_by("-value") for prize in prizes: prize.user_tickets = 0 for ticket in user_tickets: if ticket.raffle_prize == prize: prize.user_tickets += 1 return { "round_name": current_round_info["name"], "deadline": deadline, "today": today, "points_per_ticket": POINTS_PER_TICKET, "tickets": { "available": available_tickets, "total": total_tickets, "allocated": allocated_tickets, }, "prizes": prizes, }
def supply(request, page_name): """Supply the view_objects contents, which provides all raffle data.""" _ = page_name user = request.user today = datetime.datetime.today() current_round_info = challenge_mgr.get_round_info() if not current_round_info: # no in any round return {"today": today} deadline = current_round_info["end"] # Get the user's tickets. total_tickets = RaffleTicket.total_tickets(user) user_tickets = RaffleTicket.objects.filter(user=user).select_related("raffle_prize") allocated_tickets = user_tickets.count() available_tickets = total_tickets - allocated_tickets prizes = None if today < deadline: # Get the prizes for the raffle. prizes = RafflePrize.objects.filter( round__name=current_round_info["name"]).annotate( total_tickets=Count("raffleticket")).order_by("-value") for prize in prizes: prize.user_tickets = 0 for ticket in user_tickets: if ticket.raffle_prize == prize: prize.user_tickets += 1 return { "round_name": current_round_info["name"], "deadline": deadline, "today": today, "points_per_ticket": POINTS_PER_TICKET, "tickets": { "available": available_tickets, "total": total_tickets, "allocated": allocated_tickets, }, "prizes": prizes, }
def add_ticket(request, prize_id): """Adds a user's raffle ticket to the prize.""" if request.method == "POST": prize = get_object_or_404(RafflePrize, id=prize_id) user = request.user current_round_info = challenge_mgr.get_round_info() deadline = current_round_info["end"] in_deadline = datetime.datetime.today() <= deadline if RaffleTicket.available_tickets(user) > 0 and in_deadline: prize.add_ticket(user) return HttpResponseRedirect(reverse("win_index")) elif not in_deadline: messages.error(request, "The raffle for this round is over.") return HttpResponseRedirect(reverse("win_index")) else: messages.error(request, "Sorry, but you do not have any more tickets.") return HttpResponseRedirect(reverse("win_index")) raise Http404
def testTicketAllocation(self): """ Tests that a user can allocate a ticket. """ self.prize.round = RoundSetting.objects.get(name="Round 1") self.prize.save() profile = self.user.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Add a ticket to the prize self.assertEqual(RaffleTicket.available_tickets(self.user), 1, "User should have one raffle ticket.") self.prize.add_ticket(self.user) self.assertEqual(RaffleTicket.available_tickets(self.user), 0, "User should not have any raffle tickets.") self.assertEqual(self.prize.allocated_tickets(), 1, "1 ticket should be allocated to this prize.") self.assertEqual( self.prize.allocated_tickets(self.user), 1, "1 ticket should be allocated by this user to this prize.") # Have another user add a ticket to the prize. user2 = User.objects.create_user("user2", "*****@*****.**", password="******") profile = user2.get_profile() profile.add_points(25, datetime.datetime.today(), "test") profile.save() # Add a ticket to the prize self.prize.add_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 2, "2 tickets should be allocated to this prize.") self.assertEqual( self.prize.allocated_tickets(user2), 1, "1 ticket should be allocated by this user to this prize.") # Add another ticket to the prize. profile.add_points(25, datetime.datetime.today(), "test") profile.save() self.prize.add_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 3, "3 tickets should be allocated to this prize.") self.assertEqual( self.prize.allocated_tickets(user2), 2, "2 tickets should be allocated by this user to this prize.") # Remove a ticket from the prize. self.prize.remove_ticket(self.user) self.assertEqual(self.prize.allocated_tickets(), 2, "2 tickets should be allocated to this prize.") self.assertEqual( self.prize.allocated_tickets(self.user), 0, "No tickets should be allocated by this user to this prize.") self.prize.remove_ticket(user2) self.assertEqual(self.prize.allocated_tickets(), 1, "1 ticket should be allocated to this prize.") self.assertEqual( self.prize.allocated_tickets(user2), 1, "1 ticket should be allocated by this user to this prize.")