0001-general-expose-named-data-sources-as-substitution-va.patch
tests/test_datasource.py | ||
---|---|---|
19 | 19 | |
20 | 20 |
pub = create_temporary_pub() |
21 | 21 | |
22 |
req = HTTPRequest(None, {}) |
|
22 |
req = HTTPRequest(None, {'SERVER_NAME': 'example.net', 'SCRIPT_NAME': ''})
|
|
23 | 23 |
pub._set_request(req) |
24 | 24 | |
25 | 25 | |
... | ... | |
176 | 176 |
assert data_sources.get_items(datasource) == [('1', 'foo', '1'), ('2', 'bar', '2')] |
177 | 177 |
assert data_sources.get_structured_items(datasource) == [ |
178 | 178 |
{'id': '1', 'text': 'foo'}, {'id': '2', 'text': 'bar'}] |
179 | ||
180 |
def test_data_source_substitution_variables(): |
|
181 |
NamedDataSource.wipe() |
|
182 |
data_source = NamedDataSource(name='foobar') |
|
183 |
data_source.data_source = {'type': 'formula', 'value': repr(['un', 'deux'])} |
|
184 |
data_source.store() |
|
185 | ||
186 |
pub.substitutions.feed(NamedDataSource) |
|
187 |
context = pub.substitutions.get_context_variables() |
|
188 |
assert context.get('data_source').foobar == [ |
|
189 |
{'id': 'un', 'text': 'un'}, {'id': 'deux', 'text': 'deux'}] |
tests/test_form_pages.py | ||
---|---|---|
8 | 8 |
from wcs.categories import Category |
9 | 9 |
from wcs.roles import Role, logged_users_role |
10 | 10 |
from wcs.tracking_code import TrackingCode |
11 |
from wcs.data_sources import NamedDataSource |
|
11 | 12 |
from wcs import fields |
12 | 13 |
from wcs.sessions import BasicSession |
13 | 14 | |
... | ... | |
307 | 308 |
resp = resp.forms[0].submit('submit') # should go to second page |
308 | 309 |
assert 'f3' in resp.forms[0].fields |
309 | 310 | |
311 |
def test_form_multi_page_condition_data_source(pub): |
|
312 |
formdef = create_formdef() |
|
313 |
formdef.fields = [fields.PageField(id='0', label='1st page', type='page'), |
|
314 |
fields.BoolField(id='1', label='checkbox', varname='checkbox'), |
|
315 |
fields.PageField(id='2', label='2nd page', type='page', |
|
316 |
condition='len(data_source.foobar) > 0'), |
|
317 |
fields.StringField(id='3', label='string 2')] |
|
318 |
formdef.store() |
|
319 | ||
320 |
# add the named data source, empty |
|
321 |
NamedDataSource.wipe() |
|
322 |
data_source = NamedDataSource(name='foobar') |
|
323 |
data_source.data_source = {'type': 'formula', 'value': repr([])} |
|
324 |
data_source.store() |
|
325 | ||
326 |
resp = get_app(pub).get('/test/') |
|
327 |
formdef.data_class().wipe() |
|
328 |
resp = resp.forms[0].submit('submit') # should go straight to validation |
|
329 |
assert 'Check values then click submit.' in resp.body |
|
330 |
assert resp.forms[0]['previous'] |
|
331 |
resp = resp.forms[0].submit('previous') |
|
332 |
assert resp.forms[0]['f1'] |
|
333 | ||
334 |
# replace the named data source with one with items |
|
335 |
NamedDataSource.wipe() |
|
336 |
data_source = NamedDataSource(name='foobar') |
|
337 |
data_source.data_source = {'type': 'formula', 'value': repr(['un', 'deux'])} |
|
338 |
data_source.store() |
|
339 | ||
340 |
resp = resp.forms[0].submit('submit') # should go to second page |
|
341 |
assert 'f3' in resp.forms[0].fields |
|
342 | ||
310 | 343 |
def test_form_submit_with_user(pub): |
311 | 344 |
create_user(pub) |
312 | 345 |
formdef = create_formdef() |
wcs/data_sources.py | ||
---|---|---|
229 | 229 |
return objects[0] |
230 | 230 |
raise KeyError() |
231 | 231 |
get_by_slug = classmethod(get_by_slug) |
232 | ||
233 |
@classmethod |
|
234 |
def get_substitution_variables(cls): |
|
235 |
return {'data_source': DataSourcesSubstitutionProxy()} |
|
236 | ||
237 | ||
238 |
class DataSourcesSubstitutionProxy(object): |
|
239 |
def __getattr__(self, attr): |
|
240 |
return get_structured_items(NamedDataSource.get_by_slug(attr).data_source) |
wcs/root.py | ||
---|---|---|
46 | 46 |
from qommon.afterjobs import AfterJobStatusDirectory |
47 | 47 | |
48 | 48 |
from categories import Category |
49 |
from data_sources import NamedDataSource |
|
49 | 50 |
from formdef import FormDef |
50 | 51 |
from anonylink import AnonymityLink |
51 | 52 |
from roles import Role |
... | ... | |
336 | 337 |
response.breadcrumb = [ ('', _('Home')) ] |
337 | 338 | |
338 | 339 |
get_publisher().substitutions.feed(get_request().user) |
340 |
get_publisher().substitutions.feed(NamedDataSource) |
|
339 | 341 | |
340 | 342 |
if not self.admin: |
341 | 343 |
self.admin = get_publisher().admin_directory_class() |
342 |
- |