mirror of
https://github.com/lemeow125/DocManagerBackend.git
synced 2025-01-19 01:23:02 +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):
|
class DocumentRequestSerializer(serializers.ModelSerializer):
|
||||||
documents = serializers.SerializerMethodField()
|
documents = serializers.SerializerMethodField()
|
||||||
|
questionnaire = serializers.SlugRelatedField(
|
||||||
|
many=False,
|
||||||
|
slug_field="id",
|
||||||
|
queryset=Questionnaire.objects.all(),
|
||||||
|
)
|
||||||
requester = serializers.SlugRelatedField(
|
requester = serializers.SlugRelatedField(
|
||||||
many=False,
|
many=False,
|
||||||
slug_field="email",
|
slug_field="email",
|
||||||
queryset=CustomUser.objects.all(),
|
queryset=CustomUser.objects.all(),
|
||||||
required=False,
|
|
||||||
)
|
)
|
||||||
purpose = serializers.CharField(max_length=512)
|
purpose = serializers.CharField(max_length=512)
|
||||||
date_requested = serializers.DateTimeField(
|
date_requested = serializers.DateTimeField(
|
||||||
|
@ -90,6 +94,7 @@ class DocumentRequestSerializer(serializers.ModelSerializer):
|
||||||
model = DocumentRequest
|
model = DocumentRequest
|
||||||
fields = [
|
fields = [
|
||||||
"id",
|
"id",
|
||||||
|
"questionnaire",
|
||||||
"requester",
|
"requester",
|
||||||
"college",
|
"college",
|
||||||
"type",
|
"type",
|
||||||
|
@ -100,6 +105,7 @@ class DocumentRequestSerializer(serializers.ModelSerializer):
|
||||||
]
|
]
|
||||||
read_only_fields = [
|
read_only_fields = [
|
||||||
"id",
|
"id",
|
||||||
|
"questionnaire",
|
||||||
"requester",
|
"requester",
|
||||||
"college",
|
"college",
|
||||||
"type",
|
"type",
|
||||||
|
|
|
@ -17,7 +17,8 @@ class Questionnaire(models.Model):
|
||||||
|
|
||||||
date_submitted = models.DateTimeField(default=now, editable=False)
|
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)
|
service_availed = models.CharField(max_length=64, null=False, blank=False)
|
||||||
I_AM_I_CHOICES = (
|
I_AM_I_CHOICES = (
|
||||||
("faculty", "Faculty"),
|
("faculty", "Faculty"),
|
||||||
|
|
|
@ -1,16 +1,25 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from accounts.models import CustomUser
|
from accounts.models import CustomUser
|
||||||
|
from document_requests.models import DocumentRequest
|
||||||
from .models import Questionnaire
|
from .models import Questionnaire
|
||||||
|
|
||||||
|
|
||||||
class QuestionnaireSerializer(serializers.ModelSerializer):
|
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(
|
client = serializers.SlugRelatedField(
|
||||||
many=False,
|
many=False,
|
||||||
slug_field="email",
|
slug_field="email",
|
||||||
queryset=CustomUser.objects.all(),
|
queryset=CustomUser.objects.all(),
|
||||||
required=False,
|
required=False,
|
||||||
)
|
)
|
||||||
client_type = serializers.ChoiceField(choices=Questionnaire.CLIENT_TYPE_CHOICES)
|
client_type = serializers.ChoiceField(
|
||||||
|
choices=Questionnaire.CLIENT_TYPE_CHOICES)
|
||||||
|
|
||||||
date_submitted = serializers.DateTimeField(
|
date_submitted = serializers.DateTimeField(
|
||||||
format="%m-%d-%Y %I:%M %p", read_only=True
|
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)
|
region_of_residence = serializers.CharField(max_length=64)
|
||||||
service_availed = 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 = 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)
|
q1_answer = serializers.ChoiceField(choices=Questionnaire.Q1_CHOICES)
|
||||||
q2_answer = serializers.ChoiceField(choices=Questionnaire.Q2_CHOICES)
|
q2_answer = serializers.ChoiceField(choices=Questionnaire.Q2_CHOICES)
|
||||||
q3_answer = serializers.ChoiceField(choices=Questionnaire.Q3_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)
|
sqd6_answer = serializers.ChoiceField(choices=Questionnaire.SQD_CHOICES)
|
||||||
sqd7_answer = serializers.ChoiceField(choices=Questionnaire.SQD_CHOICES)
|
sqd7_answer = serializers.ChoiceField(choices=Questionnaire.SQD_CHOICES)
|
||||||
sqd8_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):
|
def get_age(self, obj):
|
||||||
return obj.client.age
|
return obj.client.age
|
||||||
|
@ -58,20 +68,32 @@ class QuestionnaireSerializer(serializers.ModelSerializer):
|
||||||
{"error": "Missing description for client type: Other"}
|
{"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)
|
instance = self.Meta.model(**validated_data)
|
||||||
|
|
||||||
# Explicitly set the client_type attribute
|
# Explicitly set the client_type attribute
|
||||||
instance.client_type = validated_data.get("client_type")
|
instance.client_type = validated_data.get("client_type")
|
||||||
|
|
||||||
# Save the instance
|
# Save the instance
|
||||||
instance.save()
|
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
|
return instance
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Questionnaire
|
model = Questionnaire
|
||||||
fields = [
|
fields = [
|
||||||
"id",
|
"id",
|
||||||
|
"document_request",
|
||||||
"client",
|
"client",
|
||||||
"client_type",
|
"client_type",
|
||||||
"date_submitted",
|
"date_submitted",
|
||||||
|
|
Loading…
Reference in a new issue