示例#1
0
def summoner(request):
    context = {}
    summonerName = request.GET.get('summonerName', None)
    region = request.GET.get('region', None)
    context['errorFlag'] = "false"
    if not summonerName or not region:
        #TODO: ADD ERROR PAGE(?)
        print 'error'
        return render(request, 'templates/main.html')  
    else:
        api = RiotAPI(RiotConstants.API_KEY, region)
        # Catches TypeError when user enters invalid summoner name.
        try:
            context['summonerName'] = summonerName
            context['region'] = region
            summonerName = summonerName.replace(' ','')
            summonerId = api.getSummonerByName(summonerName)[summonerName.lower()]['id']
            championList = api.getChampionMasteryList(summonerId,10)
    
            context['championList'] = championList
            # creating a list of champions for dropdown in champion search bar.
            championListOrdered = []
    
            for k,v in api.getChampionListByName().items():
                championListOrdered.append([v['name'],v['key']])
            championListOrdered.sort()
            context['orderedChampionList'] = championListOrdered
            # recentMatchesData returns a list with two elements. The first is a 
            # list of the labels for the graphs (days) and the second is a list
            # of champion point values corresponding to the day labels.
            recentMatchesData = api.getRecentMatches(summonerId)
            recentMatchesDataParsed = graphParser.parseRecentMatches(recentMatchesData)
            context['graphLabels'] = recentMatchesDataParsed[0]
            context['graphData'] = recentMatchesDataParsed[1]
            return render(request,'templates/summoner.html', context)          
        except TypeError:
            print("TypeError")
            context['errorFlag'] = "true"
            context['summonerName'] = summonerName
            return render(request, 'templates/main.html', context)