class ListElasticGroupSerializer(serializers.ModelSerializer): id = serializers.CharField(max_length=12) name = serializers.SerializerMethodField() min = serializers.SerializerMethodField() max = serializers.SerializerMethodField() cd = serializers.SerializerMethodField() status = serializers.SerializerMethodField() class Meta: model = ElasticGroup fields = ('id', 'name', 'min', 'max', 'cd', 'status') def get_name(self, ins): return ins.info_name def get_min(self, ins): return ins.info['min'] def get_max(self, ins): return ins.info['max'] def get_cd(self, ins): return ins.info['cd'] def get_status(self, ins): return ElasticGroupStatus[ins.status]
class DescribePortalorderSerializer(serializers.ModelSerializer): class Meta: model = PortalOrder fields = ("id", "type", "creator_id", "lessee_id", "seller_name", "seller_id", "duration_type", "duration_value", "cabinet_count", "platform_type", "platform_version", "contract_money", "contract_id", "deploy_id", "create_datetime", "expire_at", "expire_in", "lessee_name" ) # noqa create_datetime = serializers.SerializerMethodField() expire_at = serializers.SerializerMethodField() expire_in = serializers.SerializerMethodField() lessee_name = serializers.SerializerMethodField() def get_create_datetime(self, obj): return time.mktime( obj.create_datetime.astimezone( timezone.get_current_timezone()).timetuple()) def get_expire_at(self, obj): return time.mktime( obj.expire_at.astimezone( timezone.get_current_timezone()).timetuple()) def get_expire_in(self, obj): delta = obj.expire_at - timezone.now() return int(math.ceil(delta.total_seconds() / 86400)) def get_lessee_name(self, obj): account = AccountService.get_by_owner(obj.lessee_id) return account and account.name or '-'
class DevopsInstanceSerializer(serializers.ModelSerializer): """ 主机序列化 """ instance_info = serializers.SerializerMethodField() class Meta: model = models.InstancesModel fields = ("instance_id", "uuid", "role", "instance_info", "charge_mode", "create_datetime") def get_instance_info(self, obj): instance_id = obj.instance_id owner = obj.user.username zone = obj.zone.name action = "DescribeInstance" _payload = { "action": action, "zone": zone, "owner": owner, "instances": [instance_id], } if obj.deleted: return {} resp = helper.describe_instances(_payload) if resp.get("ret_code") == 0: return resp.get("ret_set")[0] else: return {}
class DescribeNoticeInfoSerializer(serializers.ModelSerializer): class Meta: model = NoticeModel fields = ('msgid', 'title', 'content', 'author') author = serializers.SerializerMethodField() def get_author(self, obj): return AccountService.get_by_owner(obj.author).name
class DescribeLoadbalancerSerializer(serializers.ModelSerializer): id = serializers.CharField(max_length=12) status = serializers.SerializerMethodField() class Meta: model = ElasticGroup fields = ('id', 'status') def get_status(self, ins): return ElasticGroupStatus[ins.status]
class DescribeNoticeSerializer(serializers.ModelSerializer): class Meta: model = NoticeModel fields = ('msgid', 'title', 'author', 'commit_time') commit_time = serializers.SerializerMethodField() author = serializers.SerializerMethodField() def get_author(self, obj): return AccountService.get_by_owner(obj.author).name def get_commit_time(self, obj): """ 修改utc时间戳为utc+8 :param obj: :return: """ return time.mktime( obj.commit_time.astimezone( timezone.get_current_timezone()).timetuple())
class PortalAccountSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Account fields = ("user", "email", "cell_phone", "nickname", "name", "member_type") cell_phone = serializers.SerializerMethodField() nickname = serializers.SerializerMethodField() member_type = serializers.SerializerMethodField() def get_member_type(self, obj): return dict(AccountType.CHOICES).get(obj.type, 'tenant') def get_nickname(self, obj): return obj.name or obj.email[:obj.email.find('@')] def get_cell_phone(self, obj): return obj.phone
class DetailElasticGroupSerializer(serializers.ModelSerializer): id = serializers.CharField(max_length=12) status = serializers.SerializerMethodField() info = ElasticGroupInfoValidator() config = ElasticGroupConfigValidator() trigger = ElasticGroupTriggerValidator() class Meta: model = ElasticGroup fields = ('id', 'info', 'config', 'trigger', 'status') def get_status(self, ins): return ElasticGroupStatus[ins.status]
class FrameSerializer(serializers.ModelSerializer): class Meta: model = FrameModel fields = ('frame_id', 'ref_action', 'rank', 'meta_data') meta_data = serializers.SerializerMethodField() def get_meta_data(self, obj): try: info = json.loads(obj.meta_data) except Exception as exp: info = {} logger.error('exp when json meta_data, detail: %s', exp) return info
class BoardSerializer(serializers.ModelSerializer): class Meta: model = BoardModel fields = ('board_id', 'name', 'background', 'location', 'current', 'frame_info') frame_info = serializers.SerializerMethodField() def get_frame_info(self, obj): if obj.current: frame_list = FrameModel.objects.describe_frame( obj.related_user.username, obj.board_id) info = FrameListSerializer(frame_list).data else: info = {} return info
class FinanceAccountSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Account fields = ( "user", "email", "cell_phone", "nickname", "name", "company", ) cell_phone = serializers.SerializerMethodField() def get_cell_phone(self, obj): return obj.phone
class ListInstanceSuitesSerializer(serializers.ModelSerializer): class Meta: model = Suite fields = ('id', 'name', 'passwd', 'image', 'system', 'cpu', 'volume', 'memory', 'net') passwd = serializers.SerializerMethodField() image = serializers.SerializerMethodField() system = serializers.SerializerMethodField() cpu = serializers.SerializerMethodField() memory = serializers.SerializerMethodField() volume = serializers.SerializerMethodField() net = serializers.SerializerMethodField() def get_cpu(self, ins): return int(ins.config.get('cpu')) def get_memory(self, ins): return int(ins.config.get('memory')) def get_system(self, ins): return int(ins.config.get('sys')) def get_image(self, ins): image = SuiteService.get_default_image(ins.vtype, ins.zone.name, ins.owner) return image['name'] if image else '没有可用的操作系统镜像' def get_volume(self, ins): volume = ins.config.get('volume') return dict(capacity=int(volume['capacity'])) def get_net(self, ins): default = dict( cidr='169.254.0.0/16', name='没有可用的网络', ) net = SuiteService.get_default_net(ins.vtype, ins.zone.name, ins.owner) return net or default def get_passwd(self, ins): passwd = ins.config.get('passwd') if not passwd: passwd = get_random_string(8) return passwd