add API to get user forms and user drafts (#5146)
This commit is contained in:
parent
f0fa69237e
commit
be564d8edf
|
@ -24,6 +24,8 @@ de l'utilisateur, modification, etc.).
|
|||
<section id="pull">
|
||||
<title>Mode pull</title>
|
||||
|
||||
<section>
|
||||
<title>Profil</title>
|
||||
<p>
|
||||
Les informations associées à un utilisateur sont accessibles à l'URL
|
||||
<code>/user</code>, elles reprennent son nom (<code>user_display_name</code>),
|
||||
|
@ -42,6 +44,79 @@ autorisations d'accès au backoffice (<code>user_backoffice_access</code>) ou
|
|||
}
|
||||
</output></screen>
|
||||
|
||||
</section>
|
||||
|
||||
<section id="forms">
|
||||
<title>Formulaires</title>
|
||||
|
||||
<p>
|
||||
La liste des formulaires transmis par un utilisateur est accessible à l'URL
|
||||
<code>/myspace/forms</code>, elle reprend un ensemble minimal
|
||||
d'informations concernant chacun de ceux-ci.
|
||||
</p>
|
||||
<screen>
|
||||
<output style="prompt">$ </output><input>curl -H "Accept: application/json" https://www.example.net/myspace/forms</input>
|
||||
<output>[
|
||||
{
|
||||
"category_id": "1",
|
||||
"category_name": "Divers",
|
||||
"datetime": "2014-03-28 15:36:52",
|
||||
"form_name": "Demande d'inscription",
|
||||
"form_number": "123",
|
||||
"form_number_raw": "123",
|
||||
"form_receipt_date": "28/03/2014",
|
||||
"form_receipt_time": "15:36",
|
||||
"form_status": "Nouveau",
|
||||
"form_uri": "demande-d-inscription/123/",
|
||||
"form_url": "http://www.example.net/demande-d-inscription/123/",
|
||||
"form_url_backoffice": "http://www.example.net/backoffice/demande-d-inscription/123/",
|
||||
"name": "Demande d'inscription",
|
||||
"status": "Nouveau",
|
||||
"title": "Demande d'inscription #123 (Nouveau)",
|
||||
"url": "http://www.example.net/demande-d-inscription/123/",
|
||||
},
|
||||
{
|
||||
"category_id": "2",
|
||||
"category_name": "Prise de rendez-vous",
|
||||
"datetime": "2014-03-17 10:39:52",
|
||||
"form_name": "Rendez-vous avec le service B",
|
||||
"form_number": "456",
|
||||
"form_number_raw": "456",
|
||||
"form_receipt_date": "17/03/2014",
|
||||
"form_receipt_time": "10:39",
|
||||
"form_status": "En cours",
|
||||
"form_uri": "rendez-vous-service-b/456/",
|
||||
"form_url": "http://www.example.net/rendez-vous-service-b/456/",
|
||||
"form_url_backoffice": "http://www.example.net/backoffice/rendez-vous-service-b/456/",
|
||||
"name": "Rendez-vous avec le service B"",
|
||||
"status": "Nouveau",
|
||||
"title": "Rendez-vous avec le service B #456 (En cours)",
|
||||
"url": "http://www.example.net/rendez-vous-service-b/456/",
|
||||
}
|
||||
]</output></screen>
|
||||
|
||||
</section>
|
||||
<section>
|
||||
<title>Brouillons</title>
|
||||
|
||||
<p>
|
||||
La liste des brouillons de l'utilisateur est accessible à l'adresse
|
||||
<code>/myspace/drafts</code>.
|
||||
</p>
|
||||
|
||||
<screen>
|
||||
<output style="prompt">$ </output><input>curl -H "Accept: application/json" https://www.example.net/myspace/drafts</input>
|
||||
<output>[
|
||||
{
|
||||
"datetime": "2014-07-21 10:15:21",
|
||||
"name": "Demande de relecture",
|
||||
"title": "Demande de relecture, brouillon enregistré le 21/07/2014 10:15",
|
||||
"url": "http://www.example.net/demande-de-relecture/164"
|
||||
}
|
||||
]</output></screen>
|
||||
|
||||
</section>
|
||||
|
||||
</section>
|
||||
|
||||
</page>
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
# w.c.s. - web application for online forms
|
||||
# Copyright (C) 2005-2014 Entr'ouvert
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import json
|
||||
|
||||
from quixote import get_request, get_response, redirect
|
||||
from quixote.directory import Directory
|
||||
|
||||
import qommon.myspace
|
||||
from qommon import misc
|
||||
|
||||
from api import get_user_from_api_query_string
|
||||
from formdef import FormDef
|
||||
|
||||
class MyspaceDirectory(qommon.myspace.MyspaceDirectory):
|
||||
_q_exports = ['', 'profile', 'new', 'password', 'remove', 'drafts', 'forms']
|
||||
|
||||
def get_user_forms(self, user):
|
||||
formdefs = FormDef.select(lambda x: not x.is_disabled())
|
||||
user_forms = []
|
||||
for formdef in formdefs:
|
||||
user_forms.extend(formdef.data_class().get_with_indexed_value(
|
||||
'user_id', user.id))
|
||||
try:
|
||||
user_forms.extend(formdef.data_class().get_with_indexed_value(
|
||||
'user_hash', user.hash))
|
||||
except AttributeError:
|
||||
pass
|
||||
user_forms.sort(lambda x,y: cmp(x.receipt_time, y.receipt_time))
|
||||
return user_forms
|
||||
|
||||
def drafts(self):
|
||||
if get_request().is_json():
|
||||
return self.drafts_json()
|
||||
return redirect('.')
|
||||
|
||||
def drafts_json(self):
|
||||
get_response().set_content_type('application/json')
|
||||
user = get_user_from_api_query_string() or get_request().user
|
||||
if not user:
|
||||
return errors.AccessForbiddenError()
|
||||
drafts = []
|
||||
for form in self.get_user_forms(user):
|
||||
if not form.is_draft():
|
||||
continue
|
||||
title = '%(name)s, draft saved on %(datetime)s' % {
|
||||
'name': form.formdef.name,
|
||||
'datetime': misc.localstrftime(form.receipt_time)
|
||||
}
|
||||
url = form.get_url()
|
||||
d = {'title': title,
|
||||
'name': form.formdef.name,
|
||||
'url': url,
|
||||
'datetime': misc.strftime.strftime('%Y-%m-%d %H:%M:%S', form.receipt_time),
|
||||
}
|
||||
drafts.append(d)
|
||||
|
||||
return json.dumps(drafts)
|
||||
|
||||
def forms(self):
|
||||
if get_request().is_json():
|
||||
return self.forms_json()
|
||||
return redirect('.')
|
||||
|
||||
def forms_json(self):
|
||||
get_response().set_content_type('application/json')
|
||||
user = get_user_from_api_query_string() or get_request().user
|
||||
if not user:
|
||||
return errors.AccessForbiddenError()
|
||||
forms = []
|
||||
for form in self.get_user_forms(user):
|
||||
if form.is_draft():
|
||||
continue
|
||||
visible_status = form.get_visible_status(user=user)
|
||||
# skip hidden forms
|
||||
if not visible_status:
|
||||
continue
|
||||
name = form.formdef.name
|
||||
id = form.get_display_id()
|
||||
status = visible_status.name
|
||||
title = _('%(name)s #%(id)s (%(status)s)') % {
|
||||
'name': name,
|
||||
'id': id,
|
||||
'status': status
|
||||
}
|
||||
url = form.get_url()
|
||||
d = {'title': title,
|
||||
'name': form.formdef.name,
|
||||
'url': url,
|
||||
'datetime': misc.strftime.strftime('%Y-%m-%d %H:%M:%S', form.receipt_time),
|
||||
'status': status,
|
||||
}
|
||||
d.update(form.get_substitution_variables(minimal=True))
|
||||
forms.append(d)
|
||||
|
||||
return json.dumps(forms)
|
|
@ -38,7 +38,6 @@ from qommon import template
|
|||
from qommon import misc
|
||||
from qommon.form import *
|
||||
import qommon.ident
|
||||
import qommon.myspace
|
||||
import qommon.pages
|
||||
|
||||
from qommon.afterjobs import AfterJobStatusDirectory
|
||||
|
@ -49,6 +48,7 @@ from formdata import FormData
|
|||
from anonylink import AnonymityLink
|
||||
from roles import Role
|
||||
from wcs.api import get_user_from_api_query_string
|
||||
from myspace import MyspaceDirectory
|
||||
|
||||
|
||||
class CompatibilityDirectory(Directory):
|
||||
|
@ -186,7 +186,7 @@ class RootDirectory(Directory):
|
|||
'pages', ('tmp-upload', 'tmp_upload'), '__version__']
|
||||
|
||||
themes = template.ThemesDirectory()
|
||||
myspace = qommon.myspace.MyspaceDirectory()
|
||||
myspace = MyspaceDirectory()
|
||||
pages = qommon.pages.PagesDirectory()
|
||||
|
||||
def __version__(self):
|
||||
|
@ -298,6 +298,7 @@ class RootDirectory(Directory):
|
|||
charset = get_publisher().site_charset
|
||||
for role in Role.select():
|
||||
list_roles.append({'text': unicode(role.name, charset),
|
||||
'allows_backoffice_access': role.allows_backoffice_access,
|
||||
'id': role.id})
|
||||
get_response().set_content_type('application/json')
|
||||
return json.dumps({'data': list_roles})
|
||||
|
|
Loading…
Reference in New Issue