Example #1
0
    def __show_update_games(self):
        d = Database()

        if self.user.is_superuser:
            return True

        user_not_linked_to_player = self.player_id == None

        if user_not_linked_to_player:
            return False

        if d.before_pick_deadline(self.year, self.week_number):
            return False

        if d.is_week_scores_locked(self.year, self.week_number):
            return False

        if d.is_week_being_setup(self.year, self.week_number):
            return False

        return True
Example #2
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)
    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 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)
    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)