Skip to content

Commit

Permalink
latest edits
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarMAshour committed Oct 23, 2022
1 parent b1a93da commit ea027ec
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 112 deletions.
84 changes: 43 additions & 41 deletions coop_challenge/generator/info_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def __init__(self, data):
self.add_qr()
self.add_name()
self.add_attributes()
self.add_article_number()


def build_rect(self):
self.set_fill_color(255, 168, 0)
self.rect(x=self.pdf_w - self.rec_width, y=0, w=self.rec_width, h=self.pdf_h, style='F')
Expand Down Expand Up @@ -69,48 +68,48 @@ def add_footer(self):
self.set_font("Arial", style='', size=12)
self.set_text_color(0)

def get_attr_or_none(self, dict, key):
return dict.get(key, None)

def add_attributes(self):
self.set_text_color(255)
price = str(self.data['allPrices'][0]['sellPrice']) + " CHF"
character = self.data['wineCharacter']
origin = self.data['wineOrigin']
maker = self.data['wineMaker']
enjoyFrom = self.data["enjoyFrom"]
enjoyUntil = self.data["enjoyUntil"]
wineCharacter = self.data.get('wineCharacter', 'NotProvided')
wineOrigin = self.data.get('wineOrigin', 'NotProvided')
wineMaker = self.data.get('wineMaker', 'NotProvided')
enjoyFrom = self.data.get('enjoyFrom', 'NotProvided')
enjoyUntil = self.data.get('enjoyUntil', 'NotProvided')
bottomTemp = self.data["servingTemperature"].split("-")[0]
topTemp = self.data["servingTemperature"].split("-")[1]
grapes = self.data['grapesText']
percent = self.data['alcohol']

text = f"{price}\n\n{character}\n{origin}\nWinzer - {maker} \n\nRebsorte - {grapes}\n{percent}% VOL\n\n"
text += f"Am besten, {enjoyFrom} - {enjoyUntil}\n {bottomTemp} - {topTemp} °C\n\nPrämierung - {self.data['averageRating']}"
text += f"\n\nFlaschenverschluss - {self.data['typeOfSeal']}\n\nAusbauart - {self.data['wineAgeing']}"
grapes = self.data.get('grapesText', 'NotProvided')
percent = self.data.get('alcohol', 'NotProvided')
averageRating = self.data.get('averageRating', 'NotProvided')
typeOfSeal = self.data.get('typeOfSeal', 'NotProvided')
wineAgeing = self.data.get('wineAgeing', 'NotProvided')

text = f"{price}\n\n{wineCharacter}\n{wineOrigin}\nWinzer - {wineMaker} \n\nRebsorte - {grapes}\n{percent}% VOL\n\n"
text += f"Am besten, {enjoyFrom} - {enjoyUntil}\n {bottomTemp} - {topTemp} °C\n\nPrämierung - {averageRating}"
text += f"\n\nFlaschenverschluss - {typeOfSeal}\n\nAusbauart - {wineAgeing}"
self.set_xy(self.pdf_w - self.rec_width + 5, 3 * self.margin)
self.multi_cell(w=60, h=5, txt=text,
align="C")

def add_article_number(self):
self.set_text_color(255)
self.set_font("Arial", style='B', size=14)
self.text(x=self.pdf_w - self.rec_width + self.margin,
y=self.pdf_h - 5, txt=f"Art. {self.data['code']}")
self.set_text_color(0)

def add_name(self):
self.set_xy(self.pdf_w - self.rec_width + self.margin, self.margin)
name = self.data['name_de']
name = self.data.get('name_de', 'NotProvided')
self.set_text_color(255)

vintage = self.data['yearOfVintage']
yearOfVintage = self.data.get('yearOfVintage', 'NotProvided')

self.set_font("Arial", style='B', size=14)
self.set_xy(self.pdf_w - self.rec_width + 5, self.margin)
self.multi_cell(w=60, h=5, txt=name + "\n" + str(vintage), align="C")
self.multi_cell(w=60, h=5, txt=name + "\n" + str(yearOfVintage), align="C")
self.set_font("Arial", style='', size=12)
self.set_text_color(0)

def add_qr(self):
pagelink = "https://www.coop.ch/de/p/" + self.data['baseMaterialNumber']
code = self.data.get('code', 'NotProvided')
pagelink = "https://www.coop.ch/de/p/" + code
img_url = self.generate_qr_link(pagelink)
self.image(img_url, x=self.pdf_w - self.rec_width + 10,
y=self.pdf_h - self.rec_width + 10, w=self.rec_width - 20, h=self.rec_width - 20, type='PNG')
Expand All @@ -121,29 +120,32 @@ def generate_qr_link(content):
return QR_API + content

