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 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.")