Skip to content

Commit

Permalink
Merge branch 'production' of https://github.com/zakarm/ft_transcendence
Browse files Browse the repository at this point in the history
… into production
  • Loading branch information
Nouakchi committed May 7, 2024
2 parents 9f0266e + d2d181c commit e33df8e
Show file tree
Hide file tree
Showing 17 changed files with 787 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MAJOR ?=
MINOR ?=
PATCH ?=
VERSION := $(MAJOR).$(MINOR).$(PATCH)
DATA_DIR := /Users/${USER}/Desktop/data
DATA_DIR := ${HOME}/Desktop/data

# Define colors
GREEN := $(shell tput -Txterm setaf 2)
Expand Down
3 changes: 2 additions & 1 deletion app/back-end/dashboards/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib import admin
from .models import Friendship
from .models import Friendship, Notification

# Register your models here.
admin.site.register(Friendship)
admin.site.register(Notification)
12 changes: 12 additions & 0 deletions app/back-end/dashboards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ class Friendship(models.Model):
u_two_is_blocked_u_one = models.BooleanField(default=False)
class Meta:
db_table = 'Friendship'

class Notification(models.Model):
notification_id = models.AutoField(primary_key=True)
user = models.ForeignKey('authentication.User', models.DO_NOTHING,
db_column='user')
image_url = models.CharField(max_length=200, blank=True)
message_url = models.CharField(max_length=200, blank=True)
title = models.CharField(max_length=50, blank=True)
link = models.CharField(max_length=200, blank=True)

class Meta:
db_table = 'Notification'
62 changes: 44 additions & 18 deletions app/back-end/dashboards/serializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import serializers
from authentication.models import User
from game.models import Match
from .models import Friendship
from .models import Friendship, Notification
from django.db.models import F, Q
from .utils import (get_total_games,
get_win_games,
Expand Down Expand Up @@ -102,25 +102,33 @@ def get_friends(self, obj):
return serializer.data


# class FriendshipSerializer(serializers.ModelSerializer):
# user = serializers.SerializerMethodField()
# class Meta:
# model = Friendship
# fields = ('user', 'is_accepted')
class BlockedFriendshipSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField()
blocked = serializers.SerializerMethodField()
is_user_from = serializers.SerializerMethodField()
class Meta:
model = Friendship
fields = ('user', 'is_accepted', 'blocked', 'is_user_from')

# def get_user(self, obj):
# if obj.user_from.id == self.context['id']:
# user_data = User.objects.get(id=obj.user_to.id)
# else:
# user_data = User.objects.get(id=obj.user_from.id)

# serializer = UserSerializer(user_data)
# return serializer.data
def get_user(self, obj):
if obj.user_from.id == self.context['id']:
user_data = User.objects.get(id=obj.user_to.id)
else:
user_data = User.objects.get(id=obj.user_from.id)

serializer = UserSerializer(user_data)
return serializer.data

# def get_blocked(self, obj):
# if obj.user_from.id == self.context['id']:
# Friendship.objects.filter
def get_blocked(self, obj):
if obj.user_from.id == self.context['id']:
blocked = obj.u_one_is_blocked_u_two
else:
blocked = obj.u_two_is_blocked_u_one
return blocked

def get_is_user_from(self, obj):
return obj.user_from.id == self.context['id']

class BlockedFriendsSerializer(serializers.ModelSerializer):
friends = serializers.SerializerMethodField()
class Meta:
Expand All @@ -129,5 +137,23 @@ class Meta:

def get_friends(self, obj):
friends_data = Friendship.objects.filter(Q(user_from = obj)| Q(user_to= obj))
serializer = FriendshipSerializer(friends_data, many=True, context = {'id': obj.id})
serializer = BlockedFriendshipSerializer(friends_data, many=True,
context = {'id': obj.id})
return serializer.data


class NotificationSerializer(serializers.ModelSerializer):
class Meta:
model = Notification
fields = ('notification_id', 'image_url', 'message_url', 'title', 'link')

class NotificationUserSerializer(serializers.ModelSerializer):
notifications = serializers.SerializerMethodField()
class Meta:
model = User
fields = ('id', 'username', 'notifications')

def get_notifications(self, obj):
notifications_data = Notification.objects.filter(user = obj)
serializer = NotificationSerializer(notifications_data, many = True)
return serializer.data
6 changes: 5 additions & 1 deletion app/back-end/dashboards/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
AcceptFriendshipView,
AddFriendshipView,
BlockFriendshipView,
UnblockFriendshipView
UnblockFriendshipView,
BlockedFriendsView,
NotificationsView
)

urlpatterns = [
Expand All @@ -22,4 +24,6 @@
path('friends-add', AddFriendshipView.as_view(), name="friends-add"),
path('friends-block', BlockFriendshipView.as_view(), name="friends-block"),
path('friends-unblock', UnblockFriendshipView.as_view(), name="friends-unblock"),
path('blocked-friends', BlockedFriendsView.as_view(), name="friends-unblock"),
path('notifications', NotificationsView.as_view(), name="notifications"),
]
21 changes: 20 additions & 1 deletion app/back-end/dashboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from .serializer import (MainDashboardSerializer,
ProfileSerializer,
FriendsSerializer,
UserSerializer)
UserSerializer,
BlockedFriendsSerializer,
NotificationUserSerializer)
from .models import Friendship
from authentication.models import User
from rest_framework import status
Expand Down Expand Up @@ -199,3 +201,20 @@ def post(self, request):
except Friendship.DoesNotExist:
return Response({'error': 'Friendship does not exist'}, status=status.HTTP_400_BAD_REQUEST)

class BlockedFriendsView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request):
user = request.user
serializer = BlockedFriendsSerializer(instance=user)
return Response(serializer.data)

class NotificationsView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request):
user = request.user
serializer = NotificationUserSerializer(instance=user)
return Response(serializer.data)
Loading

0 comments on commit e33df8e

Please sign in to comment.