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