sbt-idp/cope2n-api/fwd_api/response/TemplateResponse.py

44 lines
1.6 KiB
Python
Raw Normal View History

2023-11-30 11:19:06 +00:00
from rest_framework import serializers
from fwd_api.constant.common import TEMPLATE_BOX_TYPE, FolderFileType
from fwd_api.models.OcrTemplate import OcrTemplate
from fwd_api.models.OcrTemplateBox import OcrTemplateBox
2023-12-15 05:43:19 +00:00
from fwd_api.utils import file as FileUtils
2023-11-30 11:19:06 +00:00
class TemplateBoxResponse(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=False)
coordinates = serializers.SerializerMethodField()
def get_coordinates(self, obj: OcrTemplateBox):
if not obj.coordinates:
return []
list_box = obj.coordinates.split(",")
return list_box
class TemplateResponse(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=False)
data_boxs = serializers.SerializerMethodField()
anchor_boxs = serializers.SerializerMethodField()
file_url = serializers.SerializerMethodField()
def get_file_url(self, obj: OcrTemplate):
return FileUtils.build_url(FolderFileType.TEMPLATES.value, str(obj.id), obj.subscription.user.id)
def get_data_boxs(self, obj: OcrTemplate):
data = OcrTemplateBox.objects.filter(template=obj, type=TEMPLATE_BOX_TYPE.DATA.value)
if len(data) == 0:
return []
return TemplateBoxResponse(data, many=True).data
def get_anchor_boxs(self, obj: OcrTemplate):
data = OcrTemplateBox.objects.filter(template=obj, type=TEMPLATE_BOX_TYPE.ANCHOR.value)
if len(data) == 0:
return []
return TemplateBoxResponse(data, many=True).data