This commit is contained in:
Xamora 2024-04-18 20:56:21 +02:00
parent abb7797e48
commit 6b82f00a14
5 changed files with 13 additions and 74 deletions

View File

@ -19,22 +19,12 @@ class MyProfile extends Profile
* @type {[Profile]}
*/
this.friends = [];
/**
* @type {[Profile]}
*/
this.incomingFriendRequests = [];
/**
* @type {[Profile]}
*/
this.outgoingFriendRequests = [];
}
async init() {
await super.init();
await this.getBlockedUsers();
await this.getFriends();
await this.getIncomingFriendRequests()
await this.getOutgoingFriendRequests()
}
async getBlockedUsers() {
@ -48,20 +38,6 @@ class MyProfile extends Profile
const data = await response.json();
data.forEach(profileData => this.friends.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)));
}
async getIncomingFriendRequests() {
const response = await this.client._get('/api/profiles/incoming_friend_requests');
const data = await response.json();
data.forEach(profileData => this.incomingFriendRequests.push(
new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)
));
}
async getOutgoingFriendRequests() {
const response = await this.client._get('/api/profiles/outgoing_friend_requests');
const data = await response.json();
data.forEach(profileData => this.outgoingFriendRequests.push(
new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar)
));
}
/**
*
* @param {File} selectedFile

View File

@ -1,4 +1,4 @@
import {client} from "../../index.js";
import {client, navigateTo} from "../../index.js";
import AbstractAuthenticatedView from "../abstracts/AbstractAuthenticatedView.js";
export default class extends AbstractAuthenticatedView
@ -39,6 +39,10 @@ export default class extends AbstractAuthenticatedView
// name
let td = document.createElement("td");
td.innerText = tournament.name;
td.onclick = () => {
navigateTo("tournaments/" + tournament.id);
}
tr.appendChild(td);
// state

View File

@ -44,7 +44,7 @@ class ProfileModel(Model):
).delete()
def is_friend_requested_by(self, profile):
return FriendRequestModel.objects.filter(author=profile, target=self).exists()
return self.get_received_friend_request_from(profile) is None
def get_received_friend_request_from(self, profile):
return FriendRequestModel.objects.filter(author=profile, target=self).first()
@ -52,13 +52,10 @@ class ProfileModel(Model):
def is_friend_requesting(self, profile):
return FriendRequestModel.objects.filter(author=self, target=profile).exists()
def get_outgoing_friend_request_to(self, profile):
return FriendRequestModel.objects.filter(author=self, target=profile).first()
def get_outgoing_friend_requests(self) -> list[ProfileModel]:
def get_sent_friend_requests(self) -> list[ProfileModel]:
return FriendRequestModel.objects.filter(author=self)
def get_incoming_friend_requests(self) -> list[ProfileModel]:
def get_received_friend_requests(self) -> list[ProfileModel]:
return FriendRequestModel.objects.filter(target=self)

View File

@ -3,10 +3,7 @@ from django.urls import path
from .viewsets.ProfileViewSet import ProfileViewSet
from .viewsets.MyProfileViewSet import MyProfileViewSet
from .views.blocks import GetBlocksView, EditBlocksView
from .views.friends import (GetFriendsView,
EditFriendView,
GetIncomingFriendRequestView,
GetOutgoingFriendRequestView)
from .views.friends import GetFriendsView, EditFriendView
urlpatterns = [
path("settings", MyProfileViewSet.as_view({'patch': 'partial_update', 'delete': 'delete_avatar'}), name="my_profile_page"),
@ -16,8 +13,6 @@ urlpatterns = [
path("block/<int:pk>", EditBlocksView.as_view(), name="block_page"),
path("friends", GetFriendsView.as_view(), name="friends_list_page"),
path("friends/<int:pk>", EditFriendView.as_view(), name="friends_edit_page"),
path("incoming_friend_requests", GetIncomingFriendRequestView.as_view(), name="incoming_friend_requests"),
path("outgoing_friend_requests", GetOutgoingFriendRequestView.as_view(), name="outgoing_friend_requests"),
path("user/<str:username>", ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"),
path("id/<int:pk>", ProfileViewSet.as_view({'get': 'retrieve_id'}), name="profile_page"),
]

View File

@ -6,7 +6,7 @@ from rest_framework.authentication import SessionAuthentication
from django.utils.translation import gettext as _
from django.shortcuts import get_object_or_404
from ..models import ProfileModel, FriendRequestModel
from ..models import ProfileModel, FriendModel
from ..serializers.ProfileSerializer import ProfileSerializer
@ -26,7 +26,7 @@ class EditFriendView(APIView):
return self.request.user.profilemodel
def post(self, request, pk=None):
user_profile: ProfileModel = self.get_object()
user_profile = self.get_object()
friend_profile = get_object_or_404(ProfileModel, pk=pk)
if user_profile.pk == pk:
@ -35,16 +35,8 @@ class EditFriendView(APIView):
if user_profile.is_friend(friend_profile):
return Response(_('You are already friend with this user.'), status.HTTP_400_BAD_REQUEST)
if user_profile.is_friend_requesting(friend_profile):
return Response(_('You already sent a request to this user.'), status.HTTP_400_BAD_REQUEST)
incoming_request = user_profile.get_received_friend_request_from(friend_profile)
if incoming_request:
incoming_request.accept()
return Response(_('Friendship succssfully created.'), status.HTTP_201_CREATED)
FriendRequestModel(author=user_profile, target=friend_profile).save()
return Response(_('Friend request sent.'), status.HTTP_200_OK)
FriendModel(friend1=user_profile, friend2=friend_profile).save()
return Response(_('Friendship succssfully created.'), status.HTTP_201_CREATED)
def delete(self, request, pk=None):
user_profile = self.get_object()
@ -53,30 +45,5 @@ class EditFriendView(APIView):
if not user_profile.is_friend(friend_profile):
return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST)
outgoing_request = user_profile.get_outgoing_friend_request_to(friend_profile)
if outgoing_request:
outgoing_request.delete()
return Response(_('Friend request cancelled.'))
user_profile.delete_friend(friend_profile)
return Response(_('Friendship succssfully deleted.'))
class GetIncomingFriendRequestView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(self, request):
requests = request.user.profilemodel.get_incoming_friend_requests()
profiles = [request.author for request in requests]
return Response(ProfileSerializer(profiles, many=True).data)
class GetOutgoingFriendRequestView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(self, request):
requests = request.user.profilemodel.get_outgoing_friend_requests()
profiles = [request.target for request in requests]
return Response(ProfileSerializer(profiles, many=True).data)