| 595 | | |
| 596 | | def build_data_from_dict(self, query, config): |
| 597 | | x = query.setTag(common.xmpp.NS_DATA + ' x', attrs = {'type': 'submit'}) |
| 598 | | i = 0 |
| 599 | | while config.has_key(i): |
| 600 | | if not config[i].has_key('type'): |
| 601 | | i += 1 |
| 602 | | continue |
| 603 | | if config[i]['type'] == 'fixed': |
| 604 | | i += 1 |
| 605 | | continue |
| 606 | | tag = x.addChild('field') |
| 607 | | if config[i].has_key('var'): |
| 608 | | tag.setAttr('var', config[i]['var']) |
| 609 | | if config[i].has_key('values'): |
| 610 | | for val in config[i]['values']: |
| 611 | | if val == False: |
| 612 | | val = '0' |
| 613 | | elif val == True: |
| 614 | | val = '1' |
| 615 | | # Force to create a new child |
| 616 | | tag.addChild('value').addData(val) |
| 617 | | i += 1 |
| 618 | | |
| | 595 | |
| 1800 | | |
| 1801 | | |
| 1802 | | def parse_data_form(self, node): |
| 1803 | | dic = {} |
| 1804 | | tag = node.getTag('title') |
| 1805 | | if tag: |
| 1806 | | dic['title'] = tag.getData() |
| 1807 | | tag = node.getTag('instructions') |
| 1808 | | if tag: |
| 1809 | | dic['instructions'] = tag.getData() |
| 1810 | | i = 0 |
| 1811 | | for child in node.getChildren(): |
| 1812 | | if child.getName() != 'field': |
| 1813 | | continue |
| 1814 | | var = child.getAttr('var') |
| 1815 | | ctype = child.getAttr('type') |
| 1816 | | label = child.getAttr('label') |
| 1817 | | if not var and ctype != 'fixed': # We must have var if type != fixed |
| 1818 | | continue |
| 1819 | | dic[i] = {} |
| 1820 | | if var: |
| 1821 | | dic[i]['var'] = var |
| 1822 | | if ctype: |
| 1823 | | dic[i]['type'] = ctype |
| 1824 | | if label: |
| 1825 | | dic[i]['label'] = label |
| 1826 | | tags = child.getTags('value') |
| 1827 | | if len(tags): |
| 1828 | | dic[i]['values'] = [] |
| 1829 | | for tag in tags: |
| 1830 | | data = tag.getData() |
| 1831 | | if ctype == 'boolean': |
| 1832 | | if data in ('yes', 'true', 'assent', '1'): |
| 1833 | | data = True |
| 1834 | | else: |
| 1835 | | data = False |
| 1836 | | dic[i]['values'].append(data) |
| 1837 | | tag = child.getTag('desc') |
| 1838 | | if tag: |
| 1839 | | dic[i]['desc'] = tag.getData() |
| 1840 | | option_tags = child.getTags('option') |
| 1841 | | if len(option_tags): |
| 1842 | | dic[i]['options'] = {} |
| 1843 | | j = 0 |
| 1844 | | for option_tag in option_tags: |
| 1845 | | dic[i]['options'][j] = {} |
| 1846 | | label = option_tag.getAttr('label') |
| 1847 | | tags = option_tag.getTags('value') |
| 1848 | | dic[i]['options'][j]['values'] = [] |
| 1849 | | for tag in tags: |
| 1850 | | dic[i]['options'][j]['values'].append(tag.getData()) |
| 1851 | | if not label: |
| 1852 | | label = dic[i]['options'][j]['values'][0] |
| 1853 | | dic[i]['options'][j]['label'] = label |
| 1854 | | j += 1 |
| 1855 | | if not dic[i].has_key('values'): |
| 1856 | | dic[i]['values'] = [dic[i]['options'][0]['values'][0]] |
| 1857 | | i += 1 |
| 1858 | | return dic |