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 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 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 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