0001-api-add-possibility-to-get-drafts-from-api-user-form.patch
help/fr/api-user.page | ||
---|---|---|
128 | 128 | |
129 | 129 |
<p> |
130 | 130 |
Il est possible de recevoir un ensemble plus complet de données en passant un |
131 |
paramètre <code>full=on</code> à l'adresse. |
|
131 |
paramètre <code>full=on</code> à l'adresse. Pour inclure également les |
|
132 |
brouillons, un paramètre <code>include-drafts=true</code> peut être passé. |
|
132 | 133 |
</p> |
133 | 134 | |
134 |
<note> |
|
135 |
<p>Note de compatibilité : cette information est également disponible à |
|
136 |
l'adresse <code>/myspace/forms</code>. |
|
137 |
</p> |
|
138 |
</note> |
|
139 | ||
140 | 135 |
</section> |
141 | 136 |
<section> |
142 | 137 |
<title>Brouillons</title> |
... | ... | |
147 | 142 |
</p> |
148 | 143 | |
149 | 144 |
<screen> |
150 |
<output style="prompt">$ </output><input>curl https://www.example.net/myspace/drafts</input>
|
|
145 |
<output style="prompt">$ </output><input>curl https://www.example.net/api/user/drafts</input>
|
|
151 | 146 |
<output>[ |
152 | 147 |
{ |
153 | 148 |
"datetime": "2014-07-21 10:15:21", |
tests/test_api.py | ||
---|---|---|
1096 | 1096 |
fields.StringField(id='0', label='foobar', varname='foobar'), |
1097 | 1097 |
fields.StringField(id='1', label='foobar2'),] |
1098 | 1098 |
formdef.keywords = 'hello, world' |
1099 |
formdef.disabled = False |
|
1100 |
formdef.enable_tracking_codes = True |
|
1099 | 1101 |
formdef.store() |
1100 | 1102 |
formdef.data_class().wipe() |
1101 | 1103 | |
... | ... | |
1130 | 1132 |
resp = get_app(pub).get(sign_uri('/api/user/forms', user=local_user)) |
1131 | 1133 |
assert len(resp.json) == 1 |
1132 | 1134 | |
1135 |
formdata = formdef.data_class()() |
|
1136 |
formdata.user_id = local_user.id |
|
1137 |
formdata.status = 'draft' |
|
1138 |
formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple() |
|
1139 |
formdata.store() |
|
1140 | ||
1141 |
resp = get_app(pub).get(sign_uri('/api/user/forms', user=local_user)) |
|
1142 |
assert len(resp.json) == 1 |
|
1143 | ||
1144 |
resp = get_app(pub).get(sign_uri('/api/user/forms?include-drafts=true', user=local_user)) |
|
1145 |
assert len(resp.json) == 1 |
|
1146 | ||
1147 |
formdef.disabled = False |
|
1148 |
formdef.store() |
|
1149 | ||
1150 |
resp = get_app(pub).get(sign_uri('/api/user/forms?include-drafts=true', user=local_user)) |
|
1151 |
assert len(resp.json) == 2 |
|
1152 | ||
1153 |
draft_formdata = [x for x in resp.json if x['status'] == 'Draft'][0] |
|
1154 |
assert draft_formdata.get('url')[-1] != '/' |
|
1155 | ||
1133 | 1156 |
def test_user_drafts(pub, local_user): |
1134 | 1157 |
FormDef.wipe() |
1135 | 1158 |
formdef = FormDef() |
... | ... | |
1143 | 1166 |
formdef.enable_tracking_codes = True |
1144 | 1167 |
formdef.store() |
1145 | 1168 | |
1169 |
formdef.data_class().wipe() |
|
1170 | ||
1146 | 1171 |
resp = get_app(pub).get(sign_uri('/api/user/drafts', user=local_user)) |
1147 | 1172 |
assert len(resp.json) == 0 |
1148 | 1173 |
wcs/api.py | ||
---|---|---|
24 | 24 | |
25 | 25 |
from qommon import _ |
26 | 26 |
from qommon import misc |
27 |
from qommon.evalutils import make_datetime |
|
27 | 28 |
from qommon.errors import (AccessForbiddenError, QueryError, TraversalError, |
28 | 29 |
UnknownNameIdAccessForbiddenError) |
29 | 30 |
from qommon.form import ValidationError, ComputedExpressionWidget |
... | ... | |
67 | 68 | |
68 | 69 | |
69 | 70 |
def get_formdata_dict(formdata, user, consider_status_visibility=True): |
70 |
if consider_status_visibility: |
|
71 |
if consider_status_visibility and not formdata.is_draft():
|
|
71 | 72 |
status = formdata.get_visible_status(user=user) |
72 | 73 |
if not status: |
73 | 74 |
# skip hidden forms |
... | ... | |
75 | 76 |
else: |
76 | 77 |
status = formdata.get_status() |
77 | 78 | |
78 |
title = _('%(name)s #%(id)s (%(status)s)') % { |
|
79 |
'name': formdata.formdef.name, |
|
80 |
'id': formdata.get_display_id(), |
|
81 |
'status': status.name, |
|
82 |
} |
|
83 |
d = {'title': title, |
|
79 |
d = { |
|
84 | 80 |
'name': formdata.formdef.name, |
85 | 81 |
'url': formdata.get_url(), |
86 | 82 |
'datetime': misc.strftime('%Y-%m-%d %H:%M:%S', formdata.receipt_time), |
87 |
'status': status.name, |
|
88 |
'status_css_class': status.extra_css_class, |
|
83 |
'status': status.name if not formdata.is_draft() else _('Draft'),
|
|
84 |
'status_css_class': status.extra_css_class if status else None,
|
|
89 | 85 |
'keywords': formdata.formdef.keywords_list, |
86 |
'draft': formdata.is_draft(), |
|
90 | 87 |
} |
91 | 88 |
if formdata.last_update_time: |
92 | 89 |
d['last_update_time'] = misc.strftime('%Y-%m-%d %H:%M:%S', formdata.last_update_time) |
90 | ||
91 |
if formdata.is_draft(): |
|
92 |
d['url'] = d['url'].rstrip('/') |
|
93 |
d['form_number_raw'] = d['form_number'] = None |
|
94 |
d['title'] = _('%(name)s (draft)') % {'name': formdata.formdef.name} |
|
95 |
else: |
|
96 |
d['title'] = _('%(name)s #%(id)s (%(status)s)') % { |
|
97 |
'name': formdata.formdef.name, |
|
98 |
'id': formdata.get_display_id(), |
|
99 |
'status': status.name, |
|
100 |
} |
|
101 | ||
93 | 102 |
d.update(formdata.get_substitution_variables(minimal=True)) |
94 | 103 |
if get_request().form.get('full') == 'on': |
95 | 104 |
d.update(formdata.get_json_export_dict(include_files=False)) |
... | ... | |
520 | 529 |
return user_forms |
521 | 530 | |
522 | 531 |
def drafts(self): |
523 |
get_response().set_content_type('application/json') |
|
524 |
user = self.user or get_user_from_api_query_string() or get_request().user |
|
525 |
if not user: |
|
526 |
raise AccessForbiddenError('no user specified') |
|
527 |
drafts = [] |
|
528 |
for form in self.get_user_forms(user): |
|
529 |
if not form.is_draft(): |
|
530 |
continue |
|
531 |
if form.formdef.is_disabled() or not form.formdef.enable_tracking_codes: |
|
532 |
continue |
|
533 |
title = _('%(name)s, draft saved on %(datetime)s') % { |
|
534 |
'name': form.formdef.name, |
|
535 |
'datetime': misc.localstrftime(form.receipt_time) |
|
536 |
} |
|
537 |
# !!! no trailing slash in the special draft case |
|
538 |
url = form.get_url().rstrip('/') |
|
539 |
d = {'title': title, |
|
540 |
'name': form.formdef.name, |
|
541 |
'url': url, |
|
542 |
'datetime': misc.strftime('%Y-%m-%d %H:%M:%S', form.receipt_time), |
|
543 |
'keywords': form.formdef.keywords_list, |
|
544 |
} |
|
545 |
if get_request().form.get('full') == 'on': |
|
546 |
d.update(form.get_json_export_dict(include_files=False)) |
|
547 |
drafts.append(d) |
|
548 | ||
549 |
return json.dumps(drafts, |
|
550 |
cls=misc.JSONEncoder, |
|
551 |
encoding=get_publisher().site_charset) |
|
532 |
return self.forms(include_drafts=True, include_non_drafts=False) |
|
552 | 533 | |
553 |
def forms(self): |
|
534 |
def forms(self, include_drafts=False, include_non_drafts=True):
|
|
554 | 535 |
get_response().set_content_type('application/json') |
555 | 536 |
user = self.user or get_user_from_api_query_string() or get_request().user |
556 | 537 |
if not user: |
557 | 538 |
raise AccessForbiddenError('no user specified') |
558 | 539 |
forms = [] |
540 |
include_drafts = include_drafts or get_request().form.get('include-drafts') == 'true' |
|
559 | 541 |
for form in self.get_user_forms(user): |
560 | 542 |
if form.is_draft(): |
543 |
if not include_drafts: |
|
544 |
continue |
|
545 |
if form.formdef.is_disabled() or not form.formdef.enable_tracking_codes: |
|
546 |
# the form or its draft support has been disabled |
|
547 |
continue |
|
548 |
elif not include_non_drafts: |
|
561 | 549 |
continue |
562 | 550 |
formdata_dict = get_formdata_dict(form, user) |
563 | 551 |
if not formdata_dict: |
564 |
- |