def setUp(self): """Creates a PDFForm with buttons, and a document on which the PDF form is rendered. """ self.document = Document('doc_id') pdf_file = open(os.path.join(os.path.dirname(__file__), 'data', 'test_button.pdf'), 'rb') self.pdf_form = PDFForm('test_pdf_form').__of__(self.document) self.pdf_form.manage_upload(pdf_file)
def setUp(self): """Creates a PDFForm, and a document on which the PDF form is rendered. """ super(TestPDFForm, self).setUp() from Products.CMFCore.tests.base.utils import _setUpDefaultTraversable _setUpDefaultTraversable() self.document = Document('doc_id') pdf_file = open(os.path.join(os.path.dirname(__file__), 'data', 'test_1.pdf'), 'rb') self.pdf_form = PDFForm('test_pdf_form').__of__(self.document) self.pdf_form.manage_upload(pdf_file)
class TestPDFFormButtons(unittest.TestCase): """Tests PDF Form with buttons """ def setUp(self): """Creates a PDFForm with buttons, and a document on which the PDF form is rendered. """ self.document = Document('doc_id') pdf_file = open(os.path.join(os.path.dirname(__file__), 'data', 'test_button.pdf'), 'rb') self.pdf_form = PDFForm('test_pdf_form').__of__(self.document) self.pdf_form.manage_upload(pdf_file) def test_getCellNames(self): self.assertEquals(['check_box',], self.pdf_form.getCellNames()) def test_SimpleGeneratePDF(self): self.pdf_form.setCellTALES('check_box', 'python: 1') self.failUnless(self.pdf_form.generatePDF()) # aliases self.failUnless(self.pdf_form.index_html()) self.failUnless(self.pdf_form())
def getPDFForm(self): """ Returns an PDF Form instance (stored in RAM) """ if self.getDefaultScribusFormValue() is None: return portal_type_name = self.getId().replace(' ', '') pdf_form_name = '%s_view%sAsPdf' % (portal_type_name, portal_type_name) pdf_file = StringIO.StringIO(self.getDefaultPdfFormValue().getData()) pdf_form = PDFForm(pdf_form_name, portal_type_name, pdf_file) pdf_form.manage_upload(pdf_file) import_scribus_file = StringIO.StringIO( self.getDefaultScribusFormValue().getData()) scribus_parser = ScribusParser(import_scribus_file) erp5_properties = scribus_parser.getERP5PropertyDict() def_usePropertySheet = 0 my_prefix = 'my_' prefix_len = len(my_prefix) pages = range(len(erp5_properties)) for page in pages: page_content = erp5_properties[page] for cell_name, field_dict in page_content: # current object is PDF Form if cell_name[:prefix_len] == my_prefix: cell_process_name_list = [] suffix = cell_name[prefix_len:].split('_')[-1] # If properties field are filled in scribus, get Type form # global_properties. Else, guess Type by suffix id (eg: List, Date,...) list_field_type_list = ( 'ListField', 'MultiListField', 'LinesField', ) date_field_type_list = ('DateTimeField', ) suffix_mapping = { 'List': list_field_type_list, 'Date': date_field_type_list } field_type = field_dict.get( 'erp_type', suffix_mapping.get(suffix, [''])[0]) if def_usePropertySheet: # generating PropertySheet and Document, no need to use them to # get field data if field_type in list_field_type_list: TALES = "python: %s " % ', '.join( "here.get%s()" % convertToUpperCase(cell_name[prefix_len:])) elif field_type in date_field_type_list: attributes_dict = field_dict['attributes'] # assign the property input_order input_order = attributes_dict['input_order'] input_order = input_order.replace('y', 'Y') # make the Tales according to the cases date_pattern = '/'.join( ['%%%s' % s for s in list(input_order)]) if not (attributes_dict['date_only']): date_pattern += ' %H:%M' TALES = "python: here.get%s() is not None and here.get%s().strftime('%s') or ''"\ % (convertToUpperCase(cell_name[prefix_len:]), convertToUpperCase(cell_name[prefix_len:]), date_pattern) else: TALES = "python: here.get%s()" %\ convertToUpperCase(cell_name[prefix_len:]) else: if field_type in list_field_type_list: TALES = "python: %s" % ', '.join( "here.getProperty('%s')" % cell_name[prefix_len:]) elif field_type in date_field_type_list: attributes_dict = field_dict['attributes'] # assign the property input_order input_order = attributes_dict['input_order'] input_order = input_order.replace('y', 'Y') # make the Tales according to the cases date_pattern = '/'.join( ['%%%s' % s for s in list(input_order)]) if not (attributes_dict['date_only']): date_pattern += ' %H:%M' TALES = "python: here.getProperty('%s') is not None and here.getProperty('%s').strftime('%s') or ''"\ % (cell_name[prefix_len:], cell_name[prefix_len:], date_pattern) else: TALES = "python: here.getProperty('%s')" % cell_name[ prefix_len:] pdf_form.setCellTALES(cell_name, TALES) import_scribus_file.close() pdf_file.close() self.REQUEST.RESPONSE.setHeader('Content-Type', 'application/pdf') return pdf_form
def getPDFForm(self): """ Returns an PDF Form instance (stored in RAM) """ if self.getDefaultScribusFormValue() is None: return portal_type_name = self.getId().replace(' ','') pdf_form_name ='%s_view%sAsPdf' % (portal_type_name, portal_type_name) pdf_file = StringIO.StringIO(self.getDefaultPdfFormValue().getData()) pdf_form = PDFForm(pdf_form_name, portal_type_name, pdf_file) pdf_form.manage_upload(pdf_file) import_scribus_file = StringIO.StringIO(self.getDefaultScribusFormValue().getData()) scribus_parser = ScribusParser(import_scribus_file) erp5_properties = scribus_parser.getERP5PropertyDict() def_usePropertySheet = 0 my_prefix = 'my_' prefix_len = len(my_prefix) pages = range(len(erp5_properties)) for page in pages: page_content = erp5_properties[page] for cell_name, field_dict in page_content: # current object is PDF Form if cell_name[:prefix_len] == my_prefix: cell_process_name_list = [] suffix = cell_name[prefix_len:].split('_')[-1] # If properties field are filled in scribus, get Type form # global_properties. Else, guess Type by suffix id (eg: List, Date,...) list_field_type_list = ('ListField', 'MultiListField', 'LinesField',) date_field_type_list = ('DateTimeField',) suffix_mapping = {'List' : list_field_type_list, 'Date' : date_field_type_list} field_type = field_dict.get('erp_type', suffix_mapping.get(suffix, [''])[0]) if def_usePropertySheet: # generating PropertySheet and Document, no need to use them to # get field data if field_type in list_field_type_list: TALES = "python: %s " % ', '.join( "here.get%s()" % convertToUpperCase(cell_name[prefix_len:])) elif field_type in date_field_type_list: attributes_dict = field_dict['attributes'] # assign the property input_order input_order = attributes_dict['input_order'] input_order = input_order.replace('y', 'Y') # make the Tales according to the cases date_pattern = '/'.join(['%%%s' % s for s in list(input_order)]) if not(attributes_dict['date_only']): date_pattern += ' %H:%M' TALES = "python: here.get%s() is not None and here.get%s().strftime('%s') or ''"\ % (convertToUpperCase(cell_name[prefix_len:]), convertToUpperCase(cell_name[prefix_len:]), date_pattern) else: TALES = "python: here.get%s()" %\ convertToUpperCase(cell_name[prefix_len:]) else: if field_type in list_field_type_list: TALES = "python: %s" % ', '.join( "here.getProperty('%s')" % cell_name[prefix_len:]) elif field_type in date_field_type_list: attributes_dict = field_dict['attributes'] # assign the property input_order input_order = attributes_dict['input_order'] input_order = input_order.replace('y', 'Y') # make the Tales according to the cases date_pattern = '/'.join(['%%%s' % s for s in list(input_order)]) if not(attributes_dict['date_only']): date_pattern += ' %H:%M' TALES = "python: here.getProperty('%s') is not None and here.getProperty('%s').strftime('%s') or ''"\ % (cell_name[prefix_len:], cell_name[prefix_len:], date_pattern) else: TALES = "python: here.getProperty('%s')" % cell_name[prefix_len:] pdf_form.setCellTALES(cell_name, TALES) import_scribus_file.close() pdf_file.close() self.REQUEST.RESPONSE.setHeader('Content-Type', 'application/pdf') return pdf_form
class TestPDFForm(PlacelessSetup, unittest.TestCase): """Tests PDF Form """ def getTitle(self): return "PDF Form" def setUp(self): """Creates a PDFForm, and a document on which the PDF form is rendered. """ super(TestPDFForm, self).setUp() from Products.CMFCore.tests.base.utils import _setUpDefaultTraversable _setUpDefaultTraversable() self.document = Document('doc_id') pdf_file = open(os.path.join(os.path.dirname(__file__), 'data', 'test_1.pdf'), 'rb') self.pdf_form = PDFForm('test_pdf_form').__of__(self.document) self.pdf_form.manage_upload(pdf_file) # if tearDown is ever added, don't forget to call PlacelessSetup.tearDown() def test_getCellNames(self): self.assertEquals(['text_1', 'text_2', 'text_3'], self.pdf_form.getCellNames()) def test_SimpleGeneratePDF(self): self.pdf_form.setCellTALES('text_1', 'string:Something simple') self.failUnless(self.pdf_form.generatePDF()) # aliases self.failUnless(self.pdf_form.index_html()) self.failUnless(self.pdf_form()) def test_EmptyGeneratePdf(self): self.failUnless(self.pdf_form.generatePDF()) # aliases self.failUnless(self.pdf_form.index_html()) self.failUnless(self.pdf_form()) def test_showCellName(self): self.failUnless(self.pdf_form.showCellNames()) def test_CellTALES(self): self.pdf_form.setCellTALES('text_1', 'here/getId') self.assertEquals('here/getId', self.pdf_form.getCellTALES('text_1')) def test_setInvalidTALES(self): from Products.PageTemplates.Expressions import getEngine CompilerError = getEngine().getCompilerError() self.pdf_form.setCellTALES('text_1', 'python:(inv.alid "= ') # maybe should raise when setting the TALES, not when getting ? self.assertRaises(CompilerError, self.pdf_form.evaluateCell, 'text_1') def test_EditCells(self): self.pdf_form.doEditCells(REQUEST=dict(text_1='here/getId', text_2='string:')) self.assertEquals('here/getId', self.pdf_form.getCellTALES('text_1')) self.assertEquals('string:', self.pdf_form.getCellTALES('text_2')) def test_EvaluateCell(self): self.pdf_form.setCellTALES('text_1', 'here/getId') self.assertEquals('doc_id', self.pdf_form.evaluateCell('text_1')) def test_EvaluateNonExistCell(self): self.assertRaises(KeyError, self.pdf_form.evaluateCell, 'this_cell_does_not_exist') def test_CalculateCellValues(self): self.pdf_form.setCellTALES('text_1', 'here/getId') self.pdf_form.setCellTALES('text_2', 'string:static') calculated_values = self.pdf_form.calculateCellValues() self.assertEquals('doc_id', calculated_values['text_1']) self.assertEquals('static', calculated_values['text_2']) def test_CalculateCellValuesWithCellKey(self): self.pdf_form.setCellTALES('text_1', 'here/getId') self.pdf_form.setCellTALES('text_2', 'cell/text_1') calculated_values = self.pdf_form.calculateCellValues() self.assertEquals('doc_id', calculated_values['text_1']) self.assertEquals('doc_id', calculated_values['text_2']) def test_CalculateCellValuesTotal(self): # The original use case of `cell` self.pdf_form.setCellTALES('text_1', 'python:3') self.pdf_form.setCellTALES('text_2', 'python:2') self.pdf_form.setCellTALES('text_3', 'python:cell["text_1"] + cell["text_2"]') self.assertEquals(3 + 2, self.pdf_form.calculateCellValues()['text_3']) def test_CalculateCellValuesCircularRefs(self): self.pdf_form.setCellTALES('text_1', 'cell/text2') self.pdf_form.setCellTALES('text_2', 'cell/text_1') from Products.ERP5Form.PDFForm import CircularReferencyError self.assertRaises(CircularReferencyError, self.pdf_form.calculateCellValues) def test_CalculateCellValuesParms(self): self.pdf_form.setCellTALES('text_1', 'a_parameter') calculated_values = self.pdf_form.calculateCellValues(a_parameter='Value') self.assertEquals('Value', calculated_values['text_1'])