def _build_ordered_columns(cls, survey_element, ordered_columns, is_repeating_section=False): """ Build a flat ordered dict of column groups is_repeating_section ensures that child questions of repeating sections are not considered columns """ for child in survey_element.children: child_xpath = child.get_abbreviated_xpath() if isinstance(child, Section): child_is_repeating = False if isinstance(child, RepeatingSection): ordered_columns[child.get_abbreviated_xpath()] = [] child_is_repeating = True cls._build_ordered_columns(child, ordered_columns, child_is_repeating) elif isinstance(child, Question) and not \ question_types_to_exclude(child.type) and not\ is_repeating_section:# if is_repeating_section, its parent # already initiliased an empty list so we dont add it to our # list of columns, the repeating columns list will be # generated when we reindex ordered_columns[child.get_abbreviated_xpath()] = None
def xpaths(self, prefix="", survey_element=None, result=None, repeat_iterations=4): """ Return a list of XPaths for this survey that will be used as headers for the csv export. """ if survey_element is None: survey_element = self.survey elif question_types_to_exclude(survey_element.type): return [] if result is None: result = [] path = "/".join([prefix, unicode(survey_element.name)]) if survey_element.children is not None: # add xpaths to result for each child indices = ( [""] if type(survey_element) != RepeatingSection else ["[%d]" % (i + 1) for i in range(repeat_iterations)] ) for i in indices: for e in survey_element.children: self.xpaths(path + i, e, result, repeat_iterations) if isinstance(survey_element, Question): result.append(path) # replace the single question column with a column for each # item in a select all that apply question. if survey_element.bind.get(u"type") == u"select": result.pop() for child in survey_element.children: result.append("/".join([path, child.name])) elif survey_element.bind.get(u"type") == u"geopoint": result += self.get_additional_geopoint_xpaths(path) return result
def xpaths(self, prefix='', survey_element=None, result=None, repeat_iterations=4): """ Return a list of XPaths for this survey that will be used as headers for the csv export. """ if survey_element is None: survey_element = self.survey elif question_types_to_exclude(survey_element.type): return [] if result is None: result = [] path = '/'.join([prefix, unicode(survey_element.name)]) if survey_element.children is not None: # add xpaths to result for each child indices = [''] if type(survey_element) != RepeatingSection else \ ['[%d]' % (i + 1) for i in range(repeat_iterations)] for i in indices: for e in survey_element.children: self.xpaths(path + i, e, result, repeat_iterations) if isinstance(survey_element, Question): result.append(path) # replace the single question column with a column for each # item in a select all that apply question. if survey_element.bind.get(u'type') == u'select': result.pop() for child in survey_element.children: result.append('/'.join([path, child.name])) elif survey_element.bind.get(u'type') == u'geopoint': for suffix in self.geodata_suffixes: result.append('_'.join([path, suffix])) return result
def _add_sheets(self): for e in self._data_dictionary.get_survey_elements(): if isinstance(e, Section): sheet_name = e.name self.add_sheet(sheet_name) for f in e.children: if isinstance(f, Question) and\ not question_types_to_exclude(f.type): self.add_column(sheet_name, f.name)
def _add_sheets(self): for e in self._data_dictionary.get_survey_elements(): if isinstance(e, Section): sheet_name = e.name self.add_sheet(sheet_name) for f in e.children: if isinstance(f, Question) and\ not question_types_to_exclude(f.type): self.add_column(sheet_name, f.name)
def _generate_sections(self): """ Split survey questions into separate sections for each xls sheet and columns for each section """ # clear list self.sections = OrderedDict() self.survey_name, survey_xpath = survey_name_and_xpath_from_dd(self.dd) # generate a unique and valid xls sheet name self.survey_name = get_valid_sheet_name(self.survey_name, self.sections.keys()) # setup the default section self._create_section(self.survey_name, survey_xpath, False) # dict of select multiple elements self.select_multiples = {} # get form elements to split repeats into separate section/sheets and # everything else in the default section for e in self.dd.get_survey_elements(): # check for a Section or sub-classes of if isinstance(e, Section): # always default to the main sheet sheet_name = self.survey_name # if a repeat we use its name if isinstance(e, RepeatingSection): sheet_name = e.name sheet_name = get_valid_sheet_name(sheet_name, self.sections.keys()) self._create_section(sheet_name, e.get_abbreviated_xpath(), True) # for each child add to survey_sections for c in e.children: if isinstance(c, Question) and not \ question_types_to_exclude(c.type)\ and not c.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE: self._add_column_to_section(sheet_name, c) elif c.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE: self.select_multiples[c.get_abbreviated_xpath()] = \ [option.get_abbreviated_xpath() for option in c.children] # if select multiple, get its choices and make them # columns for option in c.children: self._add_column_to_section(sheet_name, option) # split gps fields within this section if c.bind.get(u"type") == GEOPOINT_BIND_TYPE: # add columns for geopoint components for xpath in\ self.dd.get_additional_geopoint_xpaths( c.get_abbreviated_xpath()): self._add_column_to_section(sheet_name, xpath) self.get_exceeds_xls_limits()
def _build_sections_recursive(self, section_name, element, is_repeating=False): """Builds a section's children and recurses any repeating sections to build those as a separate section """ for child in element.children: # if a section, recurse if isinstance(child, Section): new_is_repeating = isinstance(child, RepeatingSection) new_section_name = section_name # if its repeating, build a new section if new_is_repeating: new_section_name = get_valid_sheet_name( child.name, self.sections.keys()) self._create_section(new_section_name, child.get_abbreviated_xpath(), True) self._build_sections_recursive(new_section_name, child, new_is_repeating) else: # add to survey_sections if isinstance(child, Question) and not \ question_types_to_exclude(child.type)\ and not child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE: self._add_column_to_section(section_name, child) elif child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE: self.select_multiples[child.get_abbreviated_xpath()] = \ [option.get_abbreviated_xpath() for option in child.children] # if select multiple, get its choices and make them # columns if self.split_select_multiples: for option in child.children: self._add_column_to_section(section_name, option) else: self._add_column_to_section(section_name, child) # split gps fields within this section if child.bind.get(u"type") == GEOPOINT_BIND_TYPE: # add columns for geopoint components for xpath in\ self.dd.get_additional_geopoint_xpaths( child.get_abbreviated_xpath()): self._add_column_to_section(section_name, xpath)
def _build_sections_recursive(self, section_name, element, is_repeating=False): """Builds a section's children and recurses any repeating sections to build those as a separate section """ for child in element.children: # if a section, recurse if isinstance(child, Section): new_is_repeating = isinstance(child, RepeatingSection) new_section_name = section_name # if its repeating, build a new section if new_is_repeating: new_section_name = get_valid_sheet_name( child.name, self.sections.keys()) self._create_section(new_section_name, child.get_abbreviated_xpath(), True) self._build_sections_recursive( new_section_name, child, new_is_repeating) else: # add to survey_sections if isinstance(child, Question) and not \ question_types_to_exclude(child.type)\ and not child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE: self._add_column_to_section(section_name, child) elif child.bind.get(u"type") == MULTIPLE_SELECT_BIND_TYPE: self.select_multiples[child.get_abbreviated_xpath()] = \ [option.get_abbreviated_xpath() for option in child.children] # if select multiple, get its choices and make them # columns if self.split_select_multiples: for option in child.children: self._add_column_to_section(section_name, option) else: self._add_column_to_section(section_name, child) # split gps fields within this section if child.bind.get(u"type") == GEOPOINT_BIND_TYPE: # add columns for geopoint components for xpath in\ self.dd.get_additional_geopoint_xpaths( child.get_abbreviated_xpath()): self._add_column_to_section(section_name, xpath)
def _build_ordered_columns(cls, survey_element, ordered_columns, is_repeating_section=False): """ Build a flat ordered dict of column groups is_repeating_section ensures that child questions of repeating sections are not considered as columns """ for child in survey_element.children: child_xpath = child.get_abbreviated_xpath() if isinstance(child, Section): if isinstance(child, RepeatingSection): ordered_columns[child.get_abbreviated_xpath()] = [] is_repeating_section = True cls._build_ordered_columns(child, ordered_columns, is_repeating_section) elif isinstance(child, Question) and not \ question_types_to_exclude(child.type) and not\ is_repeating_section: ordered_columns[child.get_abbreviated_xpath()] = None
def xpaths(self, prefix='', survey_element=None, result=None, repeat_iterations=4): """ Return a list of XPaths for this survey that will be used as headers for the csv export. """ if survey_element is None: survey_element = self.survey elif question_types_to_exclude(survey_element.type): return [] if result is None: result = [] path = '/'.join([prefix, unicode(survey_element.name)]) if survey_element.children is not None: # add xpaths to result for each child indices = [''] if type(survey_element) != RepeatingSection else \ ['[%d]' % (i + 1) for i in range(repeat_iterations)] for i in indices: for e in survey_element.children: self.xpaths(path + i, e, result, repeat_iterations) if isinstance(survey_element, Question): result.append(path) # replace the single question column with a column for each # item in a select all that apply question. if survey_element.bind.get(u'type') == u'select': result.pop() for child in survey_element.children: result.append('/'.join([path, child.name])) elif survey_element.bind.get(u'type') == u'geopoint': for suffix in self.geodata_suffixes: result.append('_'.join([path, suffix])) return result
def _build_ordered_columns(cls, survey_element, ordered_columns, is_repeating_section=False): """ Build a flat ordered dict of column groups is_repeating_section ensures that child questions of repeating sections are not considered as columns """ for child in survey_element.children: child_xpath = child.get_abbreviated_xpath() if isinstance(child, Section): child_is_repeating = False if isinstance(child, RepeatingSection): ordered_columns[child.get_abbreviated_xpath()] = [] child_is_repeating = True cls._build_ordered_columns(child, ordered_columns, child_is_repeating) elif isinstance(child, Question) and not \ question_types_to_exclude(child.type) and not\ is_repeating_section:# if is_repeating_section, its parent # already initiliased an empty list so we dont add it to our # list of columns, the repeating columns list will be # generated when we reindex ordered_columns[child.get_abbreviated_xpath()] = None