]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/rest/app/serializers/fields.py
2 from distutils
.version
import StrictVersion
5 from rest_framework
import serializers
6 from rest_framework
import fields
10 if StrictVersion(rest_framework
.__version
__) < StrictVersion("3.0.0"):
11 class BooleanField(serializers
.BooleanField
):
13 Version of BooleanField which handles fields which are 1,0
15 def to_native(self
, value
):
16 if isinstance(value
, int) and value
in [0, 1]:
19 super(BooleanField
, self
).to_native(value
)
21 # rest-framework 3 booleanfield handles 0, 1
22 BooleanField
= fields
.BooleanField
25 if StrictVersion(rest_framework
.__version
__) < StrictVersion("3.0.0"):
26 class UuidField(serializers
.CharField
):
28 For strings like Ceph service UUIDs and Ceph cluster FSIDs
30 type_name
= "UuidField"
31 type_label
= "uuid string"
33 # rest-framework 3 has built in uuid field.
34 UuidField
= fields
.UUIDField
36 if StrictVersion(rest_framework
.__version
__) < StrictVersion("3.0.0"):
37 class EnumField(serializers
.CharField
):
38 def __init__(self
, mapping
, *args
, **kwargs
):
39 super(EnumField
, self
).__init
__(*args
, **kwargs
)
40 self
.mapping
= mapping
41 self
.reverse_mapping
= dict([(v
, k
) for (k
, v
) in self
.mapping
.items()])
43 self
.help_text
+= " (one of %s)" % ", ".join(self
.mapping
.values())
45 def from_native(self
, value
):
46 return self
.reverse_mapping
.get(value
, value
)
48 def to_native(self
, value
):
49 return self
.mapping
.get(value
, value
)
51 #rest-framework 3 has ChoiceField
52 EnumField
= fields
.ChoiceField