2019-07-03 14:28:20 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
from rest_framework.validators import UniqueTogetherValidator
|
|
|
|
|
|
|
|
|
|
from common.validators import ProjectUniqueValidator
|
2020-04-23 03:14:02 +00:00
|
|
|
|
from common.mixins import BulkSerializerMixin, CommonSerializerMixin
|
2019-07-03 14:28:20 +00:00
|
|
|
|
from ..utils import get_current_org_id_for_serializer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"OrgResourceSerializerMixin", "BulkOrgResourceSerializerMixin",
|
2020-08-03 08:17:46 +00:00
|
|
|
|
"BulkOrgResourceModelSerializer", "OrgResourceModelSerializerMixin",
|
2019-07-03 14:28:20 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2020-04-23 03:14:02 +00:00
|
|
|
|
class OrgResourceSerializerMixin(CommonSerializerMixin, serializers.Serializer):
|
2019-07-03 14:28:20 +00:00
|
|
|
|
"""
|
|
|
|
|
通过API批量操作资源时, 自动给每个资源添加所需属性org_id的值为current_org_id
|
|
|
|
|
(同时为serializer.is_valid()对Model的unique_together校验做准备)
|
|
|
|
|
由于HiddenField字段不可读,API获取资产信息时获取不到org_id,
|
|
|
|
|
但是coco需要资产的org_id字段,所以修改为CharField类型
|
|
|
|
|
"""
|
|
|
|
|
org_id = serializers.ReadOnlyField(default=get_current_org_id_for_serializer, label=_("Organization"))
|
|
|
|
|
org_name = serializers.ReadOnlyField(label=_("Org name"))
|
|
|
|
|
|
|
|
|
|
def get_validators(self):
|
|
|
|
|
_validators = super().get_validators()
|
|
|
|
|
validators = []
|
|
|
|
|
|
|
|
|
|
for v in _validators:
|
|
|
|
|
if isinstance(v, UniqueTogetherValidator) \
|
|
|
|
|
and "org_id" in v.fields:
|
|
|
|
|
v = ProjectUniqueValidator(v.queryset, v.fields)
|
|
|
|
|
validators.append(v)
|
|
|
|
|
return validators
|
|
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
|
fields.extend(["org_id", "org_name"])
|
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
|
class OrgResourceModelSerializerMixin(OrgResourceSerializerMixin, serializers.ModelSerializer):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2019-09-11 06:40:09 +00:00
|
|
|
|
class BulkOrgResourceSerializerMixin(BulkSerializerMixin, OrgResourceSerializerMixin):
|
2019-07-03 14:28:20 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BulkOrgResourceModelSerializer(BulkOrgResourceSerializerMixin, serializers.ModelSerializer):
|
|
|
|
|
pass
|