class ConfigSerializer(serializers.ModelSerializer): """Serialize a :class:`~api.models.Config` model.""" owner = serializers.Field(source='owner.username') app = serializers.SlugRelatedField(slug_field='id') values = serializers.ModelField( model_field=models.Config()._meta.get_field('values'), required=False) class Meta: """Metadata options for a :class:`ConfigSerializer`.""" model = models.Config read_only_fields = ('uuid', 'created', 'updated')
class ConfigSerializer(serializers.ModelSerializer): """Serialize a :class:`~api.models.Config` model.""" owner = serializers.Field(source='owner.username') app = serializers.SlugRelatedField(slug_field='id') values = serializers.ModelField( model_field=models.Config()._meta.get_field('values'), required=False) memory = serializers.ModelField( model_field=models.Config()._meta.get_field('memory'), required=False) cpu = serializers.ModelField( model_field=models.Config()._meta.get_field('cpu'), required=False) tags = serializers.ModelField( model_field=models.Config()._meta.get_field('tags'), required=False) class Meta: """Metadata options for a :class:`ConfigSerializer`.""" model = models.Config read_only_fields = ('uuid', 'created', 'updated') def validate_memory(self, attrs, source): for k, v in attrs.get(source, {}).items(): if v is None: # use NoneType to unset a value continue if not re.match(PROCTYPE_MATCH, k): raise serializers.ValidationError( "Process types can only contain [a-z]") if not re.match(MEMLIMIT_MATCH, str(v)): raise serializers.ValidationError( "Limit format: <number><unit>, where unit = B, K, M or G") return attrs def validate_cpu(self, attrs, source): for k, v in attrs.get(source, {}).items(): if v is None: # use NoneType to unset a value continue if not re.match(PROCTYPE_MATCH, k): raise serializers.ValidationError( "Process types can only contain [a-z]") shares = re.match(CPUSHARE_MATCH, str(v)) if not shares: raise serializers.ValidationError( "CPU shares must be an integer") for v in shares.groupdict().values(): try: i = int(v) except ValueError: raise serializers.ValidationError( "CPU shares must be an integer") if i > 1024 or i < 0: raise serializers.ValidationError( "CPU shares must be between 0 and 1024") return attrs def validate_tags(self, attrs, source): for k, v in attrs.get(source, {}).items(): if v is None: # use NoneType to unset a value continue if not re.match(TAGKEY_MATCH, k): raise serializers.ValidationError( "Tag keys can only contain [a-z]") if not re.match(TAGVAL_MATCH, str(v)): raise serializers.ValidationError("Invalid tag value") return attrs