Projet

Général

Profil

0011-schemas-add-an-absent_label-property-to-Dimension-38.patch

Benjamin Dauvergne, 03 décembre 2019 13:53

Télécharger (4,69 ko)

Voir les différences:

Subject: [PATCH 11/13] schemas: add an absent_label property to Dimension
 (#38067)

Also force lazy strings to unicode in to_json().
 bijoe/engine.py       |  2 ++
 bijoe/schemas.py      | 18 +++++++++++++++++-
 tests/test_schemas.py | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 tests/test_schemas.py
bijoe/engine.py
23 23
import psycopg2
24 24

  
25 25
from django.core.cache import cache
26
from django.utils.translation import ugettext_lazy as _
26 27

  
27 28
from . import schemas
28 29

  
......
138 139
                                  'OR ({fact_table}.%s->>\'%s\') IN (%%s))'
139 140
                                  % (json_field, name))
140 141
        self.filter_needs_join = False
142
        self.absent_label = _('None')
141 143

  
142 144

  
143 145
class EngineJSONDimension(EngineDimension):
bijoe/schemas.py
21 21
import collections
22 22

  
23 23
from django.utils import six
24
from django.utils.functional import Promise
25
from django.utils.translation import ugettext_lazy as _
24 26

  
25 27
from .relative_time import RelativeDate
26 28

  
......
116 118
                    v = types[attr].to_json(v)
117 119
                if isinstance(v, list):
118 120
                    v = [x.to_json() if hasattr(x, 'to_json') else x for x in v]
121
                if isinstance(v, Promise):
122
                    v = unicode(v)
119 123
                d[attr] = v
120 124
        return d
121 125

  
......
137 141
class Dimension(Base):
138 142
    __slots__ = ['name', 'label', 'type', 'join', 'value', 'value_label',
139 143
                 'order_by', 'group_by', 'filter_in_join', 'filter', 'filter_value',
140
                 'filter_needs_join', 'filter_expression']
144
                 'filter_needs_join', 'filter_expression', 'absent_label']
141 145
    __types__ = {
142 146
        'name': str,
143 147
        'label': unicode,
......
153 157
        'filter_in_join': bool,
154 158
        'filter_value': str,
155 159
        'filter_needs_join': bool,
160
        'absent_label': unicode,
156 161
    }
157 162

  
158 163
    label = None
......
166 171
    filter_expression = None
167 172
    filter_needs_join = True
168 173
    members_query = None
174
    absent_label = None
175

  
176
    def __init__(self, **kwargs):
177
        super(Dimension, self).__init__(**kwargs)
178
        if not self.absent_label:
179
            if self.type in ('date', 'integer', 'string'):
180
                self.absent_label = _('None')
181
            elif self.type in ('bool',):
182
                self.absent_label = _('N/A')
183
            else:
184
                raise NotImplementedError('not absent label for type %r' % self.type)
169 185

  
170 186
    @property
171 187
    def dimensions(self):
tests/test_schemas.py
1
# bijoe - BI dashboard
2
# Copyright (C) 2019  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17

  
18
from __future__ import unicode_literals
19

  
20
import pytest
21

  
22
from django.utils.translation import ugettext as _
23

  
24
from bijoe.schemas import (
25
    Dimension,
26
)
27

  
28

  
29
def test_absent_label():
30
    assert Dimension.from_json({'name': 'x', 'value': 'x', 'type': 'integer'}).absent_label == _('None')
31
    assert Dimension.from_json({'name': 'x', 'value': 'x', 'type': 'string'}).absent_label == _('None')
32
    assert Dimension.from_json({'name': 'x', 'value': 'x', 'type': 'bool'}).absent_label == _('N/A')
33
    assert Dimension.from_json(
34
        {'name': 'x', 'value': 'x', 'type': 'boolean', 'absent_label': 'coin'}).absent_label == 'coin'
35

  
36
    with pytest.raises(NotImplementedError):
37
        Dimension.from_json({'name': 'x', 'value': 'x', 'type': 'coin'}).absent_label
38

  
0
-