Skip to content

Commit

Permalink
Fix Anonymous User Error (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwanok authored Apr 7, 2024
1 parent 7363e8e commit 594bed9
Showing 4 changed files with 14 additions and 157 deletions.
2 changes: 1 addition & 1 deletion project/app/models.py
Original file line number Diff line number Diff line change
@@ -370,7 +370,7 @@ def refresh(self):
break

if solo_rank is None:
raise Exception("No solo rank")
raise NoSoloRankException("No solo rank")

self.league_id = solo_rank["leagueId"]
self.queue_type = solo_rank["queueType"]
50 changes: 0 additions & 50 deletions project/app/riot_client.py
Original file line number Diff line number Diff line change
@@ -13,56 +13,6 @@ class RiotClient:
def __init__(self):
self.api_key = get_config().riot.api_key

def get_account_by_summoner_name(self, summoner_name) -> requests.Response:
url = f"{KR_API_HOST}/lol/summoner/v4/summoners/by-name/{summoner_name}"
response = requests.get(url, headers={"X-Riot-Token": self.api_key})
return response

def get_match_list(self, puuid: str) -> requests.Response:
url = f"{ASIA_API_HOST}/lol/match/v5/matches/by-puuid/{puuid}/ids"
response = requests.get(url, headers={"X-Riot-Token": self.api_key})
return response

def get_match(self, match_id: str) -> requests.Response:
url = f"{ASIA_API_HOST}/lol/match/v5/matches/{match_id}"
response = requests.get(url, headers={"X-Riot-Token": self.api_key})
return response

def get_champion_masteries_by_puuid_top(self, puuid: str) -> requests.Response:
url = f"{KR_API_HOST}/lol/champion-mastery/v4/champion-masteries/by-puuid/{puuid}/top"
response = requests.get(url, headers={"X-Riot-Token": self.api_key})
return response

def get_summoner_by_encrypted_summoner_id(
self, encrypted_summoner_id: str
) -> requests.Response:
url = f"{KR_API_HOST}/lol/summoner/v4/summoners/{encrypted_summoner_id}"
response = requests.get(url, headers={"X-Riot-Token": self.api_key})
return response

def get_token(
self,
redirect_uri: str,
code: str,
) -> requests.Response:
config = get_config()

return requests.post(
f"{AUTH_HOST}/token",
auth=HTTPBasicAuth(
config.riot.rso_client_id,
config.riot.rso_client_secret,
),
headers={
"Content-Type": "application/x-www-form-urlencoded",
},
data={
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri,
},
)

def refresh_token(
self,
refresh_token: str,
6 changes: 0 additions & 6 deletions project/app/urls.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@
duo_match_report,
duo_match_report_submit,
index,
get_account_by_summoner_name,
recommend_ai,
recommend_result,
riot_sign_on_callback,
@@ -46,11 +45,6 @@
index,
name="home",
),
path(
"summoner/",
get_account_by_summoner_name,
name="summoner",
),
path(
"recommend-ai/",
recommend_ai,
113 changes: 13 additions & 100 deletions project/app/views.py
Original file line number Diff line number Diff line change
@@ -20,9 +20,6 @@
Summoner,
)
from rest_framework.decorators import api_view
from app.riot_client import get_client
from app.serializers import SummonerSerializer, LeagueEntrySerializer
from app.models import LeagueEntry
from project.config import get_config
from users.models import AppUser

@@ -70,40 +67,18 @@ def authorize(request: WSGIRequest):
return redirect(url)


def get_account_by_summoner_name(request: WSGIRequest):
summoner_name = request.GET.get("summoner_name")
if Summoner.objects.filter(name=summoner_name).exists():
summoner = Summoner.objects.get(name=summoner_name)

return render(request, "summoner/index.html", {"summoner": summoner})

client = get_client()

response = client.get_account_by_summoner_name(summoner_name)

if response.status_code == 200:
data = response.json()

summoner = Summoner(
id=data["id"],
account_id=data["accountId"],
profile_icon_id=data["profileIconId"],
revision_date=data["revisionDate"],
name=data["name"],
puuid=data["puuid"],
summoner_level=data["summonerLevel"],
)

summoner.save()

return render(request, "summoner/index.html", {"summoner": summoner})

return render(request, "summoner/index.html", {"error": response.json()})


def recommend_ai(request: WSGIRequest):
user: AppUser = request.user

if request.user.is_anonymous:
return render(
request,
"recommend/ai.html",
{
"user": user,
},
)

return render(
request,
"recommend/ai.html",
@@ -124,71 +99,6 @@ def recommend_result(request: WSGIRequest):
)


@api_view(["GET"])
def search_summoners_by_name(request: WSGIRequest):
name = request.GET.get("name")
count = request.GET.get("count")
if not name or not count:
return JsonResponse({"error": "name, count are required"}, status=400)

try:
count = int(count)
except ValueError:
return JsonResponse({"error": "count must be an integer"}, status=400)

if count > 10:
return JsonResponse(
{"error": "count must be less than or equal to 10"}, status=400
)

queryset = Summoner.objects.filter(name__startswith=name)[:count]
serializer = SummonerSerializer(queryset, many=True)

summoners = serializer.data

summoner_ids = [summoner["id"] for summoner in serializer.data]

queryset = LeagueEntry.objects.filter(summoner_id__in=summoner_ids)
serializer = LeagueEntrySerializer(queryset, many=True)

league_entries = serializer.data

for summoner in summoners:
if summoner["puuid"] is None:
new_summoner = get_client().get_summoner_by_encrypted_summoner_id(
summoner["id"]
)

if new_summoner.status_code == 200:
summoner["puuid"] = new_summoner.json()["puuid"]
summoner["account_id"] = new_summoner.json()["accountId"]
summoner["profile_icon_id"] = new_summoner.json()["profileIconId"]
summoner["revision_date"] = new_summoner.json()["revisionDate"]
summoner["summoner_level"] = new_summoner.json()["summonerLevel"]

Summoner.objects.filter(id=summoner["id"]).update(
puuid=summoner["puuid"],
account_id=summoner["account_id"],
profile_icon_id=summoner["profile_icon_id"],
revision_date=summoner["revision_date"],
summoner_level=summoner["summoner_level"],
)

result = [
{
"summoner": summoner,
"league_entries": [
league_entry
for league_entry in league_entries
if league_entry["summoner"] == summoner["id"]
],
}
for summoner in summoners
]

return JsonResponse(result, safe=False)


@api_view(["POST"])
def save_summoner(request: WSGIRequest):
me = request.user
@@ -323,7 +233,10 @@ def riot_account_refresh(request: WSGIRequest):
except NoSoloRankException:
pass
else:
summoner.riot_solo_rank.refresh()
try:
summoner.riot_solo_rank.refresh()
except NoSoloRankException:
pass

return JsonResponse({"message": "전적 갱신에 성공했습니다."})

0 comments on commit 594bed9

Please sign in to comment.