Skip to content

Commit 68b51c8

Browse files
committed
Classroom users endpoint & view
1 parent c08da7b commit 68b51c8

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

authentication/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ManthanoUser(AbstractUser):
3636
date_joined = models.DateTimeField('Date joined', auto_now_add=True)
3737

3838
classroom = models.ForeignKey('classroom.Classroom', on_delete=models.CASCADE,
39-
related_name='classrooms', null=True)
39+
related_name='users', null=True)
4040

4141
USERNAME_FIELD = 'email'
4242
REQUIRED_FIELDS = ['username', 'password']

authentication/serializers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,16 @@ class UserSerializer(serializers.ModelSerializer):
4444
class Meta:
4545
model = get_user_model()
4646
fields = ('id', 'username,' 'email', 'first_name', 'last_name')
47+
48+
49+
class ClassroomUserSerializer(serializers.ModelSerializer):
50+
51+
class Meta:
52+
model = get_user_model()
53+
fields = ('id', 'first_name', 'last_name', 'username')
54+
55+
def to_representation(self, instance):
56+
ret = super().to_representation(instance)
57+
ret['profile_picture'] = instance.profile.profile_picture or None
58+
ret['profile_background'] = instance.profile.profile_background or None
59+
return ret

classroom/serializers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from rest_framework import serializers, validators
22

3+
from authentication.serializers import ClassroomUserSerializer
4+
35
from .models import *
46

57

@@ -12,6 +14,7 @@ def to_representation(self, instance):
1214
ret = super().to_representation(instance)
1315
ret['channels'] = ChannelSerializer(instance.channels, many=True).data
1416
ret['jitsi_channels'] = JitsiChannelSerializer(instance.jitsi_channels, many=True).data
17+
ret['users'] = ClassroomUserSerializer(instance.users, many=True).data
1518
return ret
1619

1720

classroom/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@
2121
path('messages/', views.GetChannelMessagesView.as_view(), name='messages_classroom'),
2222
path('message/edit/<int:id>', views.UpdateMessageView.as_view(), name='message_edit_classroom'),
2323
path('message/delete/<int:id>', views.DeleteMessageView.as_view(), name='message_remove_classroom'),
24+
25+
path('users', views.GetClassroomUsersView.as_view(), name='users_classroom')
2426
# path('<slug:code>/<slug:channel>', views.classroom, name='classroom'),
2527
]

classroom/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from rest_framework.response import Response
88
from rest_framework.permissions import IsAuthenticated
99
from authentication.models import ManthanoUser
10+
from authentication.serializers import ClassroomUserSerializer, UserSerializer
1011
from classroom.models import *
1112

1213
from classroom.serializers import *
@@ -227,3 +228,12 @@ def delete(self, request, id):
227228
except Channel.DoesNotExist:
228229
return Response(status=status.HTTP_404_NOT_FOUND)
229230
return Response(json.dumps(message), content_type='application/json', status=status.HTTP_201_CREATED)
231+
232+
233+
class GetClassroomUsersView(views.APIView):
234+
permission_classes = [IsAuthenticated]
235+
236+
def get(self, request):
237+
user: ManthanoUser = request.user
238+
serializedUsers = ClassroomUserSerializer(user.classroom.users.all(), many=True)
239+
return serializedUsers.data

0 commit comments

Comments
 (0)