utils: add helper argument post_json_schema (#48317)

This commit is contained in:
Benjamin Dauvergne 2020-11-05 10:05:58 +01:00
parent 8e9fcdeeb7
commit dceb73599a
2 changed files with 110 additions and 151 deletions

View File

@ -297,35 +297,29 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=3,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'required': ['nom', 'prenom'],
'properties': {
'email': {
'type': 'string',
'maxLength': 78,
'pattern': '^(.*@.*)?$',
},
'nom': {
'type': 'string',
'maxLength': 28,
},
'prenom': {
'type': 'string',
'maxLength': 32,
},
'tel': {
'type': 'string',
'maxLength': 10,
'pattern': '^[0-9]{0,10}$',
},
},
},
post_json_schema={
'type': 'object',
'required': ['nom', 'prenom'],
'properties': {
'email': {
'type': 'string',
'maxLength': 78,
'pattern': '^(.*@.*)?$',
},
}
'nom': {
'type': 'string',
'maxLength': 28,
},
'prenom': {
'type': 'string',
'maxLength': 32,
},
'tel': {
'type': 'string',
'maxLength': 10,
'pattern': '^[0-9]{0,10}$',
},
},
},
json_schema_response=response_schema(
{
@ -406,36 +400,30 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=3.5,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'required': ['nom', 'prenom', 'statut'],
'properties': {
'email': {
'type': 'string',
'maxLength': 78,
'pattern': '^(.*@.*)?$',
},
'nom': {
'type': 'string',
'maxLength': 28,
},
'prenom': {
'type': 'string',
'maxLength': 32,
},
'tel': {
'type': 'string',
'maxLength': 10,
'pattern': '^[0-9]{0,10}$',
},
'statut': {'enum': ['C', 'A']},
},
},
post_json_schema={
'type': 'object',
'required': ['nom', 'prenom', 'statut'],
'properties': {
'email': {
'type': 'string',
'maxLength': 78,
'pattern': '^(.*@.*)?$',
},
}
'nom': {
'type': 'string',
'maxLength': 28,
},
'prenom': {
'type': 'string',
'maxLength': 32,
},
'tel': {
'type': 'string',
'maxLength': 10,
'pattern': '^[0-9]{0,10}$',
},
'statut': {'enum': ['C', 'A']},
},
},
json_schema_response=response_schema(),
)
@ -789,25 +777,19 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=5.1,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'required': ['tel'],
'properties': {
'tel': {
'type': 'string',
'maxLength': 10,
'pattern': '^[0-9]{0,10}$',
},
'commentaire': {
'type': 'string',
},
},
},
post_json_schema={
'type': 'object',
'required': ['tel'],
'properties': {
'tel': {
'type': 'string',
'maxLength': 10,
'pattern': '^[0-9]{0,10}$',
},
}
'commentaire': {
'type': 'string',
},
},
},
json_schema_response=response_schema(),
)
@ -848,20 +830,14 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=5.2,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'required': ['commentaire'],
'properties': {
'commentaire': {
'type': 'string',
},
},
},
post_json_schema={
'type': 'object',
'required': ['commentaire'],
'properties': {
'commentaire': {
'type': 'string',
},
}
},
},
json_schema_response=response_schema(),
)
@ -906,7 +882,7 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=5.1,
post={
post_json_schema={
'request_body': {
'schema': {
'application/json': {
@ -962,20 +938,14 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=5.2,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'required': ['commentaire'],
'properties': {
'commentaire': {
'type': 'string',
},
},
},
post_json_schema={
'type': 'object',
'required': ['commentaire'],
'properties': {
'commentaire': {
'type': 'string',
},
}
},
},
json_schema_response=response_schema(),
)
@ -1266,20 +1236,14 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=10,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'properties': {
'date_debut': DATE_SCHEMA,
'date_fin': DATE_SCHEMA,
'moticlodac': {'type': 'string'},
'commentaire_ref': {'type': 'string', 'maxLength': 1000},
},
},
},
}
post_json_schema={
'type': 'object',
'properties': {
'date_debut': DATE_SCHEMA,
'date_fin': DATE_SCHEMA,
'moticlodac': {'type': 'string'},
'commentaire_ref': {'type': 'string', 'maxLength': 1000},
},
},
json_schema_response=response_schema(
{
@ -1575,18 +1539,12 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=13.5,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'properties': {
'motif': {'type': 'string'},
'commentaire_ref': {'type': 'string'},
},
},
},
}
post_json_schema={
'type': 'object',
'properties': {
'motif': {'type': 'string'},
'commentaire_ref': {'type': 'string'},
},
},
json_schema_response=response_schema(),
)
@ -1628,33 +1586,27 @@ class RSA13Resource(BaseResource, HTTPResource):
),
display_category=_('Platform'),
display_order=14,
post={
'request_body': {
'schema': {
'application/json': {
'type': 'object',
'properties': {
'rendezvous': {
'type': 'object',
'properties': {
'date_prise': DATE_SCHEMA,
'relance': {
'type': 'object',
'properties': {
'date': DATE_SCHEMA,
'motif': {'type': 'string'},
},
},
'date_reelle': DATE_SCHEMA,
'resultat': {'type': 'string'},
},
post_json_schema={
'type': 'object',
'properties': {
'rendezvous': {
'type': 'object',
'properties': {
'date_prise': DATE_SCHEMA,
'relance': {
'type': 'object',
'properties': {
'date': DATE_SCHEMA,
'motif': {'type': 'string'},
},
'commentaire_ref': {'type': 'string'},
},
'unflatten': True,
'date_reelle': DATE_SCHEMA,
'resultat': {'type': 'string'},
},
},
}
'commentaire_ref': {'type': 'string'},
},
'unflatten': True,
},
json_schema_response=response_schema(
{

View File

@ -59,6 +59,8 @@ class endpoint(object):
display_category='',
json_schema_response=None,
datasource=False,
# helper to define the POST json schema
post_json_schema=None,
):
self.perm = perm
self.methods = methods
@ -83,6 +85,11 @@ class endpoint(object):
self.example_pattern = example_pattern
self.parameters = parameters or {}
self.cache_duration = cache_duration
if post_json_schema:
post = post or {}
schema = post.setdefault('request_body', {}).setdefault('schema', {})
assert not schema.get('application/json'), 'a json schema was already set in the post argument'
schema['application/json'] = post_json_schema
self.post = post
if post:
self.methods = ['post']