160 lines
5.9 KiB
Python
160 lines
5.9 KiB
Python
"""
|
|
Django REST Framework serializers for MDT app.
|
|
"""
|
|
|
|
from rest_framework import serializers
|
|
from .models import (
|
|
MDTNote,
|
|
MDTContribution,
|
|
MDTApproval,
|
|
MDTMention,
|
|
MDTAttachment,
|
|
)
|
|
|
|
|
|
class MDTContributionSerializer(serializers.ModelSerializer):
|
|
"""Serializer for MDT contributions."""
|
|
|
|
contributor_name = serializers.CharField(source='contributor.get_full_name', read_only=True)
|
|
clinic_name = serializers.CharField(source='clinic.name_en', read_only=True)
|
|
mentioned_user_names = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = MDTContribution
|
|
fields = [
|
|
'id', 'mdt_note', 'contributor', 'contributor_name',
|
|
'clinic', 'clinic_name', 'content', 'is_final',
|
|
'mentioned_users', 'mentioned_user_names',
|
|
'created_at', 'edited_at',
|
|
]
|
|
read_only_fields = ['id', 'created_at', 'edited_at']
|
|
|
|
def get_mentioned_user_names(self, obj):
|
|
"""Get names of mentioned users."""
|
|
return [user.get_full_name() for user in obj.mentioned_users.all()]
|
|
|
|
|
|
class MDTApprovalSerializer(serializers.ModelSerializer):
|
|
"""Serializer for MDT approvals."""
|
|
|
|
approver_name = serializers.CharField(source='approver.get_full_name', read_only=True)
|
|
clinic_name = serializers.CharField(source='clinic.name_en', read_only=True)
|
|
|
|
class Meta:
|
|
model = MDTApproval
|
|
fields = [
|
|
'id', 'mdt_note', 'approver', 'approver_name',
|
|
'clinic', 'clinic_name', 'approved', 'approved_at',
|
|
'comments', 'created_at',
|
|
]
|
|
read_only_fields = ['id', 'created_at', 'approved_at']
|
|
|
|
|
|
class MDTMentionSerializer(serializers.ModelSerializer):
|
|
"""Serializer for MDT mentions."""
|
|
|
|
mentioned_user_name = serializers.CharField(source='mentioned_user.get_full_name', read_only=True)
|
|
contribution_content = serializers.CharField(source='contribution.content', read_only=True)
|
|
|
|
class Meta:
|
|
model = MDTMention
|
|
fields = [
|
|
'id', 'contribution', 'contribution_content',
|
|
'mentioned_user', 'mentioned_user_name',
|
|
'notified_at', 'viewed_at', 'created_at',
|
|
]
|
|
read_only_fields = ['id', 'created_at', 'notified_at', 'viewed_at']
|
|
|
|
|
|
class MDTAttachmentSerializer(serializers.ModelSerializer):
|
|
"""Serializer for MDT attachments."""
|
|
|
|
uploaded_by_name = serializers.CharField(source='uploaded_by.get_full_name', read_only=True)
|
|
file_type_display = serializers.CharField(source='get_file_type_display', read_only=True)
|
|
file_url = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = MDTAttachment
|
|
fields = [
|
|
'id', 'mdt_note', 'file', 'file_url', 'file_type',
|
|
'file_type_display', 'description',
|
|
'uploaded_by', 'uploaded_by_name', 'created_at',
|
|
]
|
|
read_only_fields = ['id', 'created_at']
|
|
|
|
def get_file_url(self, obj):
|
|
"""Get full URL for file."""
|
|
request = self.context.get('request')
|
|
if obj.file and request:
|
|
return request.build_absolute_uri(obj.file.url)
|
|
return None
|
|
|
|
|
|
class MDTNoteSerializer(serializers.ModelSerializer):
|
|
"""Serializer for MDT notes."""
|
|
|
|
patient_name = serializers.CharField(source='patient.get_full_name', read_only=True)
|
|
initiated_by_name = serializers.CharField(source='initiated_by.get_full_name', read_only=True)
|
|
status_display = serializers.CharField(source='get_status_display', read_only=True)
|
|
|
|
# Nested serializers
|
|
contributions = MDTContributionSerializer(many=True, read_only=True)
|
|
approvals = MDTApprovalSerializer(many=True, read_only=True)
|
|
attachments = MDTAttachmentSerializer(many=True, read_only=True)
|
|
|
|
# Computed fields
|
|
is_editable = serializers.BooleanField(read_only=True)
|
|
can_finalize = serializers.BooleanField(read_only=True)
|
|
contributor_count = serializers.SerializerMethodField()
|
|
approval_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = MDTNote
|
|
fields = [
|
|
'id', 'patient', 'patient_name', 'tenant',
|
|
'title', 'purpose', 'status', 'status_display',
|
|
'initiated_by', 'initiated_by_name',
|
|
'version', 'summary', 'recommendations',
|
|
'finalized_at', 'is_editable', 'can_finalize',
|
|
'contributor_count', 'approval_count',
|
|
'contributions', 'approvals', 'attachments',
|
|
'created_at', 'updated_at',
|
|
]
|
|
read_only_fields = ['id', 'created_at', 'updated_at', 'finalized_at', 'version']
|
|
|
|
def get_contributor_count(self, obj):
|
|
"""Get number of contributors."""
|
|
return obj.contributions.count()
|
|
|
|
def get_approval_count(self, obj):
|
|
"""Get number of approvals."""
|
|
return obj.approvals.filter(approved=True).count()
|
|
|
|
|
|
class MDTNoteListSerializer(serializers.ModelSerializer):
|
|
"""Lightweight serializer for MDT note lists."""
|
|
|
|
patient_name = serializers.CharField(source='patient.get_full_name', read_only=True)
|
|
initiated_by_name = serializers.CharField(source='initiated_by.get_full_name', read_only=True)
|
|
status_display = serializers.CharField(source='get_status_display', read_only=True)
|
|
contributor_count = serializers.SerializerMethodField()
|
|
approval_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = MDTNote
|
|
fields = [
|
|
'id', 'patient', 'patient_name', 'title', 'purpose',
|
|
'status', 'status_display', 'initiated_by', 'initiated_by_name',
|
|
'contributor_count', 'approval_count',
|
|
'finalized_at', 'created_at', 'updated_at',
|
|
]
|
|
read_only_fields = ['id', 'created_at', 'updated_at', 'finalized_at']
|
|
|
|
def get_contributor_count(self, obj):
|
|
"""Get number of contributors."""
|
|
return obj.contributions.count()
|
|
|
|
def get_approval_count(self, obj):
|
|
"""Get number of approvals."""
|
|
return obj.approvals.filter(approved=True).count()
|