Coverage for audoma_api/serializers.py: 100%

1 from datetime import date

2

3 from audoma_api.models import (

4 Car,

5 ExampleFileModel,

6 ExampleModel,

7 ExamplePerson,

8 Manufacturer,

9)

10

11 from audoma.choices import make_choices

12 from audoma.drf import serializers

13 from audoma.drf.decorators import document_and_format

14 from audoma.drf.serializers import (

15 BulkListSerializer,

16 BulkSerializerMixin,

17)

18 from audoma.drf.validators import ExclusiveFieldsValidator

19

20

21 class NestedExampleSerializer(serializers.Serializer):

22 email = serializers.EmailField()

23

24

25 class ExampleSerializer(serializers.Serializer):

26 charfield_nolimits = serializers.CharField()

27 charfield_min_max = serializers.CharField(min_length=10, max_length=20)

28 phone_number = serializers.PhoneNumberField()

29 phone_number_example = serializers.PhoneNumberField(example="+48 123 456 789")

30 email = serializers.EmailField()

31 url = serializers.URLField()

32 boolean = serializers.BooleanField()

33 nullboolean = serializers.NullBooleanField()

34 mac_address = serializers.MACAddressField()

35 regex_mac_address = serializers.RegexField(

36 regex="^([0-9A-F]{2}:){5}([0-9A-F]{2})|([0-9A-F]{2}-){5}([0-9A-F]{2})$"

37 )

38 slug = serializers.SlugField()

39 uuid = serializers.UUIDField(format="hex")

40 # file_path = serializers.FilePathField()

41 ip_address = serializers.IPAddressField()

42 integer = serializers.IntegerField(min_value=25, max_value=30)

43 float = serializers.FloatField(min_value=1.0, max_value=4.0)

44 decimal = serializers.DecimalField(max_digits=10, decimal_places=2)

45 datetime = serializers.DateTimeField()

46 date = serializers.DateField(example=str(date.today()))

47 time = serializers.TimeField(example="12:34:56.000000")

48 duration = serializers.DurationField()

49 money = serializers.MoneyField(max_digits=10, decimal_places=2)

50 choice = serializers.ChoiceField({1: "One", 2: "Two", 3: "Three"})

51 # multiple_choice = serializers.MultipleChoiceField({1: "One", 2: "Two", 3: "Three"})

52 list_of_emails = serializers.ListField(child=serializers.EmailField())

53 dict_of_addressess = serializers.DictField(child=serializers.EmailField())

54 hstore_of_emails = serializers.HStoreField(child=serializers.EmailField())

55 json = serializers.JSONField()

56 readonly = serializers.ReadOnlyField()

57 nested = NestedExampleSerializer()

58

59 class Meta:

60 model = ExampleModel

61

62

63 class ExampleModelSerializer(serializers.ModelSerializer):

64 phone_number = serializers.SerializerMethodField()

65

66 class Meta:

67

68 model = ExampleModel

69 fields = "__all__"

70 extra_kwargs = {"char_field": {"example": "lorem ipsum"}}

71

72 @document_and_format(serializers.PhoneNumberField)

73 def get_phone_number(self, obj):

74 return obj.phone_number

75

76

77 class ExamplePersonModelSerializer(serializers.ModelSerializer):

78 class Meta:

79 model = ExamplePerson

80 fields = "__all__"

81

82

83 class ExampleFileModelSerializer(serializers.ModelSerializer):

84 class Meta:

85 model = ExampleFileModel

86 fields = "__all__"

87

88

89 class ManufacturerModelSerializer(BulkSerializerMixin, serializers.ModelSerializer):

90 class Meta:

91 model = Manufacturer

92 list_serializer_class = BulkListSerializer

93 fields = "__all__"

94

95

96 class CarModelSerializer(serializers.ModelSerializer):

97

98 choices_options_links = {

99 "manufacturer": {

100 "viewname": "manufacturer_viewset-list",

101 "value_field": "id",

102 "display_field": "name",

103 }

104 }

105

106 manufacturer = serializers.IntegerField()

107

108 class Meta:

109 model = Car

110 fields = "__all__"

111

112

113 class MutuallyExclusiveExampleSerializer(serializers.Serializer):

114 class Meta:

115 validators = [

116 ExclusiveFieldsValidator(

117 fields=[

118 "example_field",

119 "second_example_field",

120 ]

121 ),

122 ExclusiveFieldsValidator(

123 fields=[

124 "third_example_field",

125 "fourth_example_field",

126 ]

127 ),

128 ]

129

130 example_field = serializers.CharField(required=False)

131 second_example_field = serializers.CharField(required=False)

132 third_example_field = serializers.CharField(required=False)

133 fourth_example_field = serializers.CharField(required=False)

134

135 not_exclusive_field = serializers.CharField(required=False)

136 second_not_exclusive_field = serializers.CharField(required=False)

137

138

139 class ExampleModelCreateSerializer(serializers.ModelSerializer):

140 class Meta:

141 model = ExampleModel

142 fields = "__all__"

143

144 def update(self):

145 for key, item in self.validated_data.items():

146 setattr(self.instance, key, item)

147 return self.instance

148

149 def create(self):

150 return ExampleModel(

151 char_field=self.validated_data["char_field"],

152 phone_number=self.validated_data["phone_number"],

153 email=self.validated_data["email"],

154 url=self.validated_data["url"],

155 boolean=self.validated_data["boolean"],

156 nullboolean=self.validated_data["nullboolean"],

157 mac_adress=self.validated_data["mac_adress"],

158 slug=self.validated_data["slug"],

159 uuid=self.validated_data["uuid"],

160 ip_address=self.validated_data["ip_address"],

161 integer=self.validated_data["integer"],

162 _float=self.validated_data["_float"],

163 decimal=self.validated_data["decimal"],

164 datetime=self.validated_data["datetime"],

165 date=self.validated_data["date"],

166 time=self.validated_data["time"],

167 duration=self.validated_data["duration"],

168 choices=self.validated_data["choices"],

169 json=self.validated_data["json"],

170 )

171

172 def save(self, **kwargs):

173 if self.instance:

174 return self.update()

175 return self.create()

176

177

178 class ExampleOneFieldSerializer(serializers.Serializer):

179

180 RATES = make_choices("RATE", ((0, "LIKE", "Like"), (1, "DISLIKIE", "Dislike")))

181

182 rate = serializers.ChoiceField(choices=RATES.get_choices())

183

184 def save(self, **kwargs):

185 return self.validated_data