From 05d9dbd29644895c9371720c821d8957fa5d7741 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 1 Jul 2023 16:09:20 +0800 Subject: [PATCH] Polished student_status serializer --- stude/student_status/consumers.py | 9 +++++---- stude/student_status/serializers.py | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/stude/student_status/consumers.py b/stude/student_status/consumers.py index 5c3affe..5d8a479 100644 --- a/stude/student_status/consumers.py +++ b/stude/student_status/consumers.py @@ -11,6 +11,7 @@ from djangochannelsrestframework.mixins import ( ListModelMixin, RetrieveModelMixin, ) +from djangochannelsrestframework.permissions import IsAuthenticated class StudentStatusConsumer( @@ -18,8 +19,8 @@ class StudentStatusConsumer( RetrieveModelMixin, GenericAsyncAPIConsumer, ): - - queryset = StudentStatus.objects.all() + permission_classes = [IsAuthenticated] + queryset = StudentStatus.objects.filter(active=True) serializer_class = StudentStatusSerializer async def websocket_connect(self, message): @@ -41,9 +42,9 @@ class StudentStatusConsumer( while True: try: data = await self.get_student_statuses() - print(f"Sending update: {data}") # existing debug statement + # print(f"Sending update: {data}") Debug await self.send(text_data=json.dumps(data)) - await asyncio.sleep(0.5) + await asyncio.sleep(5) except Exception as e: print(f"Exception in send_updates: {e}") break # Break the loop on error diff --git a/stude/student_status/serializers.py b/stude/student_status/serializers.py index 26768ad..4531a88 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -3,9 +3,11 @@ from .models import StudentStatus class StudentStatusSerializer(serializers.ModelSerializer): - year_level = serializers.CharField(source='user.year_level', read_only=True) + year_level = serializers.CharField( + source='user.year_level', read_only=True) course = serializers.CharField(source='user.course', read_only=True) semester = serializers.CharField(source='user.semester', read_only=True) + user = serializers.CharField(source='user.full_name', read_only=True) class Meta: model = StudentStatus @@ -17,7 +19,7 @@ class StudentStatusSerializer(serializers.ModelSerializer): student_status = StudentStatus.objects.create( user=user, defaults=validated_data) return student_status - + def update(self, instance, validated_data): active = validated_data.get('active', None) @@ -26,4 +28,4 @@ class StudentStatusSerializer(serializers.ModelSerializer): validated_data['y'] = None validated_data['subject'] = None - return super().update(instance, validated_data) \ No newline at end of file + return super().update(instance, validated_data)