asdf
All checks were successful
Deploy App to Docker / deploy (push) Successful in 31s

This commit is contained in:
2025-04-08 13:26:12 -04:00
parent 1bb842ea3f
commit 029c28166e
2 changed files with 44 additions and 21 deletions

View File

@@ -12,6 +12,7 @@ from weasyprint import HTML
import json
import time
import re
import csv
logger = logging.getLogger(__name__)
@@ -271,30 +272,52 @@ class TCGPlayerAPIService:
return output_filename
def get_address_labels_csv(self, order_ids: list[str]):
url = f"{self.config.ORDER_BASE_URL}/shipping/export{self.config.API_VERSION}"
payload = {
"orderNumbers": order_ids,
"timezoneOffset": -4
}
response = self.requests_util.send_request(url, method='POST', cookies=self.cookies, json=payload)
if response:
# get filename from response headers
header = response.headers.get('Content-Disposition', '')
match = re.search(r'filename="?([^";]+)"?', header)
filename = match.group(1) if match else f'shipping{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv'
output_filename = f'/app/tmp/{filename}'
# save file to disk
with open(output_filename, 'wb') as f:
f.write(response.content)
return output_filename
def get_address_labels_pdf(self, order_ids: list[str]):
shipping_csv_filename = self.get_address_labels_csv(order_ids)
with open(shipping_csv_filename, 'r') as f:
reader = csv.DictReader(f)
orders = {}
for row in reader:
order_id = row.pop('Order #')
orders[order_id] = row
labels_html = []
for order_id in order_ids:
order = self.get_order(order_id)
if order:
try:
# Extract relevant information from the order
order_info = {
"recipient_name": order['shippingAddress']['recipientName'],
"address_line1": order['shippingAddress']['addressOne'],
"address_line2": order['shippingAddress'].get('addressTwo', ''),
"city": order['shippingAddress']['city'],
"state": order['shippingAddress']['territory'],
"zip_code": order['shippingAddress']['postalCode'],
"return_address_path": self.return_address_png
}
for order in orders:
if float(order['Value of Products']) >49.99:
continue
# Extract relevant information from the order
order_info = {
"recipient_name": order['FirstName'] + ' ' + order['LastName'],
"address_line1": order['Address1'],
"address_line2": order['Address2'] if 'Address2' in order else '',
"city": order['City'],
"state": order['State'],
"zip_code": order['PostalCode'],
"return_address_path": self.return_address_png
}
# Render the label HTML using the template
labels_html.append(self.address_label_template.render(order_info))
except KeyError as e:
print(f"Missing field in order {order_id}: {e}")
continue
# Render the label HTML using the template
labels_html.append(self.address_label_template.render(order_info))
if labels_html:
# Combine the rendered labels into one HTML string