def build_description(self):
wineName = self.data["name"]
country = self.data["wineOriginCountry"]
yearOfVintage = self.data["yearOfVintage"]
enjoyFrom = self.data["enjoyFrom"]
enjoyUntil = self.data["enjoyUntil"]
bottomTemp = self.data["servingTemperature"].split("-")[0]
topTemp = self.data["servingTemperature"].split("-")[1]
name = self.data.get('name', 'NotProvided')
wineOriginCountry = self.data.get('wineOriginCountry', 'NotProvided')
yearOfVintage = self.data.get('yearOfVintage', 'NotProvided')
enjoyFrom = self.data.get('enjoyFrom', 'NotProvided')
enjoyUntil = self.data.get('enjoyUntil', 'NotProvided')
servingTemperature = self.data.get('servingTemperature', 'NotProvided').split("-")[0]
topTemp = self.data.get('servingTemperature', 'NotProvided').split("-")[1]
pairing = ""
pairing_split = self.data["goesWithText_de"].split(",")
for s in pairing_split[:-1]:
pairing += s
pairing += f", und {pairing_split[-1]}"

self.desc1 = f"{wineName}: der wein aus {country} wurde {yearOfVintage} abgefüllt. Am besten genießt man ihn zwischen {enjoyFrom} und {enjoyUntil}."
self.desc2 = f"Der Wein passt gut zu {pairing}, und wird am besten zwischen {bottomTemp} und {topTemp} °C genossen"
self.tasting_notes = self.data["tastingNotes_de"]
goesWithText_de = self.data.get('goesWithText_de', 'NotProvided')
if goesWithText_de == 'NotProvided':
pairing = "NotProvided"
else:
pairing_split = goesWithText_de.split(",")
for s in pairing_split[:-1]:
pairing += s
pairing += f", und {pairing_split[-1]}"

self.desc1 = f"{name}: der wein aus {wineOriginCountry} wurde {yearOfVintage} abgefüllt. Am besten genießt man ihn zwischen {enjoyFrom} und {enjoyUntil}."
self.desc2 = f"Der Wein passt gut zu {pairing}, und wird am besten zwischen {servingTemperature} und {topTemp} °C genossen"
self.tasting_notes = self.data.get('tastingNotes_de', 'NotProvided')

def save_doc(self, path):
self.output(path)
# self.output()


def tryInfo(product_dict):
doc = InfoDoc(product_dict)
doc.save_doc(pdf_output_file_path)
return pdf_output_file_path
return pdf_output_file_path
80 changes: 40 additions & 40 deletions coop_challenge/generator/label_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@


def tryLabel(product_dict):
product_dict['sellPrice'] = product_dict["allPrices"][0]['sellPrice']
product_dict['wineCharacter'] = product_dict["wineCharacter"].split(",")[0]
del product_dict['allPrices']
generate_html(product_dict)
label_name = create_label()

return label_name


def retrieve_wine_info():
fetcher = Fetcher()
product_dict = fetcher.find_for_labels(article_ids=["1000372018"])[0]
product_dict['sellPrice'] = product_dict["allPrices"][0]['sellPrice']
product_dict['wineCharacter'] = product_dict["wineCharacter"].split(",")[0]
del product_dict['allPrices']
return product_dict


def generate_html(product):
f = open(html_link, "w")

bottle_url = "https://svgsilh.com/png-512/150955.png"
logo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Coop.svg/2000px-Coop.svg.png"
qr_url = generate_qr_link(product['code'])

name = product.get('name', 'NotProvided').strip()
yearOfVintage = product.get('yearOfVintage', 'NotProvided')
wineCharacter = product.get('wineCharacter', 'NotProvided')
wineOrigin = product.get('wineOrigin', 'NotProvided')
grapesText = product.get('grapesText', 'NotProvided')
enjoyFrom = product.get('enjoyFrom', 'NotProvided')
enjoyUntil = product.get('enjoyUntil', 'NotProvided')
alcohol = product.get('alcohol', 'NotProvided')
allPrices = product.get('allPrices', 'NotProvided')
if allPrices == 'NotProvided':
sellPrice = 'NotProvided'
else:
sellPrice = allPrices[0]['sellPrice']

