Example #1
0
def edit_league(request, league_url):
    """
    Edit league view
    """
    
    league_name = decode_url(league_url)
    league = get_object_or_404(League, name=league_name)

    league.url = encode_url(league.name)
    
    # If form submitted
    if request.method == 'POST':

        # Delete League
        if request.POST.has_key('delete'):
            league.delete()

        # Autodraft players in to league
        elif request.POST.has_key('autodraft'):
            # Get unassigned players an all teams
            players = Player.objects.filter(league=league, team=None)
            teams = league.team_set.all()
            
            # Assign all unassigned players to a team
            num_teams = teams.count()
             
            if num_teams > 0:
                team_index = 0
                for player in players:
                    if team_index >= num_teams:
                        team_index = 0
                    player.team = teams[team_index]
                    player.save()
                    team_index += 1

        # Update League with new information
        else:
            league_form = LeagueForm(request.POST, instance=league)

            if league_form.is_valid():
                # Process user update
                league = league_form.save()
                league.save()

        return HttpResponseRedirect('/layup/manage/')

    # Accessing create league page for first time
    else:
        league_form = LeagueForm(instance=league)

    # Create context dict for page rendering
    context = {
            'league_form': league_form,
            'league': league,
        }

    return render(request, 'layup/edit_league.html', context)
Example #2
0
def create_league(request):
    """
    Create new league view
    """

    # If form submitted
    if request.method =='POST':
        league_form = LeagueForm(request.POST)

        if league_form.is_valid():
            # Process user update
            league = league_form.save()
            league.save()

            # Get number of teams to create
            num_teams = int(request.POST['teams'])

            for team in xrange(num_teams):
                team_name = team_name_generator()
                league.team_set.create(name=team_name)
            
            return HttpResponseRedirect(
                    '/layup/league/%s/' % encode_url(league.name) 
                )

    # Accessing create league page for first time
    else:
        league_form = LeagueForm()

    # Create context dict for page rendering
    context = {
            'league_form': league_form,
            'player': player,
        }

    return render(request, 'layup/create_league.html', context)