Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
2
Indexable
Never
class DelimitedList(fields.List):
    delimiter = ','

    def __init__(self, cls_or_instance, delimiter=None, as_string=False, **kwargs):
        self.delimiter = delimiter or self.delimiter
        self.as_string = as_string
        super(DelimitedList, self).__init__(cls_or_instance, **kwargs)

    def _serialize(self, value, attr, obj, **kwargs):
        ret = super(DelimitedList, self)._serialize(value, attr, obj)
        if self.as_string:
            return self.delimiter.join(format(each) for each in value)
        return ret

    def _deserialize(self, value, attr, data, **kwargs):
        ret = (
            value
            if utils.is_iterable_but_not_string(value)
            else value.split(self.delimiter)
        )
        return super(DelimitedList, self)._deserialize(ret, attr, data)


class ListID(Schema):
    list_id = StringUUID(required=True, example=list_id_example)


class ListIDs(Schema):
    list_ids = DelimitedList(StringUUID, required=True, example=[list_id_example])