Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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
Пример #4
0
 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)
Пример #5
0
 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)
Пример #6
0
    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
                child_bind_type = child.bind.get(u"type")
                if isinstance(child, Question) and not \
                        question_types_to_exclude(child.type)\
                        and not child_bind_type == MULTIPLE_SELECT_BIND_TYPE:
                    self._add_column_to_section(section_name, child)
                elif child_bind_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_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)
Пример #7
0
    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
                child_bind_type = child.bind.get(u"type")
                if isinstance(child, Question) and not \
                        question_types_to_exclude(child.type)\
                        and not child_bind_type == MULTIPLE_SELECT_BIND_TYPE:
                    self._add_column_to_section(section_name, child)
                elif child_bind_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_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)