Polished student_status serializer

This commit is contained in:
Keannu Bernasol 2023-07-01 16:09:20 +08:00
parent 4b20812021
commit 05d9dbd296
2 changed files with 10 additions and 7 deletions

View file

@ -11,6 +11,7 @@ from djangochannelsrestframework.mixins import (
ListModelMixin, ListModelMixin,
RetrieveModelMixin, RetrieveModelMixin,
) )
from djangochannelsrestframework.permissions import IsAuthenticated
class StudentStatusConsumer( class StudentStatusConsumer(
@ -18,8 +19,8 @@ class StudentStatusConsumer(
RetrieveModelMixin, RetrieveModelMixin,
GenericAsyncAPIConsumer, GenericAsyncAPIConsumer,
): ):
permission_classes = [IsAuthenticated]
queryset = StudentStatus.objects.all() queryset = StudentStatus.objects.filter(active=True)
serializer_class = StudentStatusSerializer serializer_class = StudentStatusSerializer
async def websocket_connect(self, message): async def websocket_connect(self, message):
@ -41,9 +42,9 @@ class StudentStatusConsumer(
while True: while True:
try: try:
data = await self.get_student_statuses() 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 self.send(text_data=json.dumps(data))
await asyncio.sleep(0.5) await asyncio.sleep(5)
except Exception as e: except Exception as e:
print(f"Exception in send_updates: {e}") print(f"Exception in send_updates: {e}")
break # Break the loop on error break # Break the loop on error

View file

@ -3,9 +3,11 @@ from .models import StudentStatus
class StudentStatusSerializer(serializers.ModelSerializer): 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) course = serializers.CharField(source='user.course', read_only=True)
semester = serializers.CharField(source='user.semester', read_only=True) semester = serializers.CharField(source='user.semester', read_only=True)
user = serializers.CharField(source='user.full_name', read_only=True)
class Meta: class Meta:
model = StudentStatus model = StudentStatus
@ -17,7 +19,7 @@ class StudentStatusSerializer(serializers.ModelSerializer):
student_status = StudentStatus.objects.create( student_status = StudentStatus.objects.create(
user=user, defaults=validated_data) user=user, defaults=validated_data)
return student_status return student_status
def update(self, instance, validated_data): def update(self, instance, validated_data):
active = validated_data.get('active', None) active = validated_data.get('active', None)
@ -26,4 +28,4 @@ class StudentStatusSerializer(serializers.ModelSerializer):
validated_data['y'] = None validated_data['y'] = None
validated_data['subject'] = None validated_data['subject'] = None
return super().update(instance, validated_data) return super().update(instance, validated_data)