Projet

Général

Profil

0001-templatetags-pass-unicode-to-html_format-37880.patch

Emmanuel Cazenave, 22 novembre 2019 12:29

Télécharger (6,59 ko)

Voir les différences:

Subject: [PATCH] templatetags: pass unicode to html_format (#37880)

 passerelle/base/templatetags/passerelle.py | 40 +++++++++++-----------
 tests/test_templatetags.py                 | 20 ++++++++++-
 2 files changed, 39 insertions(+), 21 deletions(-)
passerelle/base/templatetags/passerelle.py
114 114
            return mark_safe('<em>%s</em>') % _('always valid')
115 115
        if schema is False:
116 116
            return mark_safe('<em>%s</em>') % _('always invalid')
117
        return format_html('<tt>{!r}</tt>', schema)
117
        return format_html(u'<tt>{!r}</tt>', schema)
118 118

  
119 119
    def many_of(name, schemas):
120
        s = format_html('<b>{}</b>', name)
120
        s = format_html(u'<b>{}</b>', name)
121 121
        parts = [render_json_schema(schema) for schema in schemas]
122 122
        if any('\n' in part for part in parts):
123 123
            s += '<ul>'
124 124
            for part in parts:
125
                s += format_html('<li>{0}</li>\n', part)
125
                s += format_html(u'<li>{0}</li>\n', part)
126 126
            s += '</ul>'
127 127
        else:
128 128
            s += ' [ ' + ' | '.join(parts) + ' ]'
......
153 153
        pattern = schema.pop('pattern', '')
154 154
        if enum:
155 155
            enum = mark_safe(' | '.join(
156
                [format_html('"<tt>{}</tt>"', el) for el in enum]))
156
                [format_html(u'"<tt>{}</tt>"', el) for el in enum]))
157 157
        s = '<b>string'
158 158
        if max_length or min_length:
159
            s += format_html('[{0}:{1}]', min_length, max_length)
159
            s += format_html(u'[{0}:{1}]', min_length, max_length)
160 160
        s += '</b>'
161 161
        if enum:
162 162
            s += ' %s' % enum
163 163
        if pattern:
164
            s += format_html(' /<tt>{}</tt>/', pattern)
164
            s += format_html(u' /<tt>{}</tt>/', pattern)
165 165
        if schema:
166
            s += format_html('\n{!r}', schema)
166
            s += format_html(u'\n{!r}', schema)
167 167
        return mark_safe(s)
168 168
    if typ == 'integer':
169 169
        if not schema:
......
184 184
        required_keys = schema.pop('required', [])
185 185
        additional_properties = schema.pop('additionalProperties', True)
186 186
        if unflatten:
187
            s += format_html(', <em class="unflatten">%s</em>', _('unflatten'))
187
            s += format_html(u', <em class="unflatten">%s</em>', _('unflatten'))
188 188
        if merge_extra:
189
            s += format_html(', <em class="merge-extra">%s</em>', _('merge extra'))
189
            s += format_html(u', <em class="merge-extra">%s</em>', _('merge extra'))
190 190
        if not additional_properties:
191
            s += format_html(', <em class="additional-properties-false">{}</em>', _('no additional properties'))
191
            s += format_html(u', <em class="additional-properties-false">{}</em>', _('no additional properties'))
192 192
        if title:
193
            s += format_html(', <em class="title">{}</em>', title)
193
            s += format_html(u', <em class="title">{}</em>', title)
194 194
        if schema:
195
            s += format_html('<tt class="raw">{!r}</tt>', schema)
195
            s += format_html(u'<tt class="raw">{!r}</tt>', schema)
196 196
        if description:
197
            s += format_html('\n<p class="description">{}</p>', description)
197
            s += format_html(u'\n<p class="description">{}</p>', description)
198 198
        s += ' '
199 199
        if properties:
200 200
            s += '\n<ul>'
......
206 206
                required = key in required_keys
207 207
                sub_description = sub.pop('description', '')
208 208
                sub_title = sub.pop('title', '')
209
                s += format_html('<li><tt>{0}</tt>', key)
209
                s += format_html(u'<li><tt>{0}</tt>', key)
210 210
                if required:
211
                    s += format_html('<span title="{}" class="required">*</span>', _('required'))
211
                    s += format_html(u'<span title="{}" class="required">*</span>', _('required'))
212 212
                if description or sub:
213 213
                    s += ' :'
214 214
                if sub_title:
215
                    s += format_html(' <em>{0}</em>', sub_title)
215
                    s += format_html(u' <em>{0}</em>', sub_title)
216 216
                elif sub_description and '\n' not in sub_description:
217
                    s += format_html(' <em>{0}</em>', sub_description)
217
                    s += format_html(u' <em>{0}</em>', sub_description)
218 218
                if sub_title or '\n' in sub_description:
219
                    s += format_html('\n<p class="description">{}</p>', sub_description)
219
                    s += format_html(u'\n<p class="description">{}</p>', sub_description)
220 220
                if sub:
221
                    s += format_html('\n{0}', render_json_schema(sub))
221
                    s += format_html(u'\n{0}', render_json_schema(sub))
222 222
                s += '</li>'
223 223
            s += '</ul>'
224 224
        return mark_safe(s)
225 225
    if typ == 'boolean':
226 226
        if not schema:
227 227
            return mark_safe('<b>boolean</b>')
228
    return format_html('<em>{0} {1!r}</em>', _('unknown validation'), original_schema)
228
    return format_html(u'<em>{0} {1!r}</em>', _('unknown validation'), original_schema)
229 229

  
230 230

  
231 231
@register.simple_tag(takes_context=False)
tests/test_templatetags.py
17 17
import inspect
18 18

  
19 19
from django.apps import apps
20
from django.utils import translation
20 21

  
21
from passerelle.base.templatetags.passerelle import render_body_schemas
22
from passerelle.base.templatetags.passerelle import render_body_schemas, render_json_schema
22 23

  
23 24

  
24 25
def test_render_body_schemas(db):
......
41 42
    assert schemas , 'no endpoint with schema found'
42 43
    for schema, endpoint in schemas:
43 44
        assert render_body_schemas(schema), 'failed for %s' % endpoint
45

  
46

  
47
def test_render_json_schema():
48
    SCHEMA = {
49
        '$schema': 'http://json-schema.org/draft-04/schema#',
50
        'type': 'object',
51
        'required': ['foo'],
52
        'additionalProperties': False,
53
        'properties': {
54
            'foo': {
55
                'type': 'string',
56
            },
57
        }
58
    }
59
    # Check that no unicode crash occurs
60
    with translation.override('fr'):
61
        render_json_schema(SCHEMA)
44
-