def fill_story(self, object_list, story): pages = split_list(object_list, self.models_per_page) page_count = 0 for pagina in pages: page_count += 1 values_list = [] styles_list = [] for obj in pagina: field_value_list = [] for idx, field in enumerate(self.get_fields()): field_value = get_field(obj, field) value = get_model_field_repr(obj, field) style = PdfStyles.SMALL if isinstance(field_value, (float, int)): styles_list.append(('ALIGN', (idx, 0), (idx, -1), 'RIGHT')) style = PdfStyles.get_style(PdfStyles.SMALL, alignment=PdfStyles.RIGHT_ALIGN) field_value_list.append(Paragraph(value, style=style)) values_list.append(field_value_list) story.append(Paragraph(text=self.get_page_title(), style=PdfStyles.get_style(PdfStyles.LARGE, bold=True))) story.append(Spacer(width=10*mm, height=7*mm)) data = [self.get_page_header_story(), ] data.extend(values_list) if self.widths: table = Table(data, colWidths=self.get_widths()) else: table = Table(data) styles_list.extend([('VALIGN', (0, 0), (-1, -1), 'TOP'), ('GRID', (0, 0), (len(self.get_fields()) - 1, 0), 1, black), ]) table.setStyle(TableStyle(styles_list)) story.append(table) if page_count < len(pages): story.append(PageBreak())
def fill_xls_document(self, document, context): object_list = self.get_queryset(context) sheet = document.add_sheet(self.get_page_title()) config = self.get_field_configuration() for idx, field_conf in enumerate(config): field_header = field_conf['header'] if idx == 0: style = XlsStyles.HEADER_BEGIN elif id == len(config) - 1: style = XlsStyles.HEADER_END else: style = XlsStyles.HEADER_MIDDLE sheet.write(0, idx, field_header, style) for idx, instance in enumerate(object_list): for jdx, field in enumerate(self.get_fields()): value = get_field(instance, field) if not isinstance(value, (int, float, str, datetime.date)): value = get_model_field_repr(instance, field) style = XlsStyles.NORMAL if isinstance(value, datetime.date): style = XlsStyles.DATE sheet.write(idx + 1, jdx, value, style)