File: fields.py

package info (click to toggle)
python-djangorestframework-yaml 3.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: python: 483; makefile: 11
file content (58 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Custom DRF fields for use in serialization when using the YAMLRenderer.

These fields are used to render the YAML in a more readable way.
"""
from typing import Any

from rest_framework import fields

from . import styles


class FlowStyleDictField(fields.DictField):
    """A DRF field which renders as a YAML flow style mapping."""

    def to_representation(self, value: Any) -> dict[Any, Any]:
        """Render a flow style mapping."""
        return styles.FlowStyleMapping(super().to_representation(value))


class FlowStyleListField(fields.ListField):
    """A DRF field which renders as a YAML flow style sequence."""

    def to_representation(self, value: Any) -> list[Any]:
        """Render a flow style sequence."""
        return styles.FlowStyleSequence(super().to_representation(value))


class SingleQuotedCharField(fields.CharField):
    """A DRF field which renders as a YAML single quoted string."""

    def to_representation(self, value: Any) -> str:
        """Render a single quoted string."""
        return styles.SingleQuotedStr(super().to_representation(value))


class DoubleQuotedCharField(fields.CharField):
    """A DRF field which renders as a YAML double quoted string."""

    def to_representation(self, value: Any) -> str:
        """Render a double quoted string."""
        return styles.DoubleQuotedStr(super().to_representation(value))


class FoldedCharField(fields.CharField):
    """A DRF field which renders as a YAML folded string."""

    def to_representation(self, value: Any) -> str:
        """Render a folded string."""
        return styles.FoldedStr(super().to_representation(value))


class LiteralCharField(fields.CharField):
    """A DRF field which renders as a YAML literal string."""

    def to_representation(self, value: Any) -> str:
        """Render a literal string."""
        return styles.LiteralStr(super().to_representation(value))