Ejemplo n.º 1
0
    def export_to(self, path, dataview=None):
        self.ordered_columns = OrderedDict()
        self._build_ordered_columns(self.dd.survey, self.ordered_columns)

        if dataview:
            cursor = dataview.query_data(dataview, all_data=True)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            self._update_columns_from_data(cursor)

            columns = list(
                chain.from_iterable(
                    [[xpath] if cols is None else cols
                     for xpath, cols in self.ordered_columns.iteritems()
                     if [c for c in dataview.columns if xpath.startswith(c)]]))
            cursor = dataview.query_data(dataview, all_data=True)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            data = self._format_for_dataframe(cursor)
        else:
            cursor = self._query_data(self.filter_query)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            self._update_columns_from_data(cursor)

            columns = list(
                chain.from_iterable(
                    [[xpath] if cols is None else cols
                     for xpath, cols in self.ordered_columns.iteritems()]))

            # add extra columns
            columns += [col for col in self.ADDITIONAL_COLUMNS]
            for field in self.dd.get_survey_elements_of_type('osm'):
                columns += OsmData.get_tag_keys(self.xform,
                                                field.get_abbreviated_xpath(),
                                                include_prefix=True)
            cursor = self._query_data(self.filter_query)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            data = self._format_for_dataframe(cursor)

        columns_with_hxl = self.include_hxl and get_columns_with_hxl(
            self.dd.survey_elements)

        write_to_csv(path,
                     data,
                     columns,
                     columns_with_hxl=columns_with_hxl,
                     remove_group_name=self.remove_group_name,
                     dd=self.dd,
                     group_delimiter=self.group_delimiter,
                     include_labels=self.include_labels,
                     include_labels_only=self.include_labels_only,
                     include_hxl=self.include_hxl,
                     win_excel_utf8=self.win_excel_utf8,
                     total_records=self.total_records)
Ejemplo n.º 2
0
    def export_to(self, path, dataview=None):
        self.ordered_columns = OrderedDict()
        self._build_ordered_columns(self.dd.survey, self.ordered_columns)

        if dataview:
            cursor = dataview.query_data(dataview, all_data=True,
                                         filter_query=self.filter_query)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            self._update_columns_from_data(cursor)

            columns = list(chain.from_iterable(
                [[xpath] if cols is None else cols
                 for (xpath, cols) in iteritems(self.ordered_columns)
                 if [c for c in dataview.columns if xpath.startswith(c)]]
            ))
            cursor = dataview.query_data(dataview, all_data=True,
                                         filter_query=self.filter_query)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            data = self._format_for_dataframe(cursor)
        else:
            cursor = self._query_data(self.filter_query)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            self._update_columns_from_data(cursor)

            columns = list(chain.from_iterable(
                [[xpath] if cols is None else cols
                 for (xpath, cols) in iteritems(self.ordered_columns)]))

            # add extra columns
            columns += [col for col in self.extra_columns]
            for field in self.dd.get_survey_elements_of_type('osm'):
                columns += OsmData.get_tag_keys(self.xform,
                                                field.get_abbreviated_xpath(),
                                                include_prefix=True)
            cursor = self._query_data(self.filter_query)
            if isinstance(cursor, QuerySet):
                cursor = cursor.iterator()
            data = self._format_for_dataframe(cursor)

        columns_with_hxl = self.include_hxl and get_columns_with_hxl(
            self.dd.survey_elements)

        write_to_csv(path, data, columns,
                     columns_with_hxl=columns_with_hxl,
                     remove_group_name=self.remove_group_name,
                     dd=self.dd, group_delimiter=self.group_delimiter,
                     include_labels=self.include_labels,
                     include_labels_only=self.include_labels_only,
                     include_hxl=self.include_hxl,
                     win_excel_utf8=self.win_excel_utf8,
                     total_records=self.total_records,
                     index_tags=self.index_tags)