def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['teams'] = FantasyTeam.objects.filter( league_id=context['object'].id).order_by('-wins', 'name') scraper = Scraper() context['current_week'] = int( scraper.get_current_and_max_week( ScraperConstants.Men, datetime.datetime.now().year)['week']) context['max_week'] = int( scraper.get_current_and_max_week( ScraperConstants.Men, datetime.datetime.now().year)['max']) context['matchups'] = Matchup.objects.filter( team1__in=context['teams']) return context
def weekly_news(request): context = {} scraper = Scraper() context['current_week'] = int( scraper.get_current_and_max_week(ScraperConstants.Men, datetime.datetime.now().year)['week']) context['posts'] = Post.objects.filter( status=1, week=context['current_week']).order_by('-posted_at') context['lineup'] = 0 context['platform'] = 1 template_name = 'news/weekly_news.html' return render(request, 'news/weekly_news.html', context)
def remove_gymnast_from_roster(request, team_pk, gymnast_pk): scraper = Scraper() current_week = int(scraper.get_current_and_max_week(ScraperConstants.Men, datetime.datetime.now().year)['week']) gymnast = get_object_or_404(Gymnast, pk=gymnast_pk) team = get_object_or_404(FantasyTeam, pk=team_pk) team.roster.remove(gymnast) league = team.league lineups = LineUp.objects.filter(team=team, week=current_week) for lineup in lineups: lineup.gymnasts.remove(gymnast) league.drafted.remove(gymnast) return redirect('view_team', pk=team_pk)
def get_context_data(self, **kwargs): scraper = Scraper() context = super().get_context_data(**kwargs) if context['object'].team2.user == self.request.user: context['team1'] = context['object'].team2 context['team2'] = context['object'].team1 else: context['team1'] = context['object'].team1 context['team2'] = context['object'].team2 gymnasts = (Gymnast.objects.filter(LineUp__in=(LineUp.objects.filter(team=context['team1'], week=context['object'].week).all() | LineUp.objects.filter(team=context['team2'], week=context['object'].week).all())) | Gymnast.objects.filter(id__in=(context['team1'].roster.all() | context['team2'].roster.all()))).distinct() current_week = int(scraper.get_current_and_max_week(ScraperConstants.Men, datetime.datetime.now().year)['week']) context['current_week'] = current_week context['teams_competing'] = teams_competing_this_week() context['meet_started'] = {} #Could this be optimized? weeks = scraper.get_year_weeks(ScraperConstants.Men, datetime.datetime.now().year) date = [week for week in weeks if int(week['wk']) == int(context['object'].week)][0]['date'] schedule = scraper.get_schedule(ScraperConstants.Men, date) # Loops through every meet day this week for day in schedule: # Loops through every meet on day for meet in schedule[day]['meets']: # Loops through gymnasts for gymnast in gymnasts: #Could this be optimized? # Checks if gymnasts team is in this meet if gymnast.team in str(meet['home_teams']) or gymnast.team in str(meet['away_teams']): #Could this be optimized? # Checks if this is gymnasts first meet of week if gymnast.name not in context['meet_started']: # Meet start datetime meet_datetime = datetime.datetime.strptime(str(meet['d']) + " " + str(meet['time']), "%Y-%m-%d %H:%M:%S") # Current datetime (eastern because thats what RTN uses) now = datetime.datetime.now(timezone('US/Eastern')) if now.date() > meet_datetime.date(): context['meet_started'][gymnast.name] = True elif now.date() == meet_datetime.date(): if meet_datetime.time() != datetime.time(0, 0, 0): if now.time() > meet_datetime.time(): context['meet_started'][gymnast.name] = True else: context['meet_started'][gymnast.name] = False else: if now.time() >= datetime.time(12, 0, 0): context['meet_started'][gymnast.name] = True else: context['meet_started'][gymnast.name] = False else: context['meet_started'][gymnast.name] = False return context
def in_lineup_current_week(gymnast, team): scraper = Scraper() current_week = int( scraper.get_current_and_max_week(ScraperConstants.Men, datetime.now().year)['week']) return gymnast.LineUp.filter(week=current_week, team=team).exists()
def get_context_data(self, **kwargs): scraper = Scraper() context = super().get_context_data(**kwargs) context["roster"] = context["object"].roster.all() context['current_week'] = int( scraper.get_current_and_max_week( ScraperConstants.Men, datetime.datetime.now().year)['week']) context["lineups"] = LineUp.objects.filter( team=context['object'], week=context['current_week']).order_by('pk') context['teams_competing'] = teams_competing_this_week() context['meet_started'] = {} #Could this be optimized? weeks = scraper.get_year_weeks(ScraperConstants.Men, datetime.datetime.now().year) # Fixes index error once in post season try: date = [ week for week in weeks if int(week['wk']) == context['current_week'] ][0]['date'] except IndexError: return context schedule = scraper.get_schedule(ScraperConstants.Men, date) gymnasts = context["roster"] # Loops through every meet day this week for day in schedule: # Loops through every meet on day for meet in schedule[day]['meets']: # Loops through gymnasts for gymnast in gymnasts: #Could this be optimized? # Checks if gymnasts team is in this meet if gymnast.team in str( meet['home_teams']) or gymnast.team in str( meet['away_teams']): #Could this be optimized? # Checks if this is gymnasts first meet of week if gymnast.name not in context['meet_started']: # Meet start datetime meet_datetime = datetime.datetime.strptime( str(meet['d']) + " " + str(meet['time']), "%Y-%m-%d %H:%M:%S") # Current datetime (eastern because thats what RTN uses) now = datetime.datetime.now(timezone('US/Eastern')) if now.date() > meet_datetime.date(): context['meet_started'][gymnast.name] = True elif now.date() == meet_datetime.date(): if meet_datetime.time() != datetime.time( 0, 0, 0): if now.time() > meet_datetime.time(): context['meet_started'][ gymnast.name] = True else: context['meet_started'][ gymnast.name] = False else: if now.time() >= datetime.time(12, 0, 0): context['meet_started'][ gymnast.name] = True else: context['meet_started'][ gymnast.name] = False else: context['meet_started'][gymnast.name] = False return context
def receive(self, text_data): text_data_json = json.loads(text_data) gymnast_pk = text_data_json['gymnast_pk'] user = self.scope['user'] # Get league league = League.objects.filter(pk=self.league_pk).first() # Get user's team team = FantasyTeam.objects.filter(user=user, league=self.league_pk).first() # Get the position that is up to draft currently_drafting = league.currently_drafting # Check if user who send draft request is currently up if team.draft_position == currently_drafting and not league.draft_complete and league.draft_started: # Do something here with the gymnast_pk and the team gymnast = get_object_or_404(Gymnast, pk=gymnast_pk) if gymnast not in league.drafted.all() and len(team.roster.all()) < league.roster_size: team.roster.add(gymnast) league = team.league league.drafted.add(gymnast) # Snake draft, initially going down num_teams = len(FantasyTeam.objects.filter(league=self.league_pk)) if league.going_down: # If last person is drafting if league.currently_drafting == (num_teams - 1): # Give them another turn and start going up league.going_down = False else: league.currently_drafting = league.currently_drafting + 1 else: # If first person is drafting on way back up if league.currently_drafting == 0: # Give first person another chance and start going down league.going_down = True else: league.currently_drafting = league.currently_drafting - 1 league.save() if len(league.drafted.all()) == league.roster_size * num_teams: # Drafting is done scraper = Scraper() year = datetime.date.today().year num_weeks = int(scraper.get_current_and_max_week(ScraperConstants.Men, year)['max']) matchups = round_robin_matchups(num_teams, num_weeks) team_pks = [x.pk for x in list(FantasyTeam.objects.filter(league__pk=self.league_pk))] # Creates matchups for entire season for week in matchups: for matchup in matchups[week]: team1_pk = team_pks[matchup[0] - 1] team2_pk = team_pks[matchup[1] - 1] team1 = FantasyTeam.objects.filter(pk=team1_pk).first() team2 = FantasyTeam.objects.filter(pk=team2_pk).first() m = Matchup(team1=team1, team2=team2, league=league, week=week) m.save() # Creates lineups for entire season events = ['FX', 'PH', 'SR', 'VT', 'PB', 'HB'] for i in range(6): if not LineUp.objects.filter(team=team1, event=events[i], week=week).exists(): LineUp.objects.create(team=team1, event=events[i], week=week) if not LineUp.objects.filter(team=team2, event=events[i], week=week).exists(): LineUp.objects.create(team=team2, event=events[i], week=week) league.draft_complete = True async_to_sync(self.channel_layer.group_send)(self.draft_group, { 'type': 'draft_complete', }) # PERFORM CHECK AND AUTO DRAFT HERE league.save() # Send message to rest of draft group async_to_sync(self.channel_layer.group_send)(self.draft_group, { 'type': 'gymnast_drafted', 'gymnast_pk': gymnast_pk, 'gymnast_name': gymnast.name, 'team_pk': team.pk, 'team_name': team.name, 'ncaa_team_name': gymnast.team, 'position_currently_drafting': league.currently_drafting, }) else: print("DRAFTING ERROR") async_to_sync(self.channel_layer.group_send)(self.draft_group, { 'type': 'gymnast_draft_error', 'error': 'Gymnast has already been drafted' }) else: async_to_sync(self.channel_layer.group_send)(self.draft_group, { 'type': 'gymnast_draft_error', 'error': 'Not your turn to draft' })