]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/rest/app/serializers/fields.py
bump version to 12.0.3-pve3
[ceph.git] / ceph / src / pybind / mgr / rest / app / serializers / fields.py
1
2 from distutils.version import StrictVersion
3
4 import rest_framework
5 from rest_framework import serializers
6 from rest_framework import fields
7
8
9
10 if StrictVersion(rest_framework.__version__) < StrictVersion("3.0.0"):
11 class BooleanField(serializers.BooleanField):
12 """
13 Version of BooleanField which handles fields which are 1,0
14 """
15 def to_native(self, value):
16 if isinstance(value, int) and value in [0, 1]:
17 return bool(value)
18 else:
19 super(BooleanField, self).to_native(value)
20 else:
21 # rest-framework 3 booleanfield handles 0, 1
22 BooleanField = fields.BooleanField
23
24
25 if StrictVersion(rest_framework.__version__) < StrictVersion("3.0.0"):
26 class UuidField(serializers.CharField):
27 """
28 For strings like Ceph service UUIDs and Ceph cluster FSIDs
29 """
30 type_name = "UuidField"
31 type_label = "uuid string"
32 else:
33 # rest-framework 3 has built in uuid field.
34 UuidField = fields.UUIDField
35
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()])
42 if self.help_text:
43 self.help_text += " (one of %s)" % ", ".join(self.mapping.values())
44
45 def from_native(self, value):
46 return self.reverse_mapping.get(value, value)
47
48 def to_native(self, value):
49 return self.mapping.get(value, value)
50 else:
51 #rest-framework 3 has ChoiceField
52 EnumField = fields.ChoiceField