From e149c39e2622a32fe33270cc95f5f8b5204e1f36 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 23 Sep 2021 11:59:59 +0200 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 | 1 + passerelle/contrib/toulouse_smart/schemas.py | 2 +- tests/test_templatetags.py | 8 ++++++++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/passerelle/apps/cmis/models.py b/passerelle/apps/cmis/models.py index 9307e29f..9f23797a 100644 --- a/passerelle/apps/cmis/models.py +++ b/passerelle/apps/cmis/models.py @@ -55,9 +55,10 @@ UPLOAD_SCHEMA = { 'properties': { 'filename': { 'type': 'string', - 'description': _('Filename (numbers, letters and special caracters "%s" are allowed)') - % SPECIAL_CHARS, + 'description': _('Filename'), 'pattern': FILE_NAME_PATTERN, + 'pattern_description': _('Numbers, letters and special caracters "%s" are allowed.') + % SPECIAL_CHARS, }, 'content': { 'type': 'string', @@ -74,11 +75,14 @@ UPLOAD_SCHEMA = { 'type': 'string', 'description': _('Filename (takes precendence over filename in "file" object)'), 'pattern': FILE_NAME_PATTERN, + 'pattern_description': _('Numbers, letters and special caracters "%s" are allowed.') + % SPECIAL_CHARS, }, 'path': { 'type': 'string', - 'description': _('File path (with leading but not trailing slash)'), + 'description': _('File path'), 'pattern': FILE_PATH_PATTERN, + 'pattern_description': _('Must include leading but not trailing slash.'), }, 'object_type': { 'type': 'string', diff --git a/passerelle/base/templatetags/passerelle.py b/passerelle/base/templatetags/passerelle.py index 87b0d0ee..46215342 100644 --- a/passerelle/base/templatetags/passerelle.py +++ b/passerelle/base/templatetags/passerelle.py @@ -158,6 +158,7 @@ def render_json_schema(schema): min_length = schema.pop('minLength', '') max_length = schema.pop('maxLength', '') pattern = schema.pop('pattern', '') + pattern_description = schema.pop('pattern_description', '') if enum: enum = mark_safe(' | '.join([format_html('{}', json.dumps(el)) for el in enum])) s = 'string' @@ -166,7 +167,9 @@ def render_json_schema(schema): s = html_type(s) if enum: s += ' %s' % enum - if pattern: + if pattern_description: + s += format_html(' {}', pattern_description) + elif pattern: s += format_html(' /{}/', pattern) if schema: s += format_html('\n{!r}', schema) diff --git a/passerelle/contrib/caluire_axel/schemas.py b/passerelle/contrib/caluire_axel/schemas.py index 5568c0f3..d7cd56f2 100644 --- a/passerelle/contrib/caluire_axel/schemas.py +++ b/passerelle/contrib/caluire_axel/schemas.py @@ -30,6 +30,7 @@ boolean_type = { { 'type': 'string', 'pattern': '[Oo]|[Nn]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|1|0', + 'pattern_description': 'Les valeurs "0", "1", "O", "N", "true" ou "false" sont autorisées.', }, ] } diff --git a/passerelle/contrib/toulouse_smart/schemas.py b/passerelle/contrib/toulouse_smart/schemas.py index 2a58c7fd..97caeb17 100644 --- a/passerelle/contrib/toulouse_smart/schemas.py +++ b/passerelle/contrib/toulouse_smart/schemas.py @@ -14,12 +14,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . - BOOLEAN_TYPES = [ {'type': 'boolean'}, { 'type': 'string', 'pattern': '[Oo][Uu][Ii]|[Nn][Oo][Nn]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|1|0', + 'pattern_description': 'Les valeurs "0", "1", "true", "false", "oui" ou "non" sont autorisées.', }, ] diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index 63f5093a..cdb4eaa0 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -69,3 +69,11 @@ def test_render_enum_schema(): str(render_json_schema({'enum': [1, "aaa", [1]]})) == '1 | "aaa" | [1]' ) + + +def test_render_pattern_description(): + schema = {'type': 'object', 'properties': {'filename': {'type': 'string', 'pattern': 'abc'}}} + assert 'abc' in render_json_schema(schema) + + schema['properties']['filename']['pattern_description'] = 'efg' + assert 'abc' not in render_json_schema(schema) and 'efg' in render_json_schema(schema) -- 2.30.2