Ejemplo n.º 1
0
    def get(self, request, year, week_number, player_id):

        if self.__bad_year_or_week_number(year, week_number):
            data = {'year': year, 'week_number': week_number}
            return render(request, "pick10/bad_week.html", data, status=400)

        if not (self.__is_player_id_valid(player_id)):
            data = {'player_id': player_id, 'error': 'bad_id'}
            return render(request, "pick10/bad_player.html", data, status=400)

        year = int(year)
        week_number = int(week_number)
        player_id = int(player_id)
        d = Database()

        if not (self.__is_player_in_year(player_id, year)):
            data = {'year': year, 'player_id': player_id, 'error': 'bad_year'}
            return render(request, "pick10/bad_player.html", data, status=400)

        if d.is_week_being_setup(year, week_number):
            data = {'error': 'week_not_setup'}
            return render(request, "pick10/bad_player.html", data, status=400)

        access = UserAccess(request.user)

        # get the timezone for displaying the kickoff date and pick deadline
        if access.get_profile() == None:
            timezone = 'US/Eastern'
        else:
            timezone = access.get_profile().preferredtz

        # TODO tests
        if self.__hide_player_results(access, player_id, year, week_number):
            pick_deadline_utc = d.get_pick_deadline(year, week_number)
            pick_deadline = self.__format_pick_deadline(
                pick_deadline_utc, timezone)
            data = {
                'year': year,
                'player_id': player_id,
                'error': 'before_pick_deadline',
                'deadline': pick_deadline
            }
            return render(request, "pick10/bad_player.html", data)

        use_private_names = access.is_private_user()

        calc = CalculatePlayerResults(year, week_number, player_id,
                                      use_private_names)
        summary = calc.get_player_summary()
        results = calc.get_results()

        self.set_game_status_params(results, timezone)

        years_in_pool = sorted(d.get_years(), reverse=True)
        sidebar = render_to_string("pick10/year_sidebar.html", {
            'years_in_pool': years_in_pool,
            'year': year
        })

        params = dict()
        params['year'] = year
        params['week_number'] = week_number
        params['weeks_in_year'] = d.get_week_numbers(year)
        params['years_in_pool'] = years_in_pool
        params['summary'] = summary
        params['results'] = results
        params['FINAL'] = FINAL
        params['IN_PROGRESS'] = IN_PROGRESS
        params['NOT_STARTED'] = NOT_STARTED
        params['side_block_content'] = sidebar

        WeekNavbar(year, week_number, 'player_results',
                   request.user).add_parameters(params)

        return render(request, "pick10/player_results.html", params)
