def single_ladder_details(request, ladder): """Retrieve info on a single ladder.""" # get the raw ranking list rank_list = Rank.objects.filter(ladder = ladder).order_by('rank') # set a flag to allow them to join the ladder join_link = None leave_link = None # if user is logged in if request.user.is_authenticated(): if ladder.max_players > ladder.players or ladder.max_players == 0 and request.user.pk not in [key.player.pk for key in rank_list]: join_link = True if request.user.pk in [key.player.pk for key in rank_list]: leave_link = True # if there is a ranking, get a list of those you can challenge. try: current_player_rank = Rank.objects.get(player = request.user, ladder = ladder) challengables = _get_valid_targets(request.user, current_player_rank, rank_list, ladder) messages.debug(request, "Challengable ranks: {0}".format(challengables)) open_challenges_exist = _open_challenges_exist(request.user, ladder) except ObjectDoesNotExist: open_challenges_exist = False current_player_rank = None challengables = [] else: open_challenges_exist = False current_player_rank = None join_link = False challengables = [] rank_list = [(r, _open_challenges_exist( r.player, ladder )) for r in rank_list] match_list = Match.objects.filter(ladder = ladder).order_by('-date_complete')[:25] open_challenges = Challenge.objects.filter(challenger = request.user.id, ladder = ladder).filter(accepted = 0).order_by('-deadline') return {'can_challenge':open_challenges_exist, 'challengables': challengables, 'current_player_rank':current_player_rank, 'leave_link':leave_link, 'join_link':join_link, 'ladder':ladder, 'rank_list':rank_list, 'match_list':match_list, 'open_challenges':open_challenges}
def issue_challenge(request, *args, **kwargs): # If POST: confirm/issue challenge if request.POST != {}: # unpack post try : challengee_id = request.POST['challengee'] ladder_slug = request.POST['ladder'] except KeyError : messages.error( request, "POST data is incomplete, nice try hacker scum" ) return HttpResponseRedirect(reverse('index')) try : challenger = request.user challengee = User.objects.get(pk = challengee_id) except ObjectDoesNotExist : messages.error( request, "Challenge target does not exist {}".format( challengee_id ) ) return HttpResponseRedirect(reverse('ladder:detail', args=(ladder.slug,))) try : ladder = Ladder.objects.get(slug=ladder_slug) if not ladder.is_user_ranked( challenger ) : raise PlayerNotRanked( "challenger isn't ranked on the ladder", ladder ) except ObjectDoesNotExist, PlayerNotRanked : messages.error( request, "You cannot issue a challenge on the ladder {}".format( ladder_slug ) ) return HttpResponseRedirect(reverse('index')) # Check for open challenges if _open_challenges_exist(request.user, ladder): messages.error(request, u"You have open challenges, you cannot challenge at this time.") else: # Generate a challenge try : challenge = Challenge( challenger=challenger, challengee=challengee, ladder=ladder ) challenge.save() messages.success(request, u"You have issued a challenged to {0}, under the ladder {1}".format(challengee.userprofile.handle, ladder.name)) except ChallengeValidationError as e : messages.error( request, "An error occurred: {}".format( str(e) ) ) return HttpResponseRedirect(reverse('ladder:detail', args=(ladder.slug,))) return render_to_response('challenge.html', {'ladder':ladder, 'challengee':challengee }, context_instance=RequestContext(request))