2020-10-19 12:13:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
|
2020-10-28 04:44:19 +00:00
|
|
|
from rest_framework import serializers
|
2020-10-28 06:45:30 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-10-19 12:13:01 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2021-01-06 04:44:12 +00:00
|
|
|
from common.drf.serializers import MethodSerializer
|
|
|
|
from .attrs import category_serializer_classes_mapping, type_serializer_classes_mapping
|
2020-10-19 12:13:01 +00:00
|
|
|
|
|
|
|
from .. import models
|
|
|
|
|
|
|
|
__all__ = [
|
2021-01-06 04:44:12 +00:00
|
|
|
'ApplicationSerializer', 'ApplicationSerializerMixin',
|
2020-10-19 12:13:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
class ApplicationSerializerMixin(serializers.Serializer):
|
|
|
|
attrs = MethodSerializer()
|
2021-01-05 15:39:38 +00:00
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
def get_attrs_serializer(self):
|
2021-01-12 10:06:42 +00:00
|
|
|
serializer_class = None
|
|
|
|
if isinstance(self.instance, models.Application):
|
|
|
|
instance_type = self.instance.type
|
|
|
|
serializer_class = type_serializer_classes_mapping.get(instance_type)
|
2021-01-05 15:39:38 +00:00
|
|
|
else:
|
2021-01-12 10:06:42 +00:00
|
|
|
request = self.context['request']
|
|
|
|
query_type = request.query_params.get('type')
|
|
|
|
query_category = request.query_params.get('category')
|
|
|
|
if query_type:
|
|
|
|
serializer_class = type_serializer_classes_mapping.get(query_type)
|
|
|
|
elif query_category:
|
|
|
|
serializer_class = category_serializer_classes_mapping.get(query_category)
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
if serializer_class is None:
|
|
|
|
serializer_class = serializers.Serializer
|
|
|
|
serializer = serializer_class()
|
|
|
|
return serializer
|
2021-01-05 15:39:38 +00:00
|
|
|
|
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
class ApplicationSerializer(ApplicationSerializerMixin, BulkOrgResourceModelSerializer):
|
2020-10-28 06:45:30 +00:00
|
|
|
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category'))
|
|
|
|
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type'))
|
2020-10-28 04:44:19 +00:00
|
|
|
|
2020-10-19 12:13:01 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Application
|
|
|
|
fields = [
|
2020-10-28 04:44:19 +00:00
|
|
|
'id', 'name', 'category', 'category_display', 'type', 'type_display', 'attrs',
|
2020-10-19 12:18:47 +00:00
|
|
|
'domain', 'created_by', 'date_created', 'date_updated', 'comment'
|
2020-10-19 12:13:01 +00:00
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
'created_by', 'date_created', 'date_updated', 'get_type_display',
|
|
|
|
]
|
|
|
|
|
2021-01-03 21:27:03 +00:00
|
|
|
def validate_attrs(self, attrs):
|
|
|
|
_attrs = self.instance.attrs if self.instance else {}
|
|
|
|
_attrs.update(attrs)
|
|
|
|
return _attrs
|
2021-01-05 15:39:38 +00:00
|
|
|
|