Ejemplo n.º 2
0
    def get(self,request,year,use_private_names=None,use_memcache=True): 
        # TODO how to get final pool state?

        year = int(year)
        loading_memcache = request == None
        d = Database()

        if not(d.is_year_valid(year)):
            assert not loading_memcache,"When loading memcache, year needs to be valid "
            data={'year':year}
            return render(request,"pick10/bad_year.html",data,status=400)

        if loading_memcache:
            access = None
        else:
            access = UserAccess(request.user)

        use_private_names = self.__determine_private_access(access,use_private_names)

        # setup memcache parameters
        if use_private_names:
            body_key = "overall_private_%d" % (year)
        else:
            body_key = "overall_public_%d" % (year)

        pool_state = d.get_pool_state(year)

        weeks_in_year = d.get_week_numbers(year)
        last_week_number = weeks_in_year[-1]

        years_in_pool = sorted(d.get_years(),reverse=True)
        sidebar = render_to_string("pick10/year_sidebar.html",{'years_in_pool':years_in_pool,'year':year})

        if pool_state == "week_setup":    # use last weeks results
            weeks_in_year.remove(last_week_number)
            last_week_number = last_week_number - 1

        # What if we need to go back a year?
        if last_week_number == 0:
            year -= 1
            pool_state = d.get_pool_state(year)
            weeks_in_year = d.get_week_numbers(year)
            last_week_number = weeks_in_year[-1]

        # look for hit in the memcache
        if not loading_memcache and use_memcache:
            body = cache.get(body_key)
            memcache_hit = body != None
            if memcache_hit:
                data = {'body_content':body,'side_block_content':sidebar,'year':year,'weeks_in_year':weeks_in_year }
                self.__set_player_id(access,data)
                WeekNavbar(year,last_week_number,'overall',request.user).add_parameters(data)
                return render(request,"pick10/overall_results.html",data)

        years_in_pool = sorted(d.get_years(),reverse=True)

        if pool_state == "not_started":
            players = d.load_players(year)

            data={'year':year, 'num_players':len(players)}
            body = render_to_string("pick10/overall_not_started.html",data)
            cache.set(body_key,body)

            if loading_memcache:
                return

            data = {'body_content':body,'side_block_content':sidebar,'year':year }
            WeekNavbar(year,last_week_number,'overall',request.user).add_parameters(data)
            return render(request,"pick10/overall_results.html",data)

        completed_games = calc_completed_games(year)
        self.cor = CalculateOverallResults(year,completed_games,use_private_names,use_weeks=weeks_in_year)
        results = self.cor.get_results()

        content_params = dict()
        content_params['year'] = year
        content_params['weeks_in_year'] = weeks_in_year
        content_params['pool_state'] = pool_state
        content_params['results'] = results
        content_params['last_week_number'] = last_week_number

        self.__render_file = "pick10/overall_results_combined.html"

        params = dict()
        params['year'] = year
        params['weeks_in_year'] = weeks_in_year
        params['years_in_pool'] = years_in_pool
        params['last_week_number'] = weeks_in_year[-1]
        params['pool_state'] = pool_state
        params['results'] = results
        params['content'] = self.__initial_content(content_params)
        params['sorted_by_overall'] = self.__sort_by_overall(content_params)
        params['sorted_by_overall_reversed'] = self.__sort_by_overall_reversed(content_params)
        params['sorted_by_players'] = self.__sort_by_players(content_params)
        params['sorted_by_players_reversed'] = self.__sort_by_players_reversed(content_params)

        if pool_state == "week_not_started":
            params['sorted_by_projected'] = ""
            params['sorted_by_projected_reversed'] = ""
            params['sorted_by_possible'] = self.__sort_by_overall_possible(content_params)
            params['sorted_by_possible_reversed'] = self.__sort_by_overall_possible_reversed(content_params)
        elif pool_state == "week_final":
            params['sorted_by_projected'] = ""
            params['sorted_by_projected_reversed'] = ""
            params['sorted_by_possible'] = self.__sort_by_overall_possible(content_params)
            params['sorted_by_possible_reversed'] = self.__sort_by_overall_possible_reversed(content_params)
        elif pool_state == "week_in_progress":
            params['sorted_by_projected'] = self.__sort_by_overall_projected(content_params)
            params['sorted_by_projected_reversed'] = self.__sort_by_overall_projected_reversed(content_params)
            params['sorted_by_possible'] = self.__sort_by_overall_possible(content_params)
            params['sorted_by_possible_reversed'] = self.__sort_by_overall_possible_reversed(content_params)
        else:
            params['sorted_by_projected'] = ""
            params['sorted_by_projected_reversed'] = ""
            params['sorted_by_possible'] = ""
            params['sorted_by_possible_reversed'] = ""

        body = render_to_string("pick10/overall_results_body.html",params)
        cache.set(body_key,body)

        if loading_memcache:
            return

        data = {'body_content':body,'side_block_content':sidebar,'year':year,'weeks_in_year':weeks_in_year }
        self.__set_player_id(access,data)
        WeekNavbar(year,last_week_number,'overall',request.user).add_parameters(data)
        return render(request,"pick10/overall_results.html",data)
    def post(self, request, year, week_number):

        if self.__bad_year_or_week_number(year, week_number):
            data = {'year': year, 'week_number': week_number}
            return render(request, "pick10/bad_week.html", data)

        d = Database()

        year = int(year)
        week_number = int(week_number)
        weeks_in_year = d.get_week_numbers(year)

        cancel_clicked = request.POST.get("cancel_form")
        if cancel_clicked:
            return redirect("week_results", year=year, week_number=week_number)

        if request.user.is_superuser:

            unlocked_clicked = request.POST.get("unlock_form")
            if unlocked_clicked:
                w = get_week(year, week_number)
                w.lock_scores = False
                w.save()
                return redirect("update_games",
                                year=year,
                                week_number=week_number)

            locked_clicked = request.POST.get("lock_form")
            if locked_clicked:
                w = get_week(year, week_number)
                w.lock_scores = True
                w.save()
                return redirect("update_games",
                                year=year,
                                week_number=week_number)

        submit_clicked = request.POST.get("submit_form")
        if not submit_clicked:
            errmsg = "Unexpected Error!  Expected submit button to be clicked but wasn't"
            return render(request, "pick10/error_message.html", message=errmsg)

        # check user/player privileges
        # superuser is allowed to access page no matter what
        access = UserAccess(request.user)

        if access.is_superuser() == False:

            # only private player can update scores
            if access.is_public_user():
                data = {'year': year, 'error': 'user_not_participant'}
                data['years_in_pool'] = sorted(d.get_years(), reverse=True)
                WeekNavbar(year, week_number, 'update_games',
                           request.user).add_parameters(data)
                return render(request, "pick10/update_games_error.html", data)

            # user's player not in the pool this year
            if access.is_player_in_year(year) == False:
                player_id = access.get_player().id
                data = {
                    'year': year,
                    'player_id': player_id,
                    'error': 'bad_year'
                }
                data['years_in_pool'] = sorted(d.get_years(), reverse=True)
                WeekNavbar(year, week_number, 'update_games',
                           request.user).add_parameters(data)
                return render(request, "pick10/update_games_error.html", data)

        if d.is_week_being_setup(year, week_number):
            data = {'year': year, 'error': 'week_not_setup'}
            data['years_in_pool'] = sorted(d.get_years(), reverse=True)
            WeekNavbar(year, week_number, 'update_games',
                       request.user).add_parameters(data)
            return render(request, "pick10/update_games_error.html", data)

        if self.__is_week_scores_locked(year, week_number):
            data = {
                'year': year,
                'week_number': week_number,
                'error': 'scores_locked'
            }
            data['years_in_pool'] = sorted(d.get_years(), reverse=True)
            WeekNavbar(year, week_number, 'update_games',
                       request.user).add_parameters(data)
            return render(response, "pick10/update_games_error.html", data)

        u = UpdateGames(year, week_number)
        week_games = u.get_games()

        try:
            for game_number in range(1, 11):
                input_data = self.__get_game_input_data(request, game_number)

                index = game_number - 1
                assert week_games[index].number == game_number

                week_games[index].team1_score = input_data['team1_score']
                week_games[index].team2_score = input_data['team2_score']
                week_games[index].quarter = input_data['quarter']
                week_games[index].time_left = input_data['time_left']
                week_games[index].state = input_data['state']

        except BadInputException as e:
            return render(request,
                          "pick10/error_message.html",
                          message=e.errmsg)

        u.update_games(week_games)
        return redirect("update_pages", year=year, week_number=week_number)
