Dynavera/apps/knowledge/serializers.py

49 lines
1.9 KiB
Python
Raw Normal View History

from rest_framework.serializers import ModelSerializer, SerializerMethodField
2026-03-15 22:19:12 +00:00
from apps.accounts.serializers import OrganizationSerializer, RoleSerializer, UserSerializer
2026-03-08 13:10:49 +00:00
from apps.knowledge.models import RoleRagDocument, TrainingFile
class TrainingFileSerializer(ModelSerializer):
uploaded_by = UserSerializer(read_only=True)
2026-03-15 22:19:12 +00:00
organization = OrganizationSerializer(read_only=True)
role = RoleSerializer(read_only=True)
file_url = SerializerMethodField()
2026-03-15 22:19:12 +00:00
scope = SerializerMethodField()
class Meta:
model = TrainingFile
fields = [
2026-03-15 22:19:12 +00:00
'id', 'uuid', 'organization', 'role', 'scope', 'uploaded_by', 'file', 'file_url',
'file_name', 'file_size', 'file_type', 'description',
'status', 'is_processed', 'created_at', 'updated_at'
]
read_only_fields = [
'id', 'uuid', 'uploaded_by', 'file_size', 'file_type',
'status', 'is_processed', 'created_at', 'updated_at',
2026-03-15 22:19:12 +00:00
'organization', 'role', 'scope'
]
2026-03-15 22:19:12 +00:00
def get_file_url(self, obj: TrainingFile):
request = self.context.get('request')
if obj.file and request:
return request.build_absolute_uri(obj.file.url)
return obj.file.url if obj.file else None
2026-03-15 22:19:12 +00:00
def get_scope(self, obj: TrainingFile) -> str:
return 'role' if obj.role_id else 'organization'
class RoleRagDocumentSerializer(ModelSerializer):
training_file_name = SerializerMethodField()
class Meta:
model = RoleRagDocument
fields = [
'id', 'uuid', 'role', 'training_file', 'training_file_name',
'content', 'content_hash', 'metadata', 'chunk_index',
'is_active', 'created_at'
]
read_only_fields = ['id', 'uuid', 'content_hash', 'created_at']
def get_training_file_name(self, obj: RoleRagDocument) -> str:
return obj.training_file.file_name if obj.training_file else None