Projet

Général

Profil

0001-templatetags-display-required-fields-on-json-schema-.patch

Nicolas Roche, 12 octobre 2022 17:52

Télécharger (5,03 ko)

Voir les différences:

Subject: [PATCH] templatetags: display required fields on json schema (#70174)

 passerelle/base/templatetags/passerelle.py | 8 ++++----
 tests/test_templatetags.py                 | 8 ++++++++
 tox.ini                                    | 1 +
 3 files changed, 13 insertions(+), 4 deletions(-)
passerelle/base/templatetags/passerelle.py
203 203
        if title:
204 204
            s += format_html(', <em class="title">{}</em>', title)
205 205
        if schema:
206 206
            s += format_html('<tt class="raw">{!r}</tt>', schema)
207 207
        if description:
208 208
            s += format_html('\n<p class="description">{}</p>', description)
209 209
        s += ' '
210 210

  
211
        def render_property_schema(key, sub):
211
        def render_property_schema(key, html, sub):
212 212
            nonlocal s
213 213

  
214 214
            required = key in required_keys
215 215
            sub_description = sub.pop('description', '')
216 216
            sub_title = sub.pop('title', '')
217
            s += format_html('<li>{0}', key)
217
            s += format_html('<li>{0}', html)
218 218
            if required:
219 219
                s += format_html('<span title="{}" class="required">*</span>', _('required'))
220 220
            if description or sub:
221 221
                s += ' :'
222 222
            if sub_title:
223 223
                s += format_html(' <em>{0}</em>', sub_title)
224 224
            elif sub_description and '\n' not in sub_description:
225 225
                s += format_html(' <em>{0}</em>', sub_description)
......
233 233
            s += '\n<ul>'
234 234

  
235 235
        if properties:
236 236
            keys = properties
237 237
            if not isinstance(properties, collections.OrderedDict):
238 238
                keys = sorted(properties, key=lambda key: key.lower())
239 239
            for key in keys:
240 240
                sub = properties.get(key, {}).copy()
241
                render_property_schema(format_html('<tt>{0}</tt>', key), sub)
241
                render_property_schema(key, format_html('<tt>{0}</tt>', key), sub)
242 242
            if one_of and one_of[0].get('required'):
243 243
                s += many_of('oneOf', one_of)
244 244

  
245 245
        if pattern_properties:
246 246
            s += format_html('<li><span>{0}</span>', _('Pattern properties'))
247 247
            s += '\n<ul>'
248 248
            for key, sub in pattern_properties.items():
249 249
                if key:
250 250
                    pattern_key = format_html('/<tt>{0}</tt>/', key)
251 251
                else:
252 252
                    pattern_key = format_html('<em>any</em>')
253
                render_property_schema(pattern_key, sub)
253
                render_property_schema(key, pattern_key, sub)
254 254
            s += '</ul>'
255 255

  
256 256
        if properties or pattern_properties:
257 257
            s += '</ul>'
258 258

  
259 259
        return mark_safe(s)
260 260
    if typ == 'boolean':
261 261
        if not schema:
tests/test_templatetags.py
13 13
#
14 14
# You should have received a.deepcopy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import inspect
18 18

  
19 19
from django.apps import apps
20 20
from django.utils import translation
21
from pyquery import PyQuery as pq
21 22

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

  
24 25

  
25 26
def test_render_body_schemas(db):
26 27
    # FIXME: db should be required but the way ProxyLogger is initialized force an access to the DB
27 28
    def collect_schemas():
28 29
        for app in apps.get_app_configs():
......
73 74
def test_render_pattern_description():
74 75
    schema = {'type': 'object', 'properties': {'filename': {'type': 'string', 'pattern': 'abc'}}}
75 76
    assert 'abc' in render_json_schema(schema)
76 77

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

  
80 81

  
82
def test_render_required_properties():
83
    schema = {'type': 'object', 'required': ['filename'], 'properties': {'filename': {'type': 'string'}}}
84
    html = render_json_schema(schema)
85
    assert pq(html)('li span.required')
86
    assert pq(html)('li').text() == 'filename* : string'
87

  
88

  
81 89
def test_render_oneof_property_required():
82 90
    schema = {
83 91
        'type': 'object',
84 92
        'properties': {
85 93
            'a': {'type': 'string'},
86 94
            'b': {'type': 'string'},
87 95
        },
88 96
        'oneOf': [
tox.ini
75 75
  pytest!=6.0.0
76 76
  WebTest
77 77
  mock<4
78 78
  httmock
79 79
  pylint
80 80
  pylint-django
81 81
  django-webtest<1.9.3
82 82
  pytest-freezegun
83
  pyquery
83 84
  responses
84 85
  mohawk
85 86
  ldaptools
86 87
commands =
87 88
  ./pylint.sh passerelle/ tests/
88
-