def test_missing_opening(self): """test orphaned /for raises a TemplateException""" template_name = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_missing_open_template.odt') outname = get_secure_filename() try: template = Template(template_name, outname) finally: os.remove(outname) class Item(object): def __init__(self, val): self.val = val data_dict = {"items": [Item(1), Item(2), Item(3), Item(4)]} template.set_image_path( 'staticimage.logo', pkg_resources.resource_filename( 'py3o.template', 'tests/templates/images/new_logo.png')) # this will raise a TemplateException... or the test will fail error_occured = False try: template.render(data_dict) except TemplateException as e: error_occured = True # make sure this is the correct TemplateException that pops assert e.message == "No open instruction for /for" # and make sure we raised assert error_occured is True
def renderdoc(data_input, outputfile): t = Template(os.path.abspath("masterdata/bestellung_template.odt"), outputfile) t.set_image_path('staticimage.logo', os.path.abspath("masterdata/logo.png")) supplier = data_input['supplier'] responsible = Staff04.objects.get(id=data_input['responsible']) items = [] total = '%.2f' % sum(item['amount'] for item in data_input['data']) for item in data_input['data']: if item['packing'] != '': item['packing'] = 'Verpackt als: %s' % item['packing'] items.append( {'id': item['prodid'], 'name': item['name'], 'unit': item['unit'], 'quantity': '%.3f' % item['quantity'], 'price': '%.2f' % item['price'], 'amount': '%.2f' % item['amount'], 'packing': item['packing'], 'comment': item['comment']}) recipient = {'address': format_py3o_context_value( '%s\n%s\n\n%s %s' % (supplier['namea'], supplier['address'], supplier['zipcode'], supplier['city']))} sender = {'address': data_input['adr_kurz'], 'info': 'info'} # company specific info = {'kostenstelle': data_input['kostenstelle'], 'bez_kostenstelle': data_input['bez_kostenstelle'], 'id': data_input['id'], 'date': data_input['docdate'], 'bauleiter': '%s %s' % (responsible.firstname, responsible.lastname), 'bauleitertel': responsible.mobile, 'polier': '', 'lieferadresse': data_input['adr_lang'], 'infotext': format_py3o_context_value(unicode(data_input['infotext']))} data = dict(items=items, recipient=recipient, sender=sender, info=info, total=total) t.render(data)
def test_link_validation_missing_equal(self): """test a missing equal sign in a link raises a template error""" template_name = pkg_resources.resource_filename( "py3o.template", "tests/templates/py3o_missing_eq_in_link.odt" ) outname = get_secure_filename() template = Template(template_name, outname) class Item(object): def __init__(self, val): self.val = val data_dict = {"items": [Item(1), Item(2), Item(3), Item(4)]} template.set_image_path( "staticimage.logo", pkg_resources.resource_filename( "py3o.template", "tests/templates/images/new_logo.png" ), ) except_occured = False error_text = "" try: template.render(data_dict) except TemplateException as e: except_occured = True error_text = "{}".format(e) assert except_occured is True assert error_text == ( "Missing '=' in instruction 'for \"item in items\"'" )
def test_example_1(self): template_name = pkg_resources.resource_filename( "py3o.template", "tests/templates/py3o_example_template.odt" ) outname = get_secure_filename() template = Template(template_name, outname) template.set_image_path( "staticimage.logo", pkg_resources.resource_filename( "py3o.template", "tests/templates/images/new_logo.png" ), ) class Item(object): pass items = list() item1 = Item() item1.val1 = "Item1 Value1" item1.val2 = "Item1 Value2" item1.val3 = "Item1 Value3" item1.Currency = "EUR" item1.Amount = "12345.35" item1.InvoiceRef = "#1234" items.append(item1) # if you are using python 2.x you should use xrange for i in range(1000): item = Item() item.val1 = "Item%s Value1" % i item.val2 = "Item%s Value2" % i item.val3 = "Item%s Value3" % i item.Currency = "EUR" item.Amount = "6666.77" item.InvoiceRef = "Reference #%04d" % i items.append(item) document = Item() document.total = "9999999999999.999" data = dict(items=items, document=document) error = False try: template.render(data) except ValueError: print("The template did not render properly...") traceback.print_exc() error = True assert error is False
def test_example_1(self): template_name = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_example_template.odt') outname = get_secure_filename() template = Template(template_name, outname) template.set_image_path( 'staticimage.logo', pkg_resources.resource_filename( 'py3o.template', 'tests/templates/images/new_logo.png')) class Item(object): pass items = list() item1 = Item() item1.val1 = 'Item1 Value1' item1.val2 = 'Item1 Value2' item1.val3 = 'Item1 Value3' item1.Currency = 'EUR' item1.Amount = '12345.35' item1.InvoiceRef = '#1234' items.append(item1) # if you are using python 2.x you should use xrange for i in range(1000): item = Item() item.val1 = 'Item%s Value1' % i item.val2 = 'Item%s Value2' % i item.val3 = 'Item%s Value3' % i item.Currency = 'EUR' item.Amount = '6666.77' item.InvoiceRef = 'Reference #%04d' % i items.append(item) document = Item() document.total = '9999999999999.999' data = dict(items=items, document=document) error = False try: template.render(data) except ValueError as e: print('The template did not render properly...') traceback.print_exc() error = True assert error is False
def test_list_duplicate(self): """test duplicated listed get a unique id""" template_name = pkg_resources.resource_filename( "py3o.template", "tests/templates/py3o_list_template.odt" ) outname = get_secure_filename() template = Template(template_name, outname) class Item(object): def __init__(self, val): self.val = val data_dict = {"items": [Item(1), Item(2), Item(3), Item(4)]} error = False template.set_image_path( "staticimage.logo", pkg_resources.resource_filename( "py3o.template", "tests/templates/images/new_logo.png" ), ) template.render(data_dict) outodt = zipfile.ZipFile(outname, "r") try: content_list = [ lxml.etree.parse(BytesIO(outodt.read(filename))) for filename in template.templated_files ] except lxml.etree.XMLSyntaxError as e: error = True print("List was not deduplicated->{}".format(e)) # remove end file os.unlink(outname) assert error is False # first content is the content.xml content = content_list[0] list_expr = "//text:list" list_items = content.xpath(list_expr, namespaces=template.namespaces) ids = [] for list_item in list_items: ids.append(list_item.get("{}id".format(XML_NS))) assert ids, "this list of ids should not be empty" assert len(ids) == len(set(ids)), "all ids should have been unique"
def renderdoc1(data_input, outputfile): t = Template(os.path.abspath("masterdata/lagerausgang_template.odt"), outputfile) t.set_image_path('staticimage.logo', os.path.abspath("masterdata/logo_sofico.png")) #responsible = Staff01.objects.get(id=data_input['responsible']) items = [] #total = '%.2f' % sum(item['amount'] for item in data_input['data']) for item in data_input['data']: packing = item['packing'] if item['packing'] else '' comment = item['comment'] if item['comment'] else '' items.append( {'id': item['prodid'], 'name': item['name'], 'unit': item['unit'], 'quantity': '%.3f' % item['quantity'], 'price': '%.2f' % item['price'], 'packing': packing, 'comment': comment}) # company specific info = {'kostenstelle': data_input['modulerefid'], 'stock': data_input["stockid"], 'bez_kostenstelle': data_input['subject'], 'id':data_input['id'], 'date': data_input['docdate'], 'recipient':'%s'%(data_input['responsible']), 'polier': data_input['leader'], 'abholer': data_input['abholer']} data = dict(items=items, info=info) t.render(data)
from py3o.template import Template t = Template("py3o_example_template_single_cell.odt", "py3o_example_output_single_cell.odt") t.set_image_path('logo', 'images/new_logo.png') class Item(object): pass items = list() item1 = Item() item1.val1 = 'Item1 Value1' item1.val2 = 'Item1 Value2' item1.val3 = 'Item1 Value3' item1.Currency = 'EUR' item1.Amount = '12,345.35' item1.InvoiceRef = '#1234' items.append(item1) for i in xrange(1000): item = Item() item.val1 = 'Item%s Value1' % i item.val2 = 'Item%s Value2' % i item.val3 = 'Item%s Value3' % i item.Currency = 'EUR' item.Amount = '6,666.77' item.InvoiceRef = 'Reference #%04d' % i
from py3o.template import Template t = Template("py3o_example_template_single_cell.odt", "py3o_example_output_single_cell.odt") t.set_image_path('logo', 'images/new_logo.png') class Item(object): pass items = list() item1 = Item() item1.val1 = 'Item1 Value1' item1.val2 = 'Item1 Value2' item1.val3 = 'Item1 Value3' item1.Currency = 'EUR' item1.Amount = '12,345.35' item1.InvoiceRef = '#1234' items.append(item1) for i in xrange(1000): item = Item() item.val1 = 'Item%s Value1' % i item.val2 = 'Item%s Value2' % i item.val3 = 'Item%s Value3' % i item.Currency = 'EUR' item.Amount = '6,666.77' item.InvoiceRef = 'Reference #%04d' % i items.append(item)
from py3o.template import Template t = Template("py3o_example_template.odt", "py3o_example_output.odt") t.set_image_path('staticimage.logo', 'images/new_logo.png') class Item(object): pass items = list() item1 = Item() item1.val1 = 'Item1 Value1' item1.image = open('images/dot1.png', 'rb').read() item1.val3 = 'Item1 Value3' item1.Currency = 'EUR' item1.Amount = '12345.35' item1.InvoiceRef = '#1234' items.append(item1) # if you are using python 2.x you should use xrange for i in range(124): item = Item() item.val1 = 'Item%s Value1' % i item.image = open('images/dot%s.png' % (i % 2), 'rb').read() item.val3 = 'Item%s Value3' % i item.Currency = 'EUR' item.Amount = '6666.77' item.InvoiceRef = 'Reference #%04d' % i items.append(item)