from typing import Any, List from django.conf import settings from rest_framework import serializers from rest_framework.fields import SerializerMethodField from rest_framework.request import Request from .models import Notification, OrganizationalUnit, Shift def replace_relative_path_with_absolute_url(request: Request, content: str) -> str: search_pattern = settings.MEDIA_URL + getattr(settings, "CKEDITOR_UPLOAD_PATH", "") replace_with = request.build_absolute_uri(search_pattern) return content.replace(search_pattern, replace_with) class NotificationSerializer(serializers.ModelSerializer[Notification]): content = SerializerMethodField() class Meta: model = Notification fields = '__all__' def get_content(self, obj: Notification) -> Any: return replace_relative_path_with_absolute_url(self.context['request'], obj.content) class OrganizationalUnitSerializer( serializers.ModelSerializer[OrganizationalUnit]): class Meta: model = OrganizationalUnit fields = '__all__' class TaskShiftSerializer(serializers.ModelSerializer[Shift]): weekdays = SerializerMethodField() def get_weekdays(self, obj: Shift) -> List[int]: queryset = obj.weekdays.values_list('number', flat=True) return list(queryset) class Meta: model = Shift fields = ( 'start_time', 'end_time', 'weekdays', )