Skip to content

Commit

Permalink
Merge pull request #233 from TheSecretOrganization/97-account-page
Browse files Browse the repository at this point in the history
add account page
  • Loading branch information
Neffi42 authored Oct 15, 2024
2 parents 2eb3efc + 75903bb commit 3af7958
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions django/src/ft_auth/templates/profiles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "./extends/base.html" %}

{% block content %}
<div>
<h3>{{ target.username }}</h3>
{% if games.count == 0 %}
<p>Nerver played a game!</p>
{% else %}
<h2>Games history</h2>
<p>Wins: {{ wins }} on {{ games.count }}</p>
{% for game in games %}
<div>
<p>{{ game.user1.username }} vs {{ game.user2.username }}</p>
<p>{{ game.score1 }} @ {{ game.score2 }}</p>
</div>
{% endfor %}
{% endif %}
</div>
{% endblock %}
1 change: 1 addition & 0 deletions django/src/pages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
path('pong/local/', views.pong_local),
path('pong/online/', views.pong_online),
path('pong/online/<uuid:id>/', views.pong_online),
path('profiles/<str:username>/', views.profiles),
path('login/', views.authentification),
path('register/', views.authentification),
path('authorize/', views.authorize),
Expand Down
15 changes: 15 additions & 0 deletions django/src/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from urllib.parse import quote
from friends.models import Friend
from logging import getLogger
from ft_auth.models import User
from games.models import Pong
import os

logger = getLogger(__name__)
Expand Down Expand Up @@ -99,3 +101,16 @@ def authentification(request: HttpRequest):
'oauth_url': (f"https://api.intra.42.fr/oauth/authorize?client_id={os.getenv('OAUTH_UID')}"
f"&redirect_uri={quote(os.getenv('OAUTH_FALLBACK'))}&response_type=code"),
}, title='Authentification')

@require_GET
def profiles(request: HttpRequest, username: str):
target = User.objects.filter(username=username)
if not target.exists():
return JsonResponse({'error': 'User unknown'}, status=404)
target = target.first()
games = Pong.objects.filter(Q(user1=target) | Q(user2=target))
win = 0
for game in games:
if (target.id is game.user1.id and game.score1 > game.score2) or (target.id is game.user2.id and game.score2 > game.score1):
win += 1
return create_response(request, 'profiles.html', {'target': target, 'games': games, 'wins': win}, title=f"{target.username} Profile")

0 comments on commit 3af7958

Please sign in to comment.