Projet

Général

Profil

0001-misc-allow-pattern-description-in-json-schemas-54663.patch

Valentin Deniaud, 24 septembre 2021 11:20

Télécharger (5,18 ko)

Voir les différences:

Subject: [PATCH] misc: allow pattern description in json schemas (#54663)

 passerelle/apps/cmis/models.py               | 10 +++++++---
 passerelle/base/templatetags/passerelle.py   |  5 ++++-
 passerelle/contrib/caluire_axel/schemas.py   |  4 ++++
 passerelle/contrib/toulouse_smart/schemas.py |  2 +-
 tests/test_templatetags.py                   |  8 ++++++++
 5 files changed, 24 insertions(+), 5 deletions(-)
passerelle/apps/cmis/models.py
55 55
            'properties': {
56 56
                'filename': {
57 57
                    'type': 'string',
58
                    'description': _('Filename (numbers, letters and special caracters "%s" are allowed)')
59
                    % SPECIAL_CHARS,
58
                    'description': _('Filename'),
60 59
                    'pattern': FILE_NAME_PATTERN,
60
                    'pattern_description': _('Numbers, letters and special caracters "%s" are allowed.')
61
                    % SPECIAL_CHARS,
61 62
                },
62 63
                'content': {
63 64
                    'type': 'string',
......
74 75
            'type': 'string',
75 76
            'description': _('Filename (takes precendence over filename in "file" object)'),
76 77
            'pattern': FILE_NAME_PATTERN,
78
            'pattern_description': _('Numbers, letters and special caracters "%s" are allowed.')
79
            % SPECIAL_CHARS,
77 80
        },
78 81
        'path': {
79 82
            'type': 'string',
80
            'description': _('File path (with leading but not trailing slash)'),
83
            'description': _('File path'),
81 84
            'pattern': FILE_PATH_PATTERN,
85
            'pattern_description': _('Must include leading but not trailing slash.'),
82 86
        },
83 87
        'object_type': {
84 88
            'type': 'string',
passerelle/base/templatetags/passerelle.py
158 158
        min_length = schema.pop('minLength', '')
159 159
        max_length = schema.pop('maxLength', '')
160 160
        pattern = schema.pop('pattern', '')
161
        pattern_description = schema.pop('pattern_description', '')
161 162
        if enum:
162 163
            enum = mark_safe(' | '.join([format_html('<tt>{}</tt>', json.dumps(el)) for el in enum]))
163 164
        s = 'string'
......
166 167
        s = html_type(s)
167 168
        if enum:
168 169
            s += ' %s' % enum
169
        if pattern:
170
        if pattern_description:
171
            s += format_html(' <em>{}</em>', pattern_description)
172
        elif pattern:
170 173
            s += format_html(' /<tt>{}</tt>/', pattern)
171 174
        if schema:
172 175
            s += format_html('\n{!r}', schema)
passerelle/contrib/caluire_axel/schemas.py
18 18
import os
19 19

  
20 20
import xmlschema
21
from django.utils.translation import ugettext_lazy as _
21 22

  
22 23
from passerelle.contrib.utils import axel
23 24

  
......
30 31
        {
31 32
            'type': 'string',
32 33
            'pattern': '[Oo]|[Nn]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|1|0',
34
            'pattern_description': _(
35
                'Values "0", "1", "O", "N", "true" or "false" are allowed (case insensitive).'
36
            ),
33 37
        },
34 38
    ]
35 39
}
passerelle/contrib/toulouse_smart/schemas.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17

  
18 17
BOOLEAN_TYPES = [
19 18
    {'type': 'boolean'},
20 19
    {
21 20
        'type': 'string',
22 21
        'pattern': '[Oo][Uu][Ii]|[Nn][Oo][Nn]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|1|0',
22
        'pattern_description': 'Les valeurs "0", "1", "true", "false", "oui" ou "non" sont autorisées (insensibles à la casse).',
23 23
    },
24 24
]
25 25

  
tests/test_templatetags.py
69 69
        str(render_json_schema({'enum': [1, "aaa", [1]]}))
70 70
        == '<tt>1</tt> | <tt>&quot;aaa&quot;</tt> | <tt>[1]</tt>'
71 71
    )
72

  
73

  
74
def test_render_pattern_description():
75
    schema = {'type': 'object', 'properties': {'filename': {'type': 'string', 'pattern': 'abc'}}}
76
    assert 'abc' in render_json_schema(schema)
77

  
78
    schema['properties']['filename']['pattern_description'] = 'efg'
79
    assert 'abc' not in render_json_schema(schema) and 'efg' in render_json_schema(schema)
72
-