3.2.2.5.2. Main class¶
The Main class inherits directly from the TableScriptToHtml class, so write:
class Main(TableScriptToHtml):In the Main class you have to add some webpage variables and some methods that allow to customize the settings file:
Last page update: November 23, 2011
First steps:
Features:
File creation:
Examples:
Library reference:
The print layout file allows to specify the layout of a print
- In the File location section we describe the specific location of the print layout file
- In the ... TODO Layout page - features, Layout print regions
Once you created the file you have to:
- import the correct module - import section
- create the Main class - Main class section
Inside the Main class you may customize your layout through:
- some variables - Main class webpage variables section
- some methods - Main class methods section
The location of the print layout file must follow a standard path followed by a custom path:
projectName/packages/packageName/resources/tables/tableName/customPathwhere:
projectName is the name of the project
packages is the packages folder
packageName is the name of the package
resources is the resources folder
tables is the tables folder
tableName is the name of the table related to the print
customPath is the path you choose for your print layout file through the “html_res” webpage variable of the Main class of the print settings file (there is any convention about the name you have to use)
customPath syntax:
anyFolderYouWant/fileNameOfThePrintSettingsFileExample:
if you have a project called base, a package called invoice, a doctor table and in your print settings file html_res = 'html_builder/my_layout', then the path of the print layout file is:
base/packages/invoice/resources/tables/doctor/html_builder/my_layoutwhere “html_builder” is a folder, “my_layout” is the file name of the print layout file.
This is a graphical map of the location of the print layout file into a project:
In this image the print layout file is called “custom_file”; html_res = ‘custom_folder/custom_file’![]()
There are two different layout for a print:
the “letterhead” layout print - a print of the data of a single record
Note
this layout can be useful for example to print the employee cards of a company, the personal card of a doctor, and so on
the “grid” layout print - a print of a set of many data taken from various database tables
Note
this layout can be useful for example to print the monthly sales of a company
In the next two sections we describe these two different layout modes
TODO
For a complete example, check the “letterhead” layout print - example example
TODO
For a complete example, check the “grid” print layout - example example
TODO
TODO an image with the graphical differences between page - doc - grid
CLIPBOARD:
here you define the main constants: maintable, some heights page header/footer doc header/footer grid header/footer for page we mean the sheet itself so logo or page numbers can fill that header or footer doc header/footer should contains the proper record info grid are the rows (the invoice rows for example) a selection that is related to the primary record or entity you need to print it can have a header (tipically the name of the columns themself) and a footer that we can use for the totals (you can put them inside the doc footer) so these are the main areas so grid_col_widths is the standard widths for the main grid's columns if you put a height to 0 the hook does not being called grid_col_headers is similar to the columns of a standard table
TODO
TODO
TODO
In order to use the layout functionalities you have to import in your print layout file the TableScriptToHtml class:
from gnr.web.gnrbaseclasses import TableScriptToHtmlThen we have to create the Main class:
The Main class inherits directly from the TableScriptToHtml class, so write:
class Main(TableScriptToHtml):In the Main class you have to add some webpage variables and some methods that allow to customize the settings file:
Note
the unit of measurement of all the numerical variables are in millimeters (e.g: print_width = 200 means 200 millimeters)
With the term webpages variables we mean that there are some defined variables belonging to the two parent classes (the BagToHtml class and the TableScriptToHtml class) of the Main class that allow you to customize your print layout.
We list here all these variables with a short description about them, specifying their parent class; if you need a more complete description, click on their name to go on their description section.
List of the webpage variables of the BagToHtml class:
List of the webpage variables of the TableScriptToHtml class:
In this section we introduce most of the important layout hook methods that allow you to personalize the print. For more information, click on the relative links
They are:
- mainLayout(): MANDATORY - it gives the page object through which you create the print
- gridLayout(): if you have a grid then this method is mandatory - it gives the grid object through which you create the grid layout
- defineStandardStyles(): TODO
- prepareRow(): TODO
- onRecordLoaded(): TODO
Warning
the following four methods can be used if and only if there is a webpage variable defined with a different value with respect to 0. For those methods we point up the related webpage variable
- docHeader() define the header of the doc. To use it give a different value to the doc_header_height webpage variable with respect to 0
- docFooter(): define the footer of the doc. To use it give a different value to the doc_footer_height webpage variable with respect to 0
- pageHeader(): define the header of the page. To use it give a different value to the page_header_height webpage variable with respect to 0
- pageFooter(): define the footer of the page. To use it give a different value to the page_header_height webpage variable with respect to 0
Inside the Main class methods, you can create the layout through the following four methods:
- prepareRow(self, row)¶
This method allow to define all the rows of the grid
Note
to select data from a database table, you have to use the rowCell(), not the normal cell():
def prepareRow(self, row): style_cell = 'text-indent:2mm;border-bottom-style:dotted;' self.rowCell('data', style=style_cell)
The lastPage attribute belongs to the pageFooter() and the docFooter() methods
usage of lastPage in the docFooter() method:
In some cases you need that the docFooter is used only in the last page (for example, when you print an extract of the monthly doctor invoices and you want in the last page the total sum of doctor’s operations)
To use the docFooter() in this way, write at the beginning of the method these two lines:
if not lastPage: returnlastPage is automatically passes as True when the print batch is going to create the last page
usage of lastPage in the pageFooter() method:
If you need to modify the footer of the page, you can use the pageFooter method. If you need to create a different pageFooter in the last page, you can use the lastPage attribute.
Just write at the beginning of the method these two lines:
if not lastPage: returnlastPage is automatically passes as True when the print batch is going to create the last page
Let’s see an example page of a “grid” layout print; read the comments that explain the code
TODO
Let’s see an example page of a “letterhead” layout print; read the comments that explain the code
TODO translate the code and add comments!
#!/usr/bin/env pythonw # -*- coding: UTF-8 -*- from gnr.web.gnrbaseclasses import TableScriptToHtml from gnr.core.gnrbag import Bag class Main(TableScriptToHtml): maintable = 'polimed.medico' # rows_table = 'polimed.prestazione' doc_header_height = 10 # docHeader is the callback doc_footer_height = 10 # docFooter is the callback grid_header_height = 6.2 # bodyHeaderHeight grid_col_widths = [17,12,0,0,20,15,15,20] # rowColWidth grid_col_headers = 'Date,Hour,Patient,Performance,Convention,Amount,Cost,Invoice' grid_row_height = 5.3 row_mode = 'attribute' rows_path = 'rows' def mainLayout(self, page): style = """font-family:"Lucida Grande", Lucida, Verdana, sans-serif; text-align:left; line-height:5mm; font-size:9pt;""" return page.layout(width=190, height=self.page_height, um='mm',top=0, left=5,border_width=0, lbl_height=4,lbl_class='caption', style=style) def docHeader(self, header): layout = header.layout(um='mm', lbl_class='smallCaption', top=1,bottom=1,left=1,right=1, lbl_height=3, border_width=.3, border_color='gray', style='line-height:6mm;text-align:left;text-indent:2mm;') row=layout.row(height=10) row.cell("%s %s" % (self.field('@anagrafica_id.cognome'), self.field('@anagrafica_id.nome')), lbl='Performances of') row.cell(self.toText(self.getData('period.from')), lbl='From',width=30,content_class='aligned_right') row.cell(self.toText(self.getData('period.to')), lbl='To', width=30,content_class='aligned_right') row.cell(self.pageCounter(mask='%s of %s'), lbl='Page', width=12, content_class='aligned_right') def docFooter(self, footer, lastPage=None): if not lastPage: return layout = footer.layout(name='footerL',um='mm',border_color='gray', lbl_class='smallCaption', top=1,bottom=1,left=80,right=1, lbl_height=3,border_width=0.3, content_class='aligned_right') row=layout.row(height=0) totals_dict = {} totals_dict['importo'],totals_dict['costo'] = self.getData('rows').sum('#a.importo,#a.costo') row.cell(self.toText(totals_dict['importo'],format=self.currencyFormat), lbl='Total amount') row.cell(self.toText(totals_dict['costo'],format=self.currencyFormat), lbl='Total cost') def gridLayout(self, grid): return grid.layout(name='rowsL',um='mm',border_color='gray', top=1,bottom=1,left=1,right=1, border_width=0.3,lbl_class='caption', style='line-height:5mm;text-align:left;font-size:7.5pt') def prepareRow(self, row): style_cell = 'text-indent:2mm;border-bottom-style:dotted;' self.rowCell('data', style=style_cell) self.rowCell('ora', format='HH:mm', style=style_cell) self.rowCell('paziente', style=style_cell) self.rowCell('prestazione', style=style_cell) self.rowCell('convenzione_codice', style=style_cell) self.rowCell('importo', format=self.currencyFormat, style=style_cell,content_class='aligned_right') self.rowCell('costo', format=self.currencyFormat, style=style_cell,content_class='aligned_right') self.rowCell('fattura', style=style_cell, content_class='aligned_right') def onRecordLoaded(self): where = '$date >= :begin_date AND $date <= :end_date AND doctor_id=:d_id' columns ='''$doctor,$date,$hour,$patient,$performance, @convention_id.code AS convention_code, $amount,$cost,@invoice_id.number AS invoice''' query = self.db.table(self.rows_table).query(columns=columns, where=where, begin_data = self.getData('period.from'), end_data = self.getData('period.to'), d_id=self.record['id']) selection = query.selection() if not selection: return False self.setData('rows',selection.output('grid'))"""
Note
my clipboard...
--Layout, righe e celle-- Per posizionare le cose, abbiamo a disposizione tre oggetti: * **layout**. Possono contenere soltanto righe. * **row**. Possono contenere soltanto celle. Le righe hanno l'altezza, se non viene specificata (o se è zero) la riga è elastica. * **celle**. Possono contenere UN SOLO layout. Le celle hanno la larghezza. Due celle attaccate autocollassano i bordi (rimane un bordo solo). --Attributi e callbacks-- Il foglio è diviso in varie parti che hanno corrispondenti callbacks: (attributo, callback) attributo page_header, callback pageHeader -- header della pagina (es. per carta intestata) page_footer, callback pageFooter -- footer della pagina (es. per carta intestata) callback docHeader -- intestazione del documento callback docFooter -- footer del documento callback prepareRow -- chiamato per ogni riga del corpo Il ``pageHeader``/``pageFooter`` è solitamente riservato alla carta intestata o al template, ``docHeader``/``docFooter`` viene usato per la testata/footer. Ad esempio, in una stampa fattura, l'intestazione va nel ``docHeader`` mentre le righe nel corpo. ``prepareRow`` viene chiamata in automatico per ogni riga. Ha una sintassi tipo field. Il componente prende i dati da una tabella, ma se invece si vogliono passare dati con un altro sistema si può ridefinire il metodo ``loadRecord``
For the complete library reference, check:
- the TableScriptToHtml class
- the BagToHtml class