How to get test data into an empty report form
The Dabo report designer doesn't know anything about the data a report will use: no connection information, no SQL, no database schema, no field names. But it's possible to put a TestCursor? tag with some data into the .rfxml file. It works like this:
<TestCursor>
<Record
emp_no = "2"
full_name = "u'Nelson, Robert'"
hire_date = "datetime.datetime(1988, 12, 28, 0, 0)"
job_code = "u'VP'"
/>
<Record
emp_no = "4"
full_name = "u'Young, Bruce'"
hire_date = "datetime.datetime(1988, 12, 28, 0, 0)"
job_code = "u'Eng'"
/>
...
</TestCursor>
(Data taken from the Employee example database that comes with Firebird)
The report designer uses this for the preview and shows the list of fields in the "New object ..." context menu.
The following application, originally written by John Fabiani, takes a connection file, a connection name from it and a SQL query to populate the TestCursor? tag of an empty report form.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import dabo
from dabo.lib.reportUtils import getTestCursorXmlFromDataSet
dabo.ui.loadUI('wx')
class MainForm(dabo.ui.dForm):
def afterInit(self):
self.Sizer = dabo.ui.dSizer('v')
panel = dabo.ui.dPanel(self)
self.Sizer.append1x(panel)
vs = panel.Sizer = dabo.ui.dSizer('v')
vs.appendSpacer(10)
bs = dabo.ui.dBorderSizer(panel, "vertical", Caption=" Report settings ")
gs = dabo.ui.dGridSizer(MaxCols=2,HGap=10, VGap=10)
gs.setColExpand(True, 1)
gs.append(dabo.ui.dLabel(panel, Caption='Connection file:'))
txFile = dabo.ui.dTextBox(panel, RegID='cnFileID')
gs.append(txFile, 'x')
gs.append(dabo.ui.dLabel(panel, Caption='Crypto Key:'))
gs.append(dabo.ui.dTextBox(panel, RegID='cnCryptID'), 'x')
gs.append(dabo.ui.dLabel(panel, Caption='Connection name:'))
gs.append(dabo.ui.dTextBox(panel, RegID='cnNameID'), 'x')
gs.append(dabo.ui.dLabel(panel, Caption='Name for new report:'))
gs.append(dabo.ui.dTextBox(panel, RegID='newRptID'), 'x')
bs.append(gs, 0, 'x')
vs.append(bs, 0, 'x')
ss = dabo.ui.dBorderSizer(panel, "vertical", Caption=" SQL for new report ")
ss.append1x(dabo.ui.dEditBox(panel,RegID="sqlID"))
vs.append(ss,0,'x')
vs.appendSpacer(10)
vs.append(dabo.ui.dButton(panel,Caption = 'Create Report', OnHit=self.createRpt), border=5)
self.layout()
txFile.setFocus()
def initProperties(self):
self.newReportXml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<report>
<page>
<marginBottom>".5 in"</marginBottom>
<marginLeft>".75 in"</marginLeft>
<marginRight>".75 in"</marginRight>
<marginTop>".75 in"</marginTop>
<orientation>"portrait"</orientation>
<size>"letter"</size>
</page>
<pageHeader>
<height>"1.75 in"</height>
<objects>
<string>
<align>"left"</align>
<vAlign>"top"</vAlign>
<vAnchor>"top"</vAnchor>
<borderWidth>"0 pt"</borderWidth>
<expr>"Simple Report"</expr>
<fontName>"Helvetica"</fontName>
<fontBold>True</fontBold>
<fontSize>14</fontSize>
<hAnchor>"left"</hAnchor>
<height>16</height>
<name>companyName</name>
<width>"4 in"</width>
<x>"1.1 in"</x>
<y>"1.7875 in"</y>
</string>
</objects>
</pageHeader>
<detail>
<height>".25 in"</height>
<objects>
<rect>
<height>".25 in"</height>
<strokeWidth>".25 pt"</strokeWidth>
<width>"5.5 in"</width>
<x>"1.5 in"</x>
<y>"0 in"</y>
</rect>
<string>
<expr>"Detail"</expr>
<height>15</height>
<width>"0.80 in"</width>
<x>"1.6 in"</x>
<y>5</y>
</string>
<line>
<strokeWidth>0.25</strokeWidth>
<strokeColor>(.2,.2,.2)</strokeColor>
<x>"2.605 in"</x>
<y>0</y>
<height>self.Bands["detail"]["height"]</height>
<width>0</width>
</line>
</objects>
</detail>
<pageBackground>
<objects>
<rect>
<strokeColor>(.7,.7,.7)</strokeColor>
<height>.25</height>
<width>.25</width>
<x>"0.25 in"</x>
<y>"6.875 in"</y>
</rect>
<rect>
<strokeColor>(.7,.7,.7)</strokeColor>
<height>.25</height>
<width>.25</width>
<x>"8.25 in"</x>
<y>"6.875 in"</y>
</rect>
</objects>
</pageBackground>
<pageFooter>
<height>"2.5 in"</height>
<objects>
<string>
<align>"center"</align>
<borderWidth>"0 pt"</borderWidth>
<expr>"This is a sample report generated by the JF COMPUTER"</expr>
<fontName>"Helvetica"</fontName>
<fontSize>12</fontSize>
<fontItalic>True</fontItalic>
<hAnchor>"center"</hAnchor>
<height>"0.25 in"</height>
<width>"8 in"</width>
<x>"3.5 in"</x>
<y>0</y>
</string>
</objects>
</pageFooter>
<PageForeground></PageForeground>"""
self.testCursorXml = ""
self.newReportTitle = """ <title>Sample Report</title>
<h2></report> """</h2>
def createRpt(self,evt):
self.createTmpCursor()
self.writeRptFile()
dabo.ui.dMessageBox.info("Finished")
def writeRptFile(self):
reptfile= open('%s.rfxml' % self.newRptID.Value, 'w')
reptfile.write(self.totalXml)
reptfile.close()
def createTmpCursor(self):
crypt = self.cnCryptID.Value
if crypt:
self.Application.CryptoKey = crypt
self.Application.addConnectFile('%s.cnxml' % self.cnFileID.Value)
myconn = self.Application.getConnectionByName(self.cnNameID.Value)
mycur = myconn.getDaboCursor()
mycur.execute(self.sqlID.Value)
self.mybizDS = mycur.getDataSet()
self.testCursorXml = getTestCursorXmlFromDataSet(self.mybizDS) ## creates the testcursor xml
self.totalXml = self.newReportXml +'\n'+ self.testCursorXml + self.newReportTitle
print self.totalXml
if __name__ == "__main__":
app = dabo.dApp()
app.MainFormClass = MainForm
app.start()
As it stands this can't be used to change existing test data or to put test data into an existing report form, because the empty form is part of the application.