def simulate_goals_for_round_2(self): round_start = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 2"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 2"]["end"], "%Y-%m-%d").date() voting_end = datetime.date.today() + datetime.timedelta(days=1) self.stdout.write( "Simulating goal participation in the second round.\n") goal = EnergyGoal(start_date=round_start, end_date=round_end, voting_end_date=voting_end) goal.save() # Simulate votes. for profile in Profile.objects.all(): # Assume 1 in 5 users do not vote. if random.randint(0, 4) % 5 != 0: # User likely to vote between 0 and 25% reduction. value = random.randint(0, 5) * 5 vote = EnergyGoalVote(user=profile.user, goal=goal, percent_reduction=value) vote.save()
def simulate_goals_for_round_1(self): round_start = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 1"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 1"]["end"], "%Y-%m-%d").date() voting_end = round_start + datetime.timedelta(days=2) self.stdout.write("Simulating goal participation in the first round.\n") goal = EnergyGoal(start_date=round_start, end_date=round_end, voting_end_date=voting_end) goal.save() # Simulate votes. for profile in Profile.objects.all(): # Assume 1 in 5 users do not vote. if random.randint(0, 4) % 5 != 0: # User likely to vote between 0 and 25% reduction. value = random.randint(0, 5) * 5 vote = EnergyGoalVote(user=profile.user, goal=goal, percent_reduction=value) vote.save() # Generate floor energy goals. for floor in Floor.objects.all(): results = goal.get_floor_results(floor) percent_reduction = 0 if len(results) > 0: percent_reduction = results[0]["percent_reduction"] floor_goal = FloorEnergyGoal(floor=floor, goal=goal, percent_reduction=percent_reduction) # Assume 1 in 5 goals fail. if random.randint(0, 4) % 5 != 0: floor_goal.completed = True floor_goal.save()
class FloorEnergyGoalUnitTestCase(TestCase): fixtures = ["base_floors.json", "test_users.json"] def setUp(self): self.saved_rounds = settings.COMPETITION_ROUNDS self.current_round = "Round 1" start = datetime.date.today() - datetime.timedelta(days=7) end = start + datetime.timedelta(days=6) settings.COMPETITION_ROUNDS = { "Round 1" : { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } start = datetime.date.today() - datetime.timedelta(days=7) voting_end = start + datetime.timedelta(days=3) end = start + datetime.timedelta(days=6) self.goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, point_conversion=1.0, ) self.goal.save() def testRoundPoints(self): """Test that we can assign points to a round when the goal ends at the same time.""" user = User.objects.get(username="******") profile = user.get_profile() floor = profile.floor overall_points = profile.points entry, created = ScoreboardEntry.objects.get_or_create(profile=profile, round_name=self.current_round) round_points = entry.points floor_goal = FloorEnergyGoal(floor=floor, goal=self.goal, percent_reduction=10, completed=True) floor_goal.save() self.assertTrue(floor_goal.awarded, "Check that the goal was awarded.") test_profile = Profile.objects.get(user=user) self.assertEqual(test_profile.points, overall_points + 10, "Check that points are awarded overall.") entry = ScoreboardEntry.objects.get(profile=user.get_profile(), round_name=self.current_round) self.assertEqual(entry.points, round_points + 10, "Check that points are awarded for this round.") def tearDown(self): """Restore the saved settings.""" settings.COMPETITION_ROUNDS = self.saved_rounds
def testGetCurrentGoal(self): """Tests that we can retrieve the current goal.""" current_goal = EnergyGoal.get_current_goal() self.assertTrue(current_goal is None, "Check that there is no current goal.") start = datetime.date.today() - datetime.timedelta(days=1) voting_end = start + datetime.timedelta(days=3) end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() current_goal = EnergyGoal.get_current_goal() self.assertEqual(current_goal, goal, "Check that we can retrieve the current goal.")
def simulate_goals_for_round_2(self): round_start = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 2"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 2"]["end"], "%Y-%m-%d").date() voting_end = datetime.date.today() + datetime.timedelta(days=1) self.stdout.write("Simulating goal participation in the second round.\n") goal = EnergyGoal(start_date=round_start, end_date=round_end, voting_end_date=voting_end) goal.save() # Simulate votes. for profile in Profile.objects.all(): # Assume 1 in 5 users do not vote. if random.randint(0, 4) % 5 != 0: # User likely to vote between 0 and 25% reduction. value = random.randint(0, 5) * 5 vote = EnergyGoalVote(user=profile.user, goal=goal, percent_reduction=value) vote.save()
def simulate_goals_for_round_1(self): round_start = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 1"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 1"]["end"], "%Y-%m-%d").date() voting_end = round_start + datetime.timedelta(days=2) self.stdout.write( "Simulating goal participation in the first round.\n") goal = EnergyGoal(start_date=round_start, end_date=round_end, voting_end_date=voting_end) goal.save() # Simulate votes. for profile in Profile.objects.all(): # Assume 1 in 5 users do not vote. if random.randint(0, 4) % 5 != 0: # User likely to vote between 0 and 25% reduction. value = random.randint(0, 5) * 5 vote = EnergyGoalVote(user=profile.user, goal=goal, percent_reduction=value) vote.save() # Generate floor energy goals. for floor in Floor.objects.all(): results = goal.get_floor_results(floor) percent_reduction = 0 if len(results) > 0: percent_reduction = results[0]["percent_reduction"] floor_goal = FloorEnergyGoal(floor=floor, goal=goal, percent_reduction=percent_reduction) # Assume 1 in 5 goals fail. if random.randint(0, 4) % 5 != 0: floor_goal.completed = True floor_goal.save()
def testUserCanVote(self): """Tests that this is toggled when the user submits a vote.""" user = User(username="******", password="******") user.save() start = datetime.date.today() - datetime.timedelta(days=2) voting_end = start + datetime.timedelta(days=3) end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() self.assertTrue(goal.user_can_vote(user), "Check that the user has not submitted a vote.") vote = EnergyGoalVote(user=user, goal=goal, percent_reduction=10) vote.save() self.assertFalse(goal.user_can_vote(user), "Check that the user has now submitted a vote.")
def generate_floor_goals(): """Called by a cron task to generate the floor goals for a floor.""" goal = EnergyGoal.get_current_goal() today = datetime.date.today() if goal and goal.voting_end_date <= today and goal.floorenergygoal_set.count() == 0: # Go through the votes and create energy goals for the floor. for floor in Floor.objects.all(): results = goal.get_floor_results(floor) percent_reduction = 0 if len(results) > 0: percent_reduction = results[0]["percent_reduction"] floor_goal = FloorEnergyGoal(floor=floor, goal=goal, percent_reduction=percent_reduction) floor_goal.save()
def testGeneratingMultipleGoals(self): """ Tests that generate_floor_goals does not generate multiple goals and that multiple goals cannot be created. """ start = datetime.date.today() - datetime.timedelta(days=2) voting_end = datetime.date.today() end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() # Try executing twice to see if an exception occurs. generate_floor_goals() try: generate_floor_goals() except ValidationError: self.fail("generate_floor_goals should not cause an exception.") for floor in Floor.objects.all(): self.assertEqual(floor.floorenergygoal_set.count(), 1, "Check that there is only one goal.")
def testInVotingPeriod(self): """Tests that the in voting method works.""" start = datetime.date.today() - datetime.timedelta(days=2) voting_end = start + datetime.timedelta(days=3) end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() self.assertTrue(goal.in_voting_period(), "Check that the goal is currently in the voting period.") goal.voting_end_date = datetime.date.today() - datetime.timedelta(days=1) goal.save() self.assertFalse(goal.in_voting_period(), "Check that the goal is now not in the voting period.")
def generate_floor_goals(): """Called by a cron task to generate the floor goals for a floor.""" goal = EnergyGoal.get_current_goal() today = datetime.date.today() if goal and goal.voting_end_date <= today and goal.floorenergygoal_set.count( ) == 0: # Go through the votes and create energy goals for the floor. for floor in Floor.objects.all(): results = goal.get_floor_results(floor) percent_reduction = 0 if len(results) > 0: percent_reduction = results[0]["percent_reduction"] floor_goal = FloorEnergyGoal(floor=floor, goal=goal, percent_reduction=percent_reduction) floor_goal.save()
def handle(self, *args, **options): goal = EnergyGoal.get_current_goal() today = datetime.date.today() if goal and goal.voting_end_date <= today and goal.floorenergygoal_set.count() == 0: self.stdout.write("Generating goals for each floor.\n") # Go through the votes and create energy goals for the floor. for floor in Floor.objects.all(): results = goal.get_floor_results(floor) percent_reduction = 0 if len(results) > 0: percent_reduction = results[0]["percent_reduction"] floor_goal = FloorEnergyGoal(floor=floor, goal=goal, percent_reduction=percent_reduction) floor_goal.save() elif not goal: self.stdout.write("There is no goal to process.\n") else: self.stdout.write("The floor goals are already created.\n")
def handle(self, *args, **options): goal = EnergyGoal.get_current_goal() today = datetime.date.today() if goal and goal.voting_end_date <= today and goal.floorenergygoal_set.count( ) == 0: self.stdout.write("Generating goals for each floor.\n") # Go through the votes and create energy goals for the floor. for floor in Floor.objects.all(): results = goal.get_floor_results(floor) percent_reduction = 0 if len(results) > 0: percent_reduction = results[0]["percent_reduction"] floor_goal = FloorEnergyGoal( floor=floor, goal=goal, percent_reduction=percent_reduction) floor_goal.save() elif not goal: self.stdout.write("There is no goal to process.\n") else: self.stdout.write("The floor goals are already created.\n")
def setUp(self): self.saved_rounds = settings.COMPETITION_ROUNDS self.current_round = "Round 1" start = datetime.date.today() - datetime.timedelta(days=7) end = start + datetime.timedelta(days=6) settings.COMPETITION_ROUNDS = { "Round 1" : { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), }, } start = datetime.date.today() - datetime.timedelta(days=7) voting_end = start + datetime.timedelta(days=3) end = start + datetime.timedelta(days=6) self.goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, point_conversion=1.0, ) self.goal.save()
def get_info_for_user(user): """Generates a return dictionary for use in rendering the user profile.""" current_goal = EnergyGoal.get_current_goal() if current_goal: in_voting = current_goal.in_voting_period() can_vote = in_voting and current_goal.user_can_vote(user) if can_vote: form = EnergyGoalVotingForm(instance=EnergyGoalVote( user=user, goal=current_goal, percent_reduction=current_goal.default_goal)) return { "goal": current_goal, "form": form, } elif in_voting: profile = user.get_profile() results = current_goal.get_floor_results(profile.floor) results_url = generate_chart_url(results) return { "goal": current_goal, "results_url": results_url, } else: floor = user.get_profile().floor try: floor_goal = floor.floorenergygoal_set.get(goal=current_goal) return { "goal": current_goal, "floor_goal": floor_goal, } except FloorEnergyGoal.DoesNotExist: pass return None
def get_info_for_user(user): """Generates a return dictionary for use in rendering the user profile.""" current_goal = EnergyGoal.get_current_goal() if current_goal: in_voting = current_goal.in_voting_period() can_vote = in_voting and current_goal.user_can_vote(user) if can_vote: form = EnergyGoalVotingForm( instance=EnergyGoalVote(user=user, goal=current_goal, percent_reduction=current_goal.default_goal) ) return { "goal": current_goal, "form": form, } elif in_voting: profile = user.get_profile() results = current_goal.get_floor_results(profile.floor) results_url = generate_chart_url(results) return { "goal": current_goal, "results_url": results_url, } else: floor = user.get_profile().floor try: floor_goal = floor.floorenergygoal_set.get(goal=current_goal) return { "goal": current_goal, "floor_goal": floor_goal, } except FloorEnergyGoal.DoesNotExist: pass return None
def testGenerateFloorGoals(self): """Tests the generation of floor goals.""" generate_floor_goals() for floor in Floor.objects.all(): self.assertEqual(floor.floorenergygoal_set.count(), 0, "Test that nothing happens if there is no goal.") start = datetime.date.today() - datetime.timedelta(days=2) voting_end = datetime.date.today() + datetime.timedelta(days=1) end = start + datetime.timedelta(days=7) goal = EnergyGoal( start_date=start, voting_end_date=voting_end, end_date=end, ) goal.save() # Test that this goal does not generate any floor goals because the voting period is not up. generate_floor_goals() for floor in Floor.objects.all(): self.assertEqual(floor.floorenergygoal_set.count(), 0, "Test that no goals are created before the voting end date.") goal.voting_end_date = datetime.date.today() goal.save() # Create a test vote for a user. user = User.objects.get(username="******") vote = EnergyGoalVote(user=user, goal=goal, percent_reduction=10) vote.save() # Generate floor goals. generate_floor_goals() for floor in Floor.objects.all(): floor_goal = FloorEnergyGoal.objects.get(floor=floor, goal=goal) # Our test user's floor should have a 10 percent reduction goal. if floor == user.get_profile().floor: self.assertEqual(floor_goal.percent_reduction, 10, "Check that test user's vote counts.") else: self.assertEqual(floor_goal.percent_reduction, 0, "Check that default goal is 0.")