|
6 | 6 | @date:2024/3/1 14:30 |
7 | 7 | @desc: |
8 | 8 | """ |
9 | | -from django.utils.translation import gettext_lazy |
| 9 | +from django.utils.functional import lazy |
| 10 | +from rest_framework import serializers |
| 11 | + |
| 12 | + |
| 13 | +def value_(field, value): |
| 14 | + return f"【{field}】 {value}" |
| 15 | + |
| 16 | + |
| 17 | +def reset_messages(field, messages): |
| 18 | + return {key: lazy(value_, str)(field, messages.get(key)) for key in messages} |
| 19 | + |
| 20 | + |
| 21 | +def reset_message_by_field(field_text, field): |
| 22 | + return reset_messages(field_text, {**field.default_error_messages, **field.__bases__[0].default_error_messages}) |
10 | 23 |
|
11 | 24 |
|
12 | 25 | class ErrMessage: |
13 | 26 | @staticmethod |
14 | 27 | def char(field: str): |
15 | | - return { |
16 | | - 'invalid': gettext_lazy("【%s】不是有效的字符串。" % field), |
17 | | - 'blank': gettext_lazy("【%s】此字段不能为空字符串。" % field), |
18 | | - 'max_length': gettext_lazy("【%s】请确保此字段的字符数不超过 {max_length} 个。" % field), |
19 | | - 'min_length': gettext_lazy("【%s】请确保此字段至少包含 {min_length} 个字符。" % field), |
20 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
21 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field) |
22 | | - } |
| 28 | + return reset_message_by_field(field, serializers.CharField) |
23 | 29 |
|
24 | 30 | @staticmethod |
25 | 31 | def uuid(field: str): |
26 | | - return {'required': gettext_lazy('【%s】此字段必填。' % field), |
27 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
28 | | - 'invalid': gettext_lazy("【%s】必须是有效的UUID。" % field), |
29 | | - } |
| 32 | + return reset_messages(field, serializers.UUIDField.default_error_messages) |
30 | 33 |
|
31 | 34 | @staticmethod |
32 | 35 | def integer(field: str): |
33 | | - return {'invalid': gettext_lazy('【%s】必须是有效的integer。' % field), |
34 | | - 'max_value': gettext_lazy('【%s】请确保此值小于或等于 {max_value} 。' % field), |
35 | | - 'min_value': gettext_lazy('【%s】请确保此值大于或等于 {min_value} 。' % field), |
36 | | - 'max_string_length': gettext_lazy('【%s】字符串值太大。') % field, |
37 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
38 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
39 | | - } |
| 36 | + return reset_messages(field, serializers.IntegerField.default_error_messages) |
40 | 37 |
|
41 | 38 | @staticmethod |
42 | 39 | def list(field: str): |
43 | | - return {'not_a_list': gettext_lazy('【%s】应为列表,但得到的类型为 "{input_type}".' % field), |
44 | | - 'empty': gettext_lazy('【%s】此列表不能为空。' % field), |
45 | | - 'min_length': gettext_lazy('【%s】请确保此字段至少包含 {min_length} 个元素。' % field), |
46 | | - 'max_length': gettext_lazy('【%s】请确保此字段的元素不超过 {max_length} 个。' % field), |
47 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
48 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
49 | | - } |
| 40 | + return reset_messages(field, serializers.ListField.default_error_messages) |
50 | 41 |
|
51 | 42 | @staticmethod |
52 | 43 | def boolean(field: str): |
53 | | - return {'invalid': gettext_lazy('【%s】必须是有效的布尔值。' % field), |
54 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
55 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field)} |
| 44 | + return reset_messages(field, serializers.BooleanField.default_error_messages) |
56 | 45 |
|
57 | 46 | @staticmethod |
58 | 47 | def dict(field: str): |
59 | | - return {'not_a_dict': gettext_lazy('【%s】应为字典,但得到的类型为 "{input_type}' % field), |
60 | | - 'empty': gettext_lazy('【%s】能是空的。' % field), |
61 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
62 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
63 | | - } |
| 48 | + return reset_messages(field, serializers.DictField.default_error_messages) |
64 | 49 |
|
65 | 50 | @staticmethod |
66 | 51 | def float(field: str): |
67 | | - return {'invalid': gettext_lazy('【%s】需要一个有效的数字。' % field), |
68 | | - 'max_value': gettext_lazy('【%s】请确保此值小于或等于 {max_value}。' % field), |
69 | | - 'min_value': gettext_lazy('【%s】请确保此值大于或等于 {min_value}。' % field), |
70 | | - 'max_string_length': gettext_lazy('【%s】字符串值太大。' % field), |
71 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
72 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
73 | | - } |
| 52 | + return reset_messages(field, serializers.FloatField.default_error_messages) |
74 | 53 |
|
75 | 54 | @staticmethod |
76 | 55 | def json(field: str): |
77 | | - return { |
78 | | - 'invalid': gettext_lazy('【%s】值必须是有效的JSON。' % field), |
79 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
80 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
81 | | - } |
| 56 | + return reset_messages(field, serializers.JSONField.default_error_messages) |
82 | 57 |
|
83 | 58 | @staticmethod |
84 | 59 | def base(field: str): |
85 | | - return { |
86 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
87 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
88 | | - } |
| 60 | + return reset_messages(field, serializers.Field.default_error_messages) |
89 | 61 |
|
90 | 62 | @staticmethod |
91 | 63 | def date(field: str): |
92 | | - return { |
93 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
94 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
95 | | - 'invalid': gettext_lazy('【%s】日期格式错误,请改用以下格式之一: {format}。'), |
96 | | - 'datetime': gettext_lazy('【%s】应为日期,但得到的是日期时间。') |
97 | | - } |
| 64 | + return reset_messages(field, serializers.DateField.default_error_messages) |
98 | 65 |
|
99 | 66 | @staticmethod |
100 | 67 | def image(field: str): |
101 | | - return { |
102 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
103 | | - 'null': gettext_lazy('【%s】此字段不能为null。' % field), |
104 | | - 'invalid_image': gettext_lazy('您上载的【%s】文件不是图像或图像已损坏,请上载有效的图像。' % field), |
105 | | - 'max_length': gettext_lazy('【%s】请确保此文件名最多包含 {max_length} 个字符(长度为 {length})。' % field), |
106 | | - 'invalid': gettext_lazy('【%s】提交的数据不是文件,请检查表单上的编码类型。' % field) |
107 | | - } |
| 68 | + return reset_messages(field, serializers.ImageField.default_error_messages) |
108 | 69 |
|
109 | 70 | @staticmethod |
110 | 71 | def file(field: str): |
111 | | - return { |
112 | | - 'required': gettext_lazy('【%s】此字段必填。' % field), |
113 | | - 'empty': gettext_lazy('【%s】提交的文件为空。' % field), |
114 | | - 'invalid': gettext_lazy('【%s】提交的数据不是文件,请检查表单上的编码类型。' % field), |
115 | | - 'no_name': gettext_lazy('【%s】无法确定任何文件名。' % field), |
116 | | - 'max_length': gettext_lazy('【%s】请确保此文件名最多包含 {max_length} 个字符(长度为 {length})。' % field) |
117 | | - } |
| 72 | + return reset_messages(field, serializers.FileField.default_error_messages) |
0 commit comments