Skip to content

Commit

Permalink
Added pdf renderer, while making the file writing class generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfiry77 committed Oct 27, 2023
1 parent c7f08b3 commit 62a64e8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
13 changes: 9 additions & 4 deletions CouponsFormatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
HTML_ROW_TEMPLATE = """
<tr>
<td>{order_date}</td>
<td>{barcode_number}</td>
<td><img src="data:image/png;base64,{image_data}"></td>
<td>{amount}</td>
<td>{barcode_number}</td>
<td><img src="data:image/png;base64,{image_data}"></td>
</tr>
"""
HTML_PAGE_TEMPLATE = """
Expand All @@ -18,7 +18,6 @@
#barcodes {{
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}}
img {{
opacity: 1
Expand All @@ -45,7 +44,12 @@
<body>
<h1> {restaurantName} </h1>
<table id="barcodes">
<tr> <th>Order date</th> <th>Barcode number</th> <th>Barcode image</th> <th>Amount</th> </tr>
<tr>
<th width="70px">Order date</th>
<th width="30px">Amount</th>
<th width="50px">Barcode number</th>
<th width="300px">Barcode image</th>
</tr>
{output_table}
</table>
</body>
Expand Down Expand Up @@ -83,3 +87,4 @@ def write_to_files(self):
filename = f"output/{date.today().strftime('%y-%m-%d')}_{r['vendorName']}.html"
with open(filename, 'w', encoding='utf-8') as file:
file.write(self.format_orders(r['orders'], r['restaurantName']))
HTML(string=open(filename, 'rb').read()).write_pdf(filename + ".pdf")
31 changes: 31 additions & 0 deletions ReportWriter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from abc import ABC, abstractmethod
from datetime import date

class ReportWriter(ABC):
def __init__(self):
super().__init__()

@abstractmethod
def write(self, buffer, base_name):
pass


class ReportWriterHtml(ReportWriter):

def write(self, buffer, base_name):
filename = f"output/{date.today().strftime('%y-%m-%d')}_{base_name}.html"
with open(filename, 'w', encoding='utf-8') as file:
file.write(buffer)


class ReportWriterPdf(ReportWriter):

def __init__(self):
try:
from weasyprint import HTML
except ImportError:
raise ImportError('Failed to import weasyprint')
super.__init__()

def write(self, buffer, base_name):
HTML(string=buffer).write_pdf(f"output/{date.today().strftime('%y-%m-%d')}_{base_name}.pdf")
13 changes: 12 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
from TenbisLogic import *
from CouponsFormatter import *
from ReportWriter import *
from sys import platform
import sys


def main(argv):
ten_bis = Tenbis()

budget_available = ten_bis.is_budget_available()
print('budget available=', budget_available)
if budget_available:
ten_bis.buy_coupon(40)
coupons = ten_bis.get_unused_coupons()

formatter = CouponFormatter(coupons)
formatter.write_to_files()
if platform == "win32":
writer = ReportWriterHtml()
else:
writer = ReportWriterPdf()

for r in coupons.values():
formatted_string = formatter.format_orders(r['orders'], r['restaurantName'])
writer.write(formatted_string, r['vendorName'])


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ charset-normalizer==3.3.0
idna==3.4
requests==2.31.0
urllib3==2.0.6
python-dateutil~=2.8.2
python-dateutil~=2.8.2
weasyprint~=60.1

0 comments on commit 62a64e8

Please sign in to comment.