def do_register(request):
    if request.method == 'POST':
        username=request.POST['username']
        if User.objects.filter(username__exact=username):
            return render_to_response('index.html', {'template_name' : 'forms/login.html', 'error_message' : 'The username is already taken'})
        email=request.POST['email']
        try:
            validate_email(email)
        except ValidationError:
            return render_to_response('index.html', {'template_name' : 'forms/login.html', 'error_message' : 'Not a valid email'})
        password1=request.POST['password1']
        password2=request.POST['password2']
        team_name=request.POST['team_name']
        if password1 != password2:
            return render_to_response('index.html', {'template_name' : 'forms/login.html', 'error_message' : 'Passwords do not match'})
        User.objects.create_user(username, email, password1)
        user = authenticate(username=username, password=password1)
        if user is not None:
            new_team = Team(team_name=team_name, owner=user)
            new_team.save()
            registration_email(username, email, team_name)
            login(request, user)
            add_message_all(username + " joined your league!")
            return redirect('/')
    return render_to_response('index.html', {'template_name' : 'forms/login.html', 'error_message' : 'Could not register new user'})
def add_from_free_agent(request):
    player_pk = request.POST['player_pk']
    player = Player.objects.get(pk=player_pk)
    team = Team.objects.get(owner=request.user)
    success = add_player(player, team)
    if success:
        add_message_all(request.user.username + ' added ' + player.full_name)
        return raw_show_free_agents(request, 'Added ' + player.full_name)
    else:
        return raw_show_free_agents(request, 'Could not add player')
def do_drop_player(request):
    player_pk = request.POST['player_pk']
    player = Player.objects.get(pk=player_pk)
    success = drop_player(player)
    if success:
        add_message_all(request.user.username + ' dropped ' + player.full_name)
        error_message = "Dropped " + player.full_name
        return raw_myteam(request, error_message)
    else:
        error_message = "Cannot Drop " + player.full_name
        return raw_myteam(request, error_message)