Skip to content

Commit

Permalink
S3RL_PDF: ability to limit the number of records in PDF exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nursix committed May 17, 2019
1 parent 761dfe2 commit 7496d8a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nursix-dev-2864-gf1af83e (2019-05-17 12:02:57)
nursix-dev-2865-g761dfe2 (2019-05-17 14:43:58)
62 changes: 56 additions & 6 deletions modules/s3/codecs/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,22 +409,35 @@ def get_resource_flowable(self, resource, doc):
dtfilter, orderby, left = resource.datatable_filter(list_fields, get_vars)
resource.add_filter(dtfilter)

# Should we limit the number of rows in the export?
max_rows = current.deployment_settings.get_pdf_max_rows()
limit, count = (max_rows, True) if max_rows else (None, False)

result = resource.select(list_fields,
left=left,
limit=None,
orderby=orderby,
represent=True,
show_links=False,
count = count,
left = left,
limit = limit,
orderby = orderby,
represent = True,
show_links = False,
)
totalrows = result.numrows if count else None

if resource.get_config("pdf_format") == "list":
output = S3PDFList(doc, result.rfields, result.rows).build()
# Export as data list
output = S3PDFList(doc,
result.rfields,
result.rows,
totalrows = totalrows,
).build()
else:
# Export as data table
output = S3PDFTable(doc,
result.rfields,
result.rows,
groupby = self.pdf_groupby,
autogrow = self.table_autogrow,
totalrows = totalrows,
).build()
return output

Expand Down Expand Up @@ -717,20 +730,25 @@ def __init__(self,
document,
rfields,
rows,
totalrows = None,
):
"""
Constructor
@param document: the DocTemplate
@param rfields: the S3ResourceFields (for labels and order)
@param rows: the data (S3ResourceData.rows)
@param totalrows: total number of rows matching the filter
"""

self.document = document

self.rfields = rfields
self.rows = rows

self.numrows = len(rows)
self.totalrows = totalrows

# Set fonts
self.font_name = None
self.font_name_bold = None
Expand Down Expand Up @@ -768,6 +786,19 @@ def build(self):
item.extend(formatted(rfield, row[rfield.colname]))
flowables.append(KeepTogether(item))

# Hint for too many records
totalrows = self.totalrows
numrows = self.numrows
if totalrows and totalrows > numrows:
hint = current.T("Too many records - %(number)s more records not included") % \
{"number": totalrows - numrows}
stylesheet = getSampleStyleSheet()
style = stylesheet["Normal"]
style.textColor = colors.red
style.fontSize = 12
flowables.append(PageBreak())
flowables.append(Paragraph(s3_str(hint), style))

return flowables

# -------------------------------------------------------------------------
Expand Down Expand Up @@ -922,6 +953,7 @@ def __init__(self,
rows,
groupby = None,
autogrow = False,
totalrows = None,
):
"""
Constructor
Expand All @@ -939,6 +971,7 @@ def __init__(self,
"V" - add extra (empty) rows to fill vertically
"B" - do both
False - do nothing
@param totalrows: total number of rows matching the filter
"""

rtl = current.response.s3.rtl
Expand Down Expand Up @@ -968,6 +1001,10 @@ def __init__(self,
self.labels = labels

# Convert the input data into suitable ReportLab elements

self.totalrows = totalrows
self.numrows = len(rows)

convert = self.convert
data = []
append = data.append
Expand Down Expand Up @@ -1396,6 +1433,19 @@ def presentation(self):
if next_part % num_horz_parts == 0:
start_row += num_rows - 1 # Don't include the heading

# Hint for too many records
totalrows = self.totalrows
numrows = self.numrows
if totalrows and totalrows > numrows:
hint = current.T("Too many records - %(number)s more records not included") % \
{"number": totalrows - numrows}
stylesheet = getSampleStyleSheet()
style = stylesheet["Normal"]
style.textColor = colors.red
style.fontSize = 12
tables.append(PageBreak())
tables.append(Paragraph(s3_str(hint), style))

# Return a list of table objects
return tables

Expand Down
6 changes: 6 additions & 0 deletions modules/s3cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,12 @@ def get_pdf_excluded_fields(self, resourcename):

return excluded_fields.get(resourcename, [])

def get_pdf_max_rows(self):
"""
Maximum number of records in a single PDF table/list export
"""
return self.base.get("pdf_max_rows")

# -------------------------------------------------------------------------
# XLS Export Settings
#
Expand Down
3 changes: 3 additions & 0 deletions modules/templates/DRKCM/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ def config(settings):
"BAMF-Az": "bamf.value",
}

# Limit the number of records in PDF exports
settings.base.pdf_max_rows = 1000

# -------------------------------------------------------------------------
# Realm Rules
#
Expand Down
2 changes: 2 additions & 0 deletions modules/templates/default/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ def config(settings):
#settings.base.pdf_orientation = "Landscape"
# Location of Logo used in pdfs headers
#settings.ui.pdf_logo = "static/img/mylogo.png"
# Limit the number of records in PDF exports
#settings.base.pdf_max_rows = 1000

#Uncomment to add a title row to XLS exports
#settings.base.xls_title_row = True
Expand Down

0 comments on commit 7496d8a

Please sign in to comment.