add json listings in /api/forms/ (#6642)
This commit is contained in:
parent
87a7b1cead
commit
588a5cb1a1
|
@ -104,5 +104,61 @@ l'exemple ci-dessus).
|
|||
|
||||
</section>
|
||||
|
||||
<section id="listing">
|
||||
<title>Liste de formulaires</title>
|
||||
|
||||
<note style="important"><p>
|
||||
Ce webservice n'est pas encore stabilisé, son URL peut encore changer dans les
|
||||
futures versions de w.c.s.
|
||||
</p></note>
|
||||
|
||||
<p>
|
||||
La liste des demandes pour un formulaire donné est destinée à être utilisée par
|
||||
un système de synchronisation. Elle ne retourne donc pour chaque demande que
|
||||
son numéro (id), ses dates de création et de dernière mise à jour.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Un système de synchronisation vérifiera depuis cette liste si de nouvelles
|
||||
demandes existent, ou si certaines ont été mises à jour, sont obsolètes ou
|
||||
effacées, puis effectuera pour chacune les actions nécessaires (pull, push,
|
||||
etc.).
|
||||
</p>
|
||||
|
||||
<screen>
|
||||
<output style="prompt">$ </output><input>curl -H "Accept: application/json" \
|
||||
https://www.example.net/api/forms/inscriptions/list</input>
|
||||
</screen>
|
||||
|
||||
<code mime="application/json">
|
||||
[
|
||||
{
|
||||
url: "https://www.example.net/inscriptions/1/",
|
||||
last_update_time: "2015-03-26T23:08:45",
|
||||
receipt_time: "2015-03-26T23:08:44",
|
||||
id: 1
|
||||
},
|
||||
{
|
||||
url: "https://www.example.net/inscriptions/3/",
|
||||
last_update_time: "2015-03-27T12:11:21",
|
||||
receipt_time: "2015-03-27T12:45:19",
|
||||
id: 3
|
||||
}
|
||||
]
|
||||
</code>
|
||||
|
||||
<p>
|
||||
Des paramètres peuvent être envoyés dans la requête pour filtrer le listing
|
||||
voulu. Il s'agit des mêmes paramètres que pour l'export en backoffice, sauf
|
||||
pour filter qui est fixé à all par défaut. Pour avoir une liste limitée aux
|
||||
demandes non terminées (pending) :
|
||||
</p>
|
||||
|
||||
<screen>
|
||||
<output style="prompt">$ </output><input>curl -H "Accept: application/json" \
|
||||
https://www.example.net/api/forms/inscriptions/list?filter=pending</input>
|
||||
</screen>
|
||||
</section>
|
||||
|
||||
|
||||
</page>
|
||||
|
|
41
wcs/api.py
41
wcs/api.py
|
@ -27,6 +27,9 @@ from quixote import get_request, get_publisher, get_response
|
|||
from quixote.directory import Directory
|
||||
from qommon.errors import AccessForbiddenError, QueryError
|
||||
|
||||
from wcs.formdef import FormDef
|
||||
|
||||
|
||||
def get_user_from_api_query_string():
|
||||
query_string = get_request().get_query()
|
||||
if not query_string:
|
||||
|
@ -116,8 +119,44 @@ def sign_string(s, key, algo='sha256', timedelta=30):
|
|||
hash = hmac.HMAC(key, digestmod=digestmod, msg=s)
|
||||
return hash.digest()
|
||||
|
||||
|
||||
# import backoffice.root.FormPage after get_user_from_api_query_string
|
||||
# to avoid circular dependencies
|
||||
from backoffice.root import FormPage as BackofficeFormPage
|
||||
|
||||
|
||||
class ApiFormPage(BackofficeFormPage):
|
||||
_q_exports = [('list', 'json')] # same as backoffice but restricted to json export
|
||||
|
||||
def __init__(self, component):
|
||||
try:
|
||||
self.formdef = FormDef.get_by_urlname(component)
|
||||
except KeyError:
|
||||
raise errors.TraversalError()
|
||||
|
||||
api_user = get_user_from_api_query_string()
|
||||
if not api_user:
|
||||
if get_request().user and get_request().user.is_admin:
|
||||
return # grant access to admins, to ease debug
|
||||
raise AccessForbiddenError()
|
||||
if not self.formdef.is_of_concern_for_user(api_user):
|
||||
raise AccessForbiddenError()
|
||||
|
||||
|
||||
class ApiFormsDirectory(Directory):
|
||||
def _q_lookup(self, component):
|
||||
api_user = get_user_from_api_query_string()
|
||||
if not api_user:
|
||||
# grant access to admins, to ease debug
|
||||
if not (get_request().user and get_request().user.is_admin):
|
||||
raise AccessForbiddenError()
|
||||
return ApiFormPage(component)
|
||||
|
||||
|
||||
class ApiDirectory(Directory):
|
||||
_q_exports = [('reverse-geocoding', 'reverse_geocoding')]
|
||||
_q_exports = ['forms', ('reverse-geocoding', 'reverse_geocoding')]
|
||||
|
||||
forms = ApiFormsDirectory()
|
||||
|
||||
def reverse_geocoding(self):
|
||||
try:
|
||||
|
|
|
@ -631,11 +631,11 @@ class FormPage(Directory):
|
|||
|
||||
return fields
|
||||
|
||||
def get_filter_from_query(self):
|
||||
def get_filter_from_query(self, default='pending'):
|
||||
if 'filter' in get_request().form:
|
||||
return get_request().form['filter']
|
||||
if self.formdef.workflow.possible_status:
|
||||
return 'pending'
|
||||
return default
|
||||
return 'all'
|
||||
|
||||
def _q_index(self):
|
||||
|
@ -959,12 +959,12 @@ class FormPage(Directory):
|
|||
def json(self):
|
||||
get_response().set_content_type('application/json')
|
||||
user = get_user_from_api_query_string() or get_request().user
|
||||
selected_filter = self.get_filter_from_query()
|
||||
selected_filter = self.get_filter_from_query(default='all')
|
||||
query = get_request().form.get('q')
|
||||
items, total_count = FormDefUI(self.formdef).get_listing_items(
|
||||
selected_filter, user=user, query=query)
|
||||
output = [{'id': filled.id,
|
||||
'url': filled.get_url(backoffice=True),
|
||||
'url': filled.get_url(),
|
||||
'receipt_time': filled.receipt_time,
|
||||
'last_update_time': filled.last_update_time} for filled in items]
|
||||
return json.dumps(output,
|
||||
|
|
Loading…
Reference in New Issue