class VTRTenantIdSerializer(serializers.ModelSerializer, PlusSerializerMixin): id = ReadOnlyField() target_id = serializers.IntegerField() test = serializers.CharField() scope = serializers.CharField() argument = serializers.CharField(required=False) provider_service = serializers.PrimaryKeyRelatedField( queryset=VTRService.get_service_objects().all(), default=get_default_vtr_service) result = serializers.CharField(required=False) result_code = serializers.CharField(required=False) backend_status = ReadOnlyField() humanReadableName = serializers.SerializerMethodField( "getHumanReadableName") is_synced = serializers.SerializerMethodField("isSynced") class Meta: model = VTRTenant fields = ('humanReadableName', 'id', 'provider_service', 'target_id', 'scope', 'test', 'argument', 'result', 'result_code', 'is_synced', 'backend_status') def getHumanReadableName(self, obj): return obj.__unicode__() def isSynced(self, obj): return (obj.enacted is not None) and (obj.enacted >= obj.updated)
def get_vtr_service(self, o): if not o.provider_service: return None vtrs = VTRService.get_service_objects().filter( id=o.provider_service.id) if not vtrs: return None return vtrs[0]
def get_default_vtr_service(): vtr_services = VTRService.get_service_objects().all() if vtr_services: return vtr_services[0] return None