def create(request): form = PlayerAddForm(request.POST or None) if request.method == 'POST': if form.is_valid(): form.save(commit=True) return redirect('Chess.apps.player.views.index') return render_to_response('player/create.html', {'form': form}, context_instance=RequestContext(request) )
def details_inactive(request, tournament_id): def get_existing_players_add_form(): form = ManyPlayersAddForm() form.fields['players'].queryset =\ Player.objects.exclude(signed_to_tournaments = tournament_id) return form sign_new_player_form = PlayerAddForm() sign_existing_players = get_existing_players_add_form() if request.method == 'POST': if 'button_sign_new' in request.POST: sign_new_player_form = PlayerAddForm(request.POST) if sign_new_player_form.is_valid(): player = sign_new_player_form.save() p_in_t = PlayersInTournament(player = player, tournament_id = tournament_id) p_in_t.save() if 'button_sign_existing' in request.POST: sign_existing_players = ManyPlayersAddForm(request.POST) sign_existing_players.fields['players'].queryset =\ Player.objects.exclude(signed_to_tournaments = tournament_id) if sign_existing_players.is_valid(): players = sign_existing_players.cleaned_data['players'] for player in players: if not PlayersInTournament.objects.filter( tournament_id=tournament_id, player=player).count(): p = PlayersInTournament(tournament_id = tournament_id, player = player) p.save() sign_existing_players = get_existing_players_add_form() info = get_object_or_404(Tournament, pk=tournament_id, active=False).get_inactive_info() return render_to_response('tournament/details_inactive.html', { 'info' : info, 'sign_new' : sign_new_player_form, 'sign_existing' : sign_existing_players }, context_instance=RequestContext(request) )