|
4 | 4 | from django.core.exceptions import ObjectDoesNotExist
|
5 | 5 | from django.utils import timezone
|
6 | 6 | from django.utils.crypto import get_random_string
|
| 7 | +from django.http import JsonResponse |
7 | 8 | from phonenumber_field.serializerfields import PhoneNumberField
|
8 | 9 | from rest_framework import serializers
|
9 | 10 | from rest_live.signals import save_handler
|
|
22 | 23 | QueueStatistic,
|
23 | 24 | Semester,
|
24 | 25 | Tag,
|
| 26 | + Review |
25 | 27 | )
|
26 | 28 | from ohq.sms import sendSMSVerification
|
27 | 29 | from ohq.tasks import sendUpNextNotificationTask
|
@@ -50,7 +52,6 @@ def save(self):
|
50 | 52 | self.validated_data["queue"] = Queue.objects.get(pk=self.context["view"].kwargs["queue_pk"])
|
51 | 53 | return super().save()
|
52 | 54 |
|
53 |
| - |
54 | 55 | class SemesterSerializer(serializers.ModelSerializer):
|
55 | 56 | pretty = serializers.SerializerMethodField()
|
56 | 57 |
|
@@ -209,8 +210,6 @@ class TagSerializer(CourseRouteMixin):
|
209 | 210 | class Meta:
|
210 | 211 | model = Tag
|
211 | 212 | fields = ("id", "name")
|
212 |
| - |
213 |
| - |
214 | 213 | class QuestionSerializer(QueueRouteMixin):
|
215 | 214 | asked_by = UserSerializer(read_only=True)
|
216 | 215 | responded_to_by = UserSerializer(read_only=True)
|
@@ -246,6 +245,7 @@ class Meta:
|
246 | 245 | "should_send_up_soon_notification",
|
247 | 246 | "resolved_note",
|
248 | 247 | "position",
|
| 248 | + "review" |
249 | 249 | )
|
250 | 250 |
|
251 | 251 | def update(self, instance, validated_data):
|
@@ -345,6 +345,43 @@ def create(self, validated_data):
|
345 | 345 | continue
|
346 | 346 | return question
|
347 | 347 |
|
| 348 | + def get_review(self, obj): |
| 349 | + review = Review.objects.get(question=obj) |
| 350 | + serializer = ReviewSerializer(review) |
| 351 | + return serializer.data |
| 352 | + |
| 353 | + |
| 354 | +class ReviewSerializer(serializers.ModelSerializer): |
| 355 | + """ |
| 356 | + Serializer for review, allowing input of a question object via a nested serializer. |
| 357 | + """ |
| 358 | + question = serializers.PrimaryKeyRelatedField( |
| 359 | + queryset=Question.objects.none(), |
| 360 | + write_only=True |
| 361 | + ) |
| 362 | + class Meta: |
| 363 | + model = Review |
| 364 | + fields = ("id", "content", "rating", "question") |
| 365 | + |
| 366 | + def __init__(self, *args, **kwargs): |
| 367 | + super().__init__(*args, **kwargs) |
| 368 | + course_pk = self.context.get('course_pk') |
| 369 | + if course_pk: |
| 370 | + course = Course.objects.filter(pk=course_pk).first() |
| 371 | + self.fields['question'].queryset = Question.objects.filter(queue__course=course) |
| 372 | + |
| 373 | + def create(self, validated_data): |
| 374 | + # Extract the nested question data |
| 375 | + question = validated_data.pop("question") |
| 376 | + validated_data["question"] = question |
| 377 | + return super().create(validated_data) |
| 378 | + |
| 379 | + def update(self, instance, validated_data): |
| 380 | + # Handle question updates if applicable |
| 381 | + question = validated_data.pop("question", None) |
| 382 | + validated_data["question"] = question |
| 383 | + return super().update(instance, validated_data) |
| 384 | + |
348 | 385 |
|
349 | 386 | class MembershipPrivateSerializer(CourseRouteMixin):
|
350 | 387 | """
|
|
0 commit comments