2023-04-18 09:07:01 +00:00
|
|
|
from django.db.models import QuerySet
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from assets.models import Custom, Platform, Asset
|
|
|
|
from common.const import UUID_PATTERN
|
|
|
|
from common.serializers import MethodSerializer, create_serializer_class
|
2023-04-20 07:23:00 +00:00
|
|
|
from common.serializers.common import DictSerializer
|
2023-04-10 02:57:44 +00:00
|
|
|
from .common import AssetSerializer
|
|
|
|
|
|
|
|
__all__ = ['CustomSerializer']
|
|
|
|
|
|
|
|
|
|
|
|
class CustomSerializer(AssetSerializer):
|
2023-04-18 09:07:01 +00:00
|
|
|
custom_info = MethodSerializer(label=_('Custom info'))
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
class Meta(AssetSerializer.Meta):
|
|
|
|
model = Custom
|
2023-04-18 09:07:01 +00:00
|
|
|
fields = AssetSerializer.Meta.fields + ['custom_info']
|
|
|
|
|
|
|
|
def get_custom_info_serializer(self):
|
|
|
|
request = self.context.get('request')
|
2023-04-20 07:23:00 +00:00
|
|
|
default_field = DictSerializer()
|
2023-04-18 09:07:01 +00:00
|
|
|
|
|
|
|
if not request:
|
|
|
|
return default_field
|
|
|
|
|
|
|
|
if self.instance and isinstance(self.instance, (QuerySet, list)):
|
|
|
|
return default_field
|
|
|
|
|
|
|
|
if not self.instance and UUID_PATTERN.findall(request.path):
|
|
|
|
pk = UUID_PATTERN.findall(request.path)[0]
|
|
|
|
self.instance = Asset.objects.filter(id=pk).first()
|
|
|
|
|
|
|
|
platform = None
|
|
|
|
if self.instance:
|
|
|
|
platform = self.instance.platform
|
|
|
|
elif request.query_params.get('platform'):
|
|
|
|
platform_id = request.query_params.get('platform')
|
|
|
|
platform_id = int(platform_id) if platform_id.isdigit() else 0
|
|
|
|
platform = Platform.objects.filter(id=platform_id).first()
|
|
|
|
|
|
|
|
if not platform:
|
|
|
|
return default_field
|
|
|
|
custom_fields = platform.custom_fields
|
|
|
|
if not custom_fields:
|
|
|
|
return default_field
|
|
|
|
name = platform.name.title() + 'CustomSerializer'
|
|
|
|
return create_serializer_class(name, custom_fields)()
|