mirror of
https://github.com/lemeow125/DocManagerBackend.git
synced 2025-01-18 17:13:00 +08:00
Add relationship between questionnaires and document request
This commit is contained in:
parent
f3b16451fd
commit
6f276b43cc
3 changed files with 36 additions and 7 deletions
|
@ -75,11 +75,15 @@ class DocumentRequestUnitWithFileSerializer(serializers.ModelSerializer):
|
|||
|
||||
class DocumentRequestSerializer(serializers.ModelSerializer):
|
||||
documents = serializers.SerializerMethodField()
|
||||
questionnaire = serializers.SlugRelatedField(
|
||||
many=False,
|
||||
slug_field="id",
|
||||
queryset=Questionnaire.objects.all(),
|
||||
)
|
||||
requester = serializers.SlugRelatedField(
|
||||
many=False,
|
||||
slug_field="email",
|
||||
queryset=CustomUser.objects.all(),
|
||||
required=False,
|
||||
)
|
||||
purpose = serializers.CharField(max_length=512)
|
||||
date_requested = serializers.DateTimeField(
|
||||
|
@ -90,6 +94,7 @@ class DocumentRequestSerializer(serializers.ModelSerializer):
|
|||
model = DocumentRequest
|
||||
fields = [
|
||||
"id",
|
||||
"questionnaire",
|
||||
"requester",
|
||||
"college",
|
||||
"type",
|
||||
|
@ -100,6 +105,7 @@ class DocumentRequestSerializer(serializers.ModelSerializer):
|
|||
]
|
||||
read_only_fields = [
|
||||
"id",
|
||||
"questionnaire",
|
||||
"requester",
|
||||
"college",
|
||||
"type",
|
||||
|
|
|
@ -17,7 +17,8 @@ class Questionnaire(models.Model):
|
|||
|
||||
date_submitted = models.DateTimeField(default=now, editable=False)
|
||||
|
||||
region_of_residence = models.CharField(max_length=64, null=False, blank=False)
|
||||
region_of_residence = models.CharField(
|
||||
max_length=64, null=False, blank=False)
|
||||
service_availed = models.CharField(max_length=64, null=False, blank=False)
|
||||
I_AM_I_CHOICES = (
|
||||
("faculty", "Faculty"),
|
||||
|
|
|
@ -1,16 +1,25 @@
|
|||
from rest_framework import serializers
|
||||
from accounts.models import CustomUser
|
||||
from document_requests.models import DocumentRequest
|
||||
from .models import Questionnaire
|
||||
|
||||
|
||||
class QuestionnaireSerializer(serializers.ModelSerializer):
|
||||
document_request = serializers.SlugRelatedField(
|
||||
many=False,
|
||||
slug_field="id",
|
||||
queryset=DocumentRequest.objects.all(),
|
||||
required=True,
|
||||
allow_null=True,
|
||||
)
|
||||
client = serializers.SlugRelatedField(
|
||||
many=False,
|
||||
slug_field="email",
|
||||
queryset=CustomUser.objects.all(),
|
||||
required=False,
|
||||
)
|
||||
client_type = serializers.ChoiceField(choices=Questionnaire.CLIENT_TYPE_CHOICES)
|
||||
client_type = serializers.ChoiceField(
|
||||
choices=Questionnaire.CLIENT_TYPE_CHOICES)
|
||||
|
||||
date_submitted = serializers.DateTimeField(
|
||||
format="%m-%d-%Y %I:%M %p", read_only=True
|
||||
|
@ -20,7 +29,7 @@ class QuestionnaireSerializer(serializers.ModelSerializer):
|
|||
region_of_residence = serializers.CharField(max_length=64)
|
||||
service_availed = serializers.CharField(max_length=64)
|
||||
i_am_a = serializers.ChoiceField(choices=Questionnaire.I_AM_I_CHOICES)
|
||||
i_am_a_other = serializers.CharField(required=False)
|
||||
i_am_a_other = serializers.CharField(required=False, allow_blank=True)
|
||||
q1_answer = serializers.ChoiceField(choices=Questionnaire.Q1_CHOICES)
|
||||
q2_answer = serializers.ChoiceField(choices=Questionnaire.Q2_CHOICES)
|
||||
q3_answer = serializers.ChoiceField(choices=Questionnaire.Q3_CHOICES)
|
||||
|
@ -32,7 +41,8 @@ class QuestionnaireSerializer(serializers.ModelSerializer):
|
|||
sqd6_answer = serializers.ChoiceField(choices=Questionnaire.SQD_CHOICES)
|
||||
sqd7_answer = serializers.ChoiceField(choices=Questionnaire.SQD_CHOICES)
|
||||
sqd8_answer = serializers.ChoiceField(choices=Questionnaire.SQD_CHOICES)
|
||||
extra_suggestions = serializers.CharField(max_length=512, required=False)
|
||||
extra_suggestions = serializers.CharField(
|
||||
max_length=512, required=False, allow_blank=True)
|
||||
|
||||
def get_age(self, obj):
|
||||
return obj.client.age
|
||||
|
@ -58,20 +68,32 @@ class QuestionnaireSerializer(serializers.ModelSerializer):
|
|||
{"error": "Missing description for client type: Other"}
|
||||
)
|
||||
|
||||
# Create the instance without calling super().create()
|
||||
if "document_request" in validated_data:
|
||||
document_request_id = validated_data["document_request"].id
|
||||
del validated_data["document_request"]
|
||||
|
||||
instance = self.Meta.model(**validated_data)
|
||||
|
||||
# Explicitly set the client_type attribute
|
||||
instance.client_type = validated_data.get("client_type")
|
||||
|
||||
# Save the instance
|
||||
instance.save()
|
||||
|
||||
# Update associated document request if it exists
|
||||
if document_request_id:
|
||||
DOCUMENT_REQUEST = DocumentRequest.objects.get(
|
||||
id=document_request_id
|
||||
)
|
||||
DOCUMENT_REQUEST.questionnaire = instance
|
||||
DOCUMENT_REQUEST.save()
|
||||
|
||||
return instance
|
||||
|
||||
class Meta:
|
||||
model = Questionnaire
|
||||
fields = [
|
||||
"id",
|
||||
"document_request",
|
||||
"client",
|
||||
"client_type",
|
||||
"date_submitted",
|
||||
|
|
Loading…
Reference in a new issue