message = f"""<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -48,37 +50,37 @@ def generate_html(product):
<title>Label</title>
</head>
<body>
<table style="font-family: sans-serif; border: 2px solid #FFA400">
<table id="table" style="font-family: sans-serif; border: 2px solid #FFA400">
<tr>
<td style=font-size:20px bgcolor="#FFA400" colspan = "4"><b> &nbsp; {product['name']}</b></td>
<td colspan = "4" id="name"><b> &nbsp; {name}</b></td>
</tr>
<tr>
<td width=80 ROWSPAN="7"><img src="{bottle_url}" height="150"></td>
<td ROWSPAN="7" id="bottle"><img src="{bottle_url}" height="150"></td>
</tr>
<tr>
<td width="80">{product["yearOfVintage"]}</td>
<td width="190">{product["wineCharacter"]}</td>
<td ROWSPAN="5"><img src={qr_url} width="100" align="center"></td>
<td width="100" id="year">{yearOfVintage}</td>
<td width="300">{wineCharacter}</td>
<td ROWSPAN="5"><img src={qr_url} id="qr"></td>
</tr>
<tr>
<td><em>Origin:</em></td>
<td>{product["wineOrigin"]}</td>
<td id="origin"><em>Origin:</em></td>
<td>{wineOrigin}</td>
</tr>
<tr>
<td><em>Grape:</em></td>
<td>{product["grapesText"]}</td>
<td id="grape"><em>Grape:</em></td>
<td>{grapesText}</td>
</tr>
<tr>
<td><em>Maturity:</em></td>
<td>{product["enjoyFrom"]} - {product["enjoyUntil"]}</td>
<td id="maturity"><em>Maturity:</em></td>
<td>{enjoyFrom} - {enjoyUntil}</td>
</tr>
<tr>
<td><em>Alcohol:</em></td>
<td>{product["alcohol"]}%</td>
<td id="alcohol"><em>Alcohol:</em></td>
<td>{alcohol}%</td>
</tr>
<tr>
<td colspan ="2"><img src="{logo_url}" width="100"></td>
<td style=font-size:20px><b>{product["sellPrice"]} CHF</b></td>
<td colspan ="2" id="logo"><img src="{logo_url}" width="100"></td>
<td id="price""><b>{sellPrice} CHF</b></td>
</tr>
</table>
</body>
Expand All @@ -89,17 +91,6 @@ def generate_html(product):


def create_label():
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
print(html_link)
pdfkit.from_file(html_link, label_output_file_path, css=css_link, options={"enable-local-file-access": ""})

return label_output_file_path
Expand All @@ -111,7 +102,16 @@ def generate_qr_link(productId):
return QR_API


def retrieve_wine_info():
fetcher = Fetcher()
product_dict = fetcher.find_for_labels(article_ids=["1000372018"])[0]
product_dict['sellPrice'] = product_dict["allPrices"][0]['sellPrice']
product_dict['wineCharacter'] = product_dict["wineCharacter"].split(",")[0]
del product_dict['allPrices']
return product_dict


if __name__ == "__main__":
product = retrieve_wine_info()
generate_html(product)
create_label()
create_label()
35 changes: 18 additions & 17 deletions coop_challenge/generator/label_template/label_template.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/home/omar/Documents/BaselHack/BaselHack2022/coop_challenge/generator/label_template/style.css">
<link rel="stylesheet" href="/home/andrei/Desktop/BaselHack2022_Team10/coop_challenge/generator/label_template/style.css">
<meta charset="UTF-8">
<title>Label</title>
</head>
<body>
<table style="font-family: sans-serif; border: 2px solid #FFA400">
<table id="table" style="font-family: sans-serif; border: 2px solid #FFA400">
<tr>
<td style=font-size:20px bgcolor="#FFA400" colspan = "4"><b> &nbsp; Château Lynch-Moussas Pauillac AC 5ème Cru Classé</b></td>
<td colspan = "4" id="name"><b> &nbsp; Rioja DOCa Selección Especial Cooperation Wine Cune</b></td>
</tr>
<tr>
<td width=80 ROWSPAN="7"><img src="https://svgsilh.com/png-512/150955.png" height="150"></td>
<td ROWSPAN="7" id="bottle"><img src="https://svgsilh.com/png-512/150955.png" height="150"></td>
</tr>
<tr>
<td width="80">2017</td>
<td width="190"></td>
<td ROWSPAN="5"><img src=https://api.qrserver.com/v1/create-qr-code/?size=210x210&data=https://www.coop.ch/p/4039025008 width="100" align="center"></td>
<td width="100" id="year">2018</td>
<td width="300">Rotwein, mittelschwer/weich</td>
<td ROWSPAN="5"><img src=https://api.qrserver.com/v1/create-qr-code/?size=210x210&data=https://www.coop.ch/p/1013351005 id="qr"></td>
</tr>
<tr>
<td><em>Origin:</em></td>
<td>Bordeaux - Pauillac, Frankreich</td>
<td id="origin"><em>Origin:</em></td>
<td>Rioja, Spanien</td>
</tr>
<tr>
<td><em>Grape:</em></td>
<td>Cabernet Sauvignon, Merlot, Cabernet Franc</td>
<td id="grape"><em>Grape:</em></td>
<td>Grenache, Graciano, Tempranillo, Mazuela</td>
</tr>
<tr>
<td><em>Maturity:</em></td>
<td>2023 - 2030</td>
<td id="maturity"><em>Maturity:</em></td>
<td>2021 - 2023</td>
</tr>
<tr>
<td><em>Alcohol:</em></td>
<td>13.0%</td>
<td id="alcohol"><em>Alcohol:</em></td>
<td>13.5%</td>
</tr>
<tr>
<td colspan ="2"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Coop.svg/2000px-Coop.svg.png" width="100"></td>
<td style=font-size:20px><b>47.5 CHF</b></td>
<td colspan ="2" id="logo"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Coop.svg/2000px-Coop.svg.png" width="100"></td>
<td id="price""><b>15.15 CHF</b></td>
</tr>
</table>
</body>
Expand Down

0 comments on commit ea027ec

Please sign in to comment.