access profiles with username instead of id

This commit is contained in:
AdrienLSH 2024-01-18 10:31:56 +01:00
parent 1af55795d9
commit dee71c7c8d
7 changed files with 22 additions and 21 deletions

View File

@ -5,21 +5,21 @@ class Profile
/** /**
* @param {Client} client * @param {Client} client
*/ */
constructor (client, id, username = undefined, avatar_url = undefined) constructor (client, username, id = undefined, avatar_url = undefined)
{ {
/** /**
* @type {Client} client * @type {Client} client
*/ */
this.client = client; this.client = client;
this.id = id;
this.username = username; this.username = username;
this.id = id;
this.avatar_url = avatar_url; this.avatar_url = avatar_url;
this.isBlocked = false; this.isBlocked = false;
} }
async init() async init()
{ {
let response = await this.client._get(`/api/profiles/${this.id}`); let response = await this.client._get(`/api/profiles/${this.username}`);
if (response.status !== 200) if (response.status !== 200)
return response.status; return response.status;

View File

@ -20,14 +20,14 @@ class Profiles
let profiles = [] let profiles = []
response_data.forEach((profile) => { response_data.forEach((profile) => {
profiles.push(new Profile(this.client, profile.user_id, profile.username, profile.avatar_url)) profiles.push(new Profile(this.client, profile.username, profile.user_id, profile.avatar_url))
}); });
return profiles; return profiles;
} }
async getProfile(user_id) async getProfile(username)
{ {
let profile = new Profile(this.client, user_id); let profile = new Profile(this.client, username);
if (await profile.init()) if (await profile.init())
return null; return null;
return profile; return profile;

View File

@ -68,7 +68,7 @@ const router = async(uri) => {
const routes = [ const routes = [
{ path: "/", view: Dashboard }, { path: "/", view: Dashboard },
{ path: "/profiles/:id", view: ProfilePageView }, { path: "/profiles/:username", view: ProfilePageView },
{ path: "/tournaments/create", view: TournamentCreateView }, { path: "/tournaments/create", view: TournamentCreateView },
{ path: "/tournaments/:id", view: TournamentPageView }, { path: "/tournaments/:id", view: TournamentPageView },
{ path: "/tournaments/", view: TournamentsView }, { path: "/tournaments/", view: TournamentsView },

View File

@ -18,7 +18,7 @@ export default class extends AbstractAuthentificateView
} }
async display_avatar() { async display_avatar() {
let profile = await client.profiles.getProfile(client.me.id); let profile = await client.profiles.getProfile(client.me.username);
if (profile !== undefined || profile !== null) { if (profile !== undefined || profile !== null) {
if (document.getElementById("avatar") != undefined) if (document.getElementById("avatar") != undefined)
document.getElementById("avatar").remove(); document.getElementById("avatar").remove();

View File

@ -4,17 +4,16 @@ import { client } from "../index.js"
export default class extends AbstractView { export default class extends AbstractView {
constructor(params) { constructor(params) {
super(params, "Profile "); super(params, "Profile ");
this.user_id = params.id; this.username = params.username;
} }
async postInit() async postInit()
{ {
let profile = await client.profiles.getProfile(this.user_id); this.profile = await client.profiles.getProfile(this.username);
if (this.profile === null)
if (profile === null)
return 404; return 404;
this.userId = this.profile.id;
this.profile = await client.profiles.getProfile(this.user_id);
this.info = document.getElementById("info"); this.info = document.getElementById("info");
// Username // Username
@ -39,16 +38,16 @@ export default class extends AbstractView {
if (await client.isAuthentificate() === false) if (await client.isAuthentificate() === false)
return; return;
if (client.me.id != this.user_id) { if (client.me.id != this.userId) {
let block = document.getElementById("block") || document.createElement("a"); let block = document.getElementById("block") || document.createElement("a");
block.id = "block"; block.id = "block";
block.innerText = ""; block.innerText = "";
block.onclick = async () => { block.onclick = async () => {
if (!this.profile.isBlocked) if (!this.profile.isBlocked)
await client.profiles.block(this.user_id); await client.profiles.block(this.userId);
else else
await client.profiles.deblock(this.user_id); await client.profiles.deblock(this.userId);
this.profile = await client.profiles.getProfile(this.user_id); this.profile = await client.profiles.getProfile(this.username);
this.blockButton(); this.blockButton();
}; };
if (this.profile.isBlocked) if (this.profile.isBlocked)

View File

@ -7,9 +7,9 @@ from . import views
urlpatterns = [ urlpatterns = [
path("me", viewsets.MyProfileViewSet.as_view({'patch': 'partial_update', 'get': 'retrieve'}), name="my_profile_page"), path("me", viewsets.MyProfileViewSet.as_view({'patch': 'partial_update', 'get': 'retrieve'}), name="my_profile_page"),
path("<int:pk>", viewsets.ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"),
path("", viewsets.ProfileViewSet.as_view({'get': 'list'}), name="profiles_list"), path("", viewsets.ProfileViewSet.as_view({'get': 'list'}), name="profiles_list"),
path("block", views.BlocksView.as_view(), name="block_page"), path("block", views.BlocksView.as_view(), name="block_page"),
path("block/<int:pk>", views.BlockView.as_view(), name="block_page"), path("block/<int:pk>", views.BlockView.as_view(), name="block_page"),
path("<str:username>", viewsets.ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"),
] + static("/static/avatars/", document_root="./avatars") ] + static("/static/avatars/", document_root="./avatars")

View File

@ -7,6 +7,7 @@ from rest_framework.authentication import SessionAuthentication
from django.http import HttpRequest from django.http import HttpRequest
from django.db.models import QuerySet from django.db.models import QuerySet
from django.contrib.auth.models import User
from .serializers import ProfileSerializer from .serializers import ProfileSerializer
from .models import ProfileModel from .models import ProfileModel
@ -17,10 +18,11 @@ class ProfileViewSet(viewsets.ModelViewSet):
parser_classes = (MultiPartParser, FormParser) parser_classes = (MultiPartParser, FormParser)
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def retrieve(self, request: HttpRequest, pk=None): def retrieve(self, request: HttpRequest, username=None):
if (not self.queryset().filter(pk=pk).exists()): user = User.objects.filter(username=username)
if (not user):
return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND) return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND)
instance = self.queryset().get(pk=pk) instance = self.queryset().get(pk=user[0].pk)
instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:] instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:]
return Response(self.serializer_class(instance).data, return Response(self.serializer_class(instance).data,
status=status.HTTP_200_OK) status=status.HTTP_200_OK)