def dict_for_mongo(d): for key, value in d.items(): if type(value) == list: value = [dict_for_mongo(e) if type(e) == dict else e for e in value] elif type(value) == dict: value = dict_for_mongo(value) elif key == '_id': try: d[key] = int(value) except ValueError: # if it is not an int don't convert it pass if _is_invalid_for_mongo(key): del d[key] d[_encode_for_mongo(key)] = value return d
def dict_for_mongo(d): for key, value in d.items(): if isinstance(value, list): value = [dict_for_mongo(e) if isinstance(e, dict) else e for e in value] elif isinstance(value, dict): value = dict_for_mongo(value) elif key == '_id': try: d[key] = int(value) except ValueError: # if it is not an int don't convert it pass if _is_invalid_for_mongo(key): del d[key] d[_encode_for_mongo(key)] = value return d
def build_sections(current_section, survey_element, sections, select_multiples, gps_fields, encoded_fields, field_delimiter='/', remove_group_name=False): for child in survey_element.children: current_section_name = current_section['name'] # if a section, recurs if isinstance(child, Section): # if its repeating, build a new section if isinstance(child, RepeatingSection): # section_name in recursive call changes section = { 'name': child.get_abbreviated_xpath(), 'elements': [] } self.sections.append(section) build_sections(section, child, sections, select_multiples, gps_fields, encoded_fields, field_delimiter, remove_group_name) else: # its a group, recurs using the same section build_sections(current_section, child, sections, select_multiples, gps_fields, encoded_fields, field_delimiter, remove_group_name) elif isinstance(child, Question) and child.bind.get(u"type")\ not in QUESTION_TYPES_TO_EXCLUDE: # add to survey_sections if isinstance(child, Question): child_xpath = child.get_abbreviated_xpath() _title = ExportBuilder.format_field_title( child.get_abbreviated_xpath(), field_delimiter, dd, remove_group_name) _label = \ dd.get_label(child_xpath, elem=child) or _title current_section['elements'].append({ 'label': _label, 'title': _title, 'xpath': child_xpath, 'type': child.bind.get(u"type") }) if _is_invalid_for_mongo(child_xpath): if current_section_name not in encoded_fields: encoded_fields[current_section_name] = {} encoded_fields[current_section_name].update( {child_xpath: _encode_for_mongo(child_xpath)}) # if its a select multiple, make columns out of its choices if child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE\ and self.SPLIT_SELECT_MULTIPLES: choices = self._get_select_mulitples_choices( child, dd, field_delimiter, remove_group_name) for choice in choices: if choice not in current_section['elements']: current_section['elements'].append(choice) choices_xpaths = [c['xpath'] for c in choices] _append_xpaths_to_section( current_section_name, select_multiples, child.get_abbreviated_xpath(), choices_xpaths) # split gps fields within this section if child.bind.get(u"type") == GEOPOINT_BIND_TYPE: # add columns for geopoint components xpaths = DataDictionary.get_additional_geopoint_xpaths( child.get_abbreviated_xpath()) for xpath in xpaths: _title = ExportBuilder.format_field_title( xpath, field_delimiter, dd, remove_group_name) current_section['elements'].append({ 'label': _title, 'title': _title, 'xpath': xpath, 'type': 'decimal' }) _append_xpaths_to_section( current_section_name, gps_fields, child.get_abbreviated_xpath(), xpaths)