Ejemplo n.º 1
0
def register(request):
    if request.method == 'GET':
        player_form = PlayerForm()
        user_form = UserForm()
        context = {'player_form': player_form, 'user_form': user_form}
    elif request.method == 'POST':
        player_form = PlayerForm(request.POST)
        user_form = UserForm(request.POST)
        if player_form.is_valid() and user_form.is_valid():
            user = user_form.save()
            player = Player.objects.create(name=player_form.cleaned_data['name'], age=player_form.cleaned_data['age'],
                                           position=player_form.cleaned_data['position'],
                                           about=player_form.cleaned_data['about'], user=user)
            for league_id in request.POST.getlist('leagues'):
                league = League.objects.get(pk=league_id)
                player.register_for_league(league)

            return HttpResponseRedirect(reverse('basketball:login'))
        else:
            context = {'player_form': player_form, 'user_form': user_form}

    return render(request, 'basketball/register.html', context)
Ejemplo n.º 2
0
def edit_player_info(request, player_id):
    player = get_object_or_404(Player, pk=player_id)
    user = request.user

    if not user.is_superuser and (not user.player or user.player.pk != player.pk):
        return HttpResponse('Unauthorized', status=401)

    player_form = PlayerForm(instance=player)
    if request.method == 'POST':
        player_form = PlayerForm(request.POST, instance=player)
        if player_form.is_valid():
            player = player_form.save(commit=False)
            player.leaguemembership_set.filter(league__signup_allowed=True, league__is_active=True).delete()
            for league_id in request.POST.getlist('leagues'):
                league = League.objects.get(pk=league_id)
                player.register_for_league(league)
            player.save()

            return HttpResponseRedirect(reverse('basketball:player-info', args=[player.pk]))

    context = {'player_form': player_form, 'player': player}
    return render(request, 'basketball/edit_player.html', context)