• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taigaio / taiga-back / 18139108398

30 Sep 2025 06:03PM UTC coverage: 81.938% (-0.005%) from 81.943%
18139108398

push

github

migonzalvar
Delete obsolete test

21222 of 25900 relevant lines covered (81.94%)

3.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.19
/taiga/base/fields.py
1
# -*- coding: utf-8 -*-
2
# This Source Code Form is subject to the terms of the Mozilla Public
3
# License, v. 2.0. If a copy of the MPL was not distributed with this
4
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
#
6
# Copyright (c) 2021-present Kaleidos INC
7

8
from django.forms import widgets
4✔
9
from django.utils.translation import gettext as _
4✔
10
from taiga.base.api import serializers, ISO_8601
4✔
11
from taiga.base.api.settings import api_settings
4✔
12

13
import serpy
4✔
14

15

16
####################################################################
17
# DRF Serializer fields (OLD)
18
####################################################################
19
# NOTE: This should be in other place, for example taiga.base.api.serializers
20

21

22
class JSONField(serializers.WritableField):
4✔
23
    """
24
    Json objects serializer.
25
    """
26
    widget = widgets.Textarea
4✔
27

28
    def to_native(self, obj):
4✔
29
        return obj
×
30

31
    def from_native(self, data):
4✔
32
        return data
4✔
33

34

35
class PgArrayField(serializers.WritableField):
4✔
36
    """
37
    PgArray objects serializer.
38
    """
39
    widget = widgets.Textarea
4✔
40

41
    def to_native(self, obj):
4✔
42
        return obj
×
43

44
    def from_native(self, data):
4✔
45
        return data
4✔
46

47

48
class PickledObjectField(serializers.WritableField):
4✔
49
    """
50
    PickledObjectField objects serializer.
51
    """
52
    widget = widgets.Textarea
4✔
53

54
    def to_native(self, obj):
4✔
55
        return obj
×
56

57
    def from_native(self, data):
4✔
58
        return data
×
59

60

61
class WatchersField(serializers.WritableField):
4✔
62
    def to_native(self, obj):
4✔
63
        return obj
×
64

65
    def from_native(self, data):
4✔
66
        return data
4✔
67

68

69
class ListField(serializers.WritableField):
4✔
70
    """
71
    A field whose values are lists of items described by the given child. The child can
72
    be another field type (e.g., CharField) or a serializer. However, for serializers, you should
73
    instead just use it with the `many=True` option.
74
    """
75

76
    default_error_messages = {
4✔
77
        'invalid_type': _('%(value)s is not a list'),
78
    }
79
    empty = []
4✔
80

81
    def __init__(self, child=None, *args, **kwargs):
4✔
82
        super().__init__(*args, **kwargs)
4✔
83
        self.child = child
4✔
84

85
    def initialize(self, parent, field_name):
4✔
86
        super().initialize(parent, field_name)
4✔
87
        if self.child:
4✔
88
            self.child.initialize(parent, field_name)
4✔
89

90
    def to_native(self, obj):
4✔
91
        if self.child and obj:
4✔
92
            return [self.child.to_native(item) for item in obj]
4✔
93
        return obj
×
94

95
    def from_native(self, data):
4✔
96
        self.validate_is_list(data)
4✔
97
        if self.child and data:
4✔
98
            return [self.child.from_native(item_data) for item_data in data]
4✔
99
        return data
×
100

101
    def validate(self, value):
4✔
102
        super().validate(value)
4✔
103

104
        self.validate_is_list(value)
4✔
105

106
        if self.child:
4✔
107
            errors = {}
4✔
108
            for index, item in enumerate(value):
4✔
109
                try:
4✔
110
                    self.child.validate(item)
4✔
111
                except ValidationError as e:
×
112
                    errors[index] = e.messages
×
113

114
            if errors:
4✔
115
                raise NestedValidationError(errors)
×
116

117
    def run_validators(self, value):
4✔
118
        super().run_validators(value)
4✔
119

120
        if self.child:
4✔
121
            errors = {}
4✔
122
            for index, item in enumerate(value):
4✔
123
                try:
4✔
124
                    self.child.run_validators(item)
4✔
125
                except ValidationError as e:
×
126
                    errors[index] = e.messages
×
127

128
            if errors:
4✔
129
                raise NestedValidationError(errors)
×
130

131
    def validate_is_list(self, value):
4✔
132
        if value is not None and not isinstance(value, list):
4✔
133
            raise ValidationError(self.error_messages['invalid_type'],
×
134
                                  code='invalid_type',
135
                                  params={'value': value})
136

137

138
####################################################################
139
# Serpy fields (NEW)
140
####################################################################
141

142
class Field(serpy.Field):
4✔
143
    pass
4✔
144

145

146
class MethodField(serpy.MethodField):
4✔
147
    pass
4✔
148

149

150
class I18NField(Field):
4✔
151
    def to_value(self, value):
4✔
152
        ret = super(I18NField, self).to_value(value)
4✔
153
        return _(ret)
4✔
154

155

156
class I18NJSONField(Field):
4✔
157
    """
158
    Json objects serializer.
159
    """
160
    def __init__(self, i18n_fields=(), *args, **kwargs):
4✔
161
        super(I18NJSONField, self).__init__(*args, **kwargs)
4✔
162
        self.i18n_fields = i18n_fields
4✔
163

164
    def translate_values(self, d):
4✔
165
        i18n_d = {}
×
166
        if d is None:
×
167
            return d
×
168

169
        for key, value in d.items():
×
170
            if isinstance(value, dict):
×
171
                i18n_d[key] = self.translate_values(value)
×
172

173
            if key in self.i18n_fields:
×
174
                if isinstance(value, list):
×
175
                    i18n_d[key] = [e is not None and _(str(e)) or e for e in value]
×
176
                if isinstance(value, str):
×
177
                    i18n_d[key] = value is not None and _(value) or value
×
178
            else:
179
                i18n_d[key] = value
×
180

181
        return i18n_d
×
182

183
    def to_native(self, obj):
4✔
184
        i18n_obj = self.translate_values(obj)
×
185
        return i18n_obj
×
186

187

188
class FileField(Field):
4✔
189
    def to_value(self, value):
4✔
190
        if value:
4✔
191
            return value.name
4✔
192
        return None
×
193

194

195
class DateTimeField(Field):
4✔
196
    format = api_settings.DATETIME_FORMAT
4✔
197

198
    def to_value(self, value):
4✔
199
        if value is None or self.format is None:
4✔
200
            return value
4✔
201

202
        if self.format.lower() == ISO_8601:
4✔
203
            ret = value.isoformat()
×
204
            if ret.endswith("+00:00"):
×
205
                ret = ret[:-6] + "Z"
×
206
            return ret
×
207
        return value.strftime(self.format)
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc