def pdf_file(invoname, download): if not invoname: return redirect('/home') fdao = InvoiceDAO() if not fdao.exist(fdao.where('name', invoname)): return redirect('/home') invoice = fdao.get(fdao.where('name', invoname))[0] def date(dat): return '/'.join(reversed(dat.split('/'))) prestamonth = '/'.join(invoice.date_sent.split('/')[1:]) client = Client() cdao = ClientDAO() client = cdao.get(cdao.where('id', invoice.id_client))[0] total = float(invoice.total) if invoice.tax: total *= (1 + (TAX / 100)) profile = get_profile_from_session() adao = InsuranceDAO() insurance = adao.get( [adao.where('id_profile', profile.id), adao.where('sel', 'True')]) html_render = render_template('template/pdf_template.html', profile=profile, prestamonth=prestamonth, date=date, invoice=invoice, convert_date=convert_date, Page_title='Facture', client=client, total=total, insurance=insurance, len=len, url="invoice") pdf = pdfkit.from_string(html_render, False) response = make_response(pdf) response.headers['Content-Type'] = 'application/pdf' if download: response.headers[ 'Content-Disposition'] = 'attachment; filename={}_{}.pdf'.format( _('Invoice'), invoname) else: response.headers[ 'Content-Disposition'] = 'inline; filename={}_{}.pdf'.format( _('Invoice'), invoname) logging.info('Invoice create pdf download: %s', str(download)) return response
class InvoiceTestCase(unittest.TestCase): def setUp(self): self.fdao = InvoiceDAO(DB_PATH) self.invoice = Invoice() self.invoice.date_expiry = '01/01/2020' self.invoice.date_sent = '01/01/2020' self.invoice.days = 10 self.invoice.max_delay = '01/01/2020' self.invoice.id_client = 1 self.invoice.id_profile = 1 self.invoice.name = 'FACTURE TEST 1' self.invoice.sold = False self.invoice.project = 'TEST 1 2 3' self.invoice.day_rate = 5000 self.invoice.total = '50000' self.invoice.tax = False def test_invoice_obj(self): self.assertIsNotNone(self.fdao, msg="Impossible to instance InvoiceDAO") self.assertTrue(self.fdao.create_table(), msg="Impossible to create invoice table in db") self.assertFalse(self.fdao.exist(self.invoice), msg="Impossible to check Invoice exist in db") self.assertTrue(self.fdao.insert(self.invoice), msg="Impossible to insert Invoice in db") list_invoice = self.fdao.get(self.fdao.where('name', self.invoice.name)) self.assertIsInstance(list_invoice, list, msg="dao.get function not return a list") self.assertGreater(len(list_invoice), 0, msg="No Invoice in db") invce = list_invoice[0] self.assertIsInstance( invce, Invoice, msg="1st element of dao.get is not a Invoice obj") self.assertEqual( self.invoice.date_expiry, invce.date_expiry, msg="Invoice get, has no same attribute 'date_expiry'") self.assertEqual(self.invoice.date_sent, invce.date_sent, msg="Invoice get, has no same attribute 'date_sent'") self.assertEqual(self.invoice.days, invce.days, msg="Invoice get, has no same attribute 'days'") self.assertEqual(self.invoice.max_delay, invce.max_delay, msg="Invoice get, has no same attribute 'max_delay'") self.assertEqual(self.invoice.id_client, invce.id_client, msg="Invoice get, has no same attribute 'id_client'") self.assertEqual(self.invoice.id_profile, invce.id_profile, msg="Invoice get, has no same attribute 'id_profile'") self.assertEqual(self.invoice.name, invce.name, msg="Invoice get, has no same attribute 'name'") self.assertEqual(self.invoice.sold, invce.sold, msg="Invoice get, has no same attribute 'sold'") self.assertEqual(self.invoice.project, invce.project, msg="Invoice get, has no same attribute 'project'") self.assertEqual(self.invoice.day_rate, invce.day_rate, msg="Invoice get, has no same attribute 'day_rate'") self.assertEqual(self.invoice.total, invce.total, msg="Invoice get, has no same attribute 'total'") self.assertEqual(self.invoice.tax, invce.tax, msg="Invoice get, has no same attribute 'tax'") self.assertTrue(hasattr(invce, 'id'), msg="Invoice get, has no attribute 'id'") invce.sold = True self.assertTrue(self.fdao.update(invce)) list_invoice2 = self.fdao.get(self.fdao.where('name', invce.name)) self.assertIsInstance(list_invoice2, list, msg="dao.get function not return a list 2") self.assertGreater(len(list_invoice2), 0, msg="No Invoice 2 in db") invce2 = list_invoice2[0] self.assertEqual( invce2.date_expiry, invce.date_expiry, msg="Invoice get 2, has no same attribute 'date_expiry'") self.assertEqual( invce2.date_sent, invce.date_sent, msg="Invoice get 2, has no same attribute 'date_sent'") self.assertEqual(invce2.days, invce.days, msg="Invoice get 2, has no same attribute 'days'") self.assertEqual( invce2.max_delay, invce.max_delay, msg="Invoice get 2, has no same attribute 'max_delay'") self.assertEqual( invce2.id_client, invce.id_client, msg="Invoice get 2, has no same attribute 'id_client'") self.assertEqual( invce2.id_profile, invce.id_profile, msg="Invoice get 2, has no same attribute 'id_profile'") self.assertEqual(invce2.name, invce.name, msg="Invoice get 2, has no same attribute 'name'") self.assertEqual(invce2.sold, invce.sold, msg="Invoice get 2, has no same attribute 'sold'") self.assertEqual(invce2.project, invce.project, msg="Invoice get 2, has no same attribute 'project'") self.assertEqual(invce2.day_rate, invce.day_rate, msg="Invoice get 2, has no same attribute 'day_rate'") self.assertEqual(invce2.total, invce.total, msg="Invoice get 2, has no same attribute 'total'") self.assertEqual(invce2.tax, invce.tax, msg="Invoice get 2, has no same attribute 'tax'") self.assertTrue(self.fdao.delete(invce), msg="Impossible to delete invoice from db") self.assertFalse(self.fdao.drop(True, False), msg="Drop table invoice not wanted") self.assertFalse(self.fdao.drop(False, True), msg="Drop table invoice not wanted") self.assertTrue(self.fdao.drop(True, True), msg="Cannot drop table invoice") self.assertFalse(self.fdao.drop(True, True), msg="The table has not deleted before")