Ejemplo n.º 4
0
    def get(self,
            request,
            year,
            week_number,
            use_private_names=None,
            use_memcache=True):

        loading_memcache = request == None

        if self.__bad_year_or_week_number(year, week_number):
            assert not loading_memcache, "When loading memcache, year/week needs to be valid "
            data = {'year': year, 'week_number': week_number}
            return render(request, "pick10/bad_week.html", data, status=400)

        year = int(year)
        week_number = int(week_number)

        d = Database()

        if d.is_week_being_setup(year, week_number):
            assert not loading_memcache, "Loading memcache does not support week being setup"
            years_in_pool = sorted(d.get_years(), reverse=True)
            data = {
                'error': 'week_not_setup',
                'years_in_pool': years_in_pool,
                'year': year
            }
            WeekNavbar(year, week_number, 'tiebreak',
                       request.user).add_parameters(data)
            return render(request,
                          "pick10/tiebreak_error.html",
                          data,
                          status=400)

        if loading_memcache:
            use_private_names = use_private_names
        else:
            use_private_names = self.__determine_private_access(
                request.user, use_private_names)

        # setup memcache parameters
        if use_private_names:
            body_key = "tiebreak_private_%d_%d" % (year, week_number)
        else:
            body_key = "tiebreak_public_%d_%d" % (year, week_number)

        years_in_pool = sorted(d.get_years(), reverse=True)
        sidebar = render_to_string("pick10/year_sidebar.html", {
            'years_in_pool': years_in_pool,
            'year': year
        })

        # look for hit in the memcache
        if not loading_memcache and use_memcache:
            body = cache.get(body_key)
            memcache_hit = body != None
            if memcache_hit:
                data = {
                    'body_content': body,
                    'side_block_content': sidebar,
                    'week_number': week_number
                }
                WeekNavbar(year, week_number, 'tiebreak',
                           request.user).add_parameters(data)
                return render(request, "pick10/tiebreak.html", data)

        weeks_in_year = d.get_week_numbers(year)

        tiebreak = CalculateTiebreak(year, week_number, use_private_names)

        params = dict()
        params['year'] = year
        params['week_number'] = week_number
        params['weeks_in_year'] = weeks_in_year
        params['years_in_pool'] = years_in_pool
        params['winner_valid'] = tiebreak.was_able_to_determine_winner()
        params['featured_game_state'] = tiebreak.get_featured_game_state()
        params['summary'] = tiebreak.get_tiebreaker_summary()
        params['tiebreaker0_details'] = tiebreak.get_tiebreaker0_details()
        params['tiebreaker1_details'] = tiebreak.get_tiebreaker1_details()
        params['tiebreaker2_details'] = tiebreak.get_tiebreaker2_details()
        params['tiebreaker3_details'] = tiebreak.get_tiebreaker3_details()
        params['tiebreaker1_summary'] = tiebreak.get_tiebreaker1_summary()
        params['tiebreaker2_summary'] = tiebreak.get_tiebreaker2_summary()
        params['tiebreaker3_summary'] = tiebreak.get_tiebreaker3_summary()
        params['tiebreaker_required'] = params['summary'] != None and len(
            params['summary']) > 1
        params['tiebreaker0_valid'] = params[
            'tiebreaker0_details'] != None and len(
                params['tiebreaker0_details']) > 0
        params['tiebreaker1_valid'] = params[
            'tiebreaker1_details'] != None and len(
                params['tiebreaker1_details']) > 0
        params['tiebreaker2_valid'] = params[
            'tiebreaker2_details'] != None and len(
                params['tiebreaker2_details']) > 0
        params['tiebreaker3_valid'] = params[
            'tiebreaker3_details'] != None and len(
                params['tiebreaker3_details']) > 0
        params['NOT_STARTED'] = NOT_STARTED
        params['IN_PROGRESS'] = IN_PROGRESS
        params['FINAL'] = FINAL

        body = render_to_string("pick10/tiebreak_body.html", params)

        cache.set(body_key, body)
        if loading_memcache:
            return

        data = {
            'body_content': body,
            'side_block_content': sidebar,
            'week_number': week_number
        }
        WeekNavbar(year, week_number, 'tiebreak',
                   request.user).add_parameters(data)
        return render(request, "pick10/tiebreak.html", data)
    def get(self, request, year, week_number):

        if self.__bad_year_or_week_number(year, week_number):
            data = {'year': year, 'week_number': week_number}
            return render(request, "pick10/bad_week.html", data, status=400)

        access = UserAccess(request.user)

        d = Database()

        year = int(year)
        week_number = int(week_number)
        weeks_in_year = d.get_week_numbers(year)
        years_in_pool = sorted(d.get_years(), reverse=True)

        # check user/player privileges
        # superuser is allowed to access page no matter what
        if access.is_superuser() == False:

            # only private player can update scores
            if access.is_public_user():
                data = {
                    'year': year,
                    'error': 'user_not_participant',
                    'years_in_pool': years_in_pool
                }
                WeekNavbar(year, week_number, 'update_games',
                           request.user).add_parameters(data)
                return render(request, "pick10/update_games_error.html", data)

            # user's player not in the pool this year
            if access.is_player_in_year(year) == False:
                player_id = access.get_player().id
                data = {
                    'year': year,
                    'player_id': player_id,
                    'error': 'bad_year',
                    'years_in_pool': years_in_pool
                }
                WeekNavbar(year, week_number, 'update_games',
                           request.user).add_parameters(data)
                return render(request, "pick10/update_games_error.html", data)

        if d.is_week_being_setup(year, week_number):
            data = {
                'year': year,
                'error': 'week_not_setup',
                'years_in_pool': years_in_pool
            }
            WeekNavbar(year, week_number, 'update_games',
                       request.user).add_parameters(data)
            return render(request, "pick10/update_games_error.html", data)

        params = dict()
        params['year'] = year
        params['week_number'] = week_number
        params['weeks_in_year'] = weeks_in_year
        params['years_in_pool'] = years_in_pool
        params['games'] = UpdateGames(year, week_number).get_games()
        params['locked'] = self.__is_week_scores_locked(year, week_number)
        params['FINAL'] = FINAL
        params['IN_PROGRESS'] = IN_PROGRESS
        params['NOT_STARTED'] = NOT_STARTED

        WeekNavbar(year, week_number, 'update_games',
                   request.user).add_parameters(params)

        return render(request, "pick10/update_games.html", params)