various small bugfixes; add cards list view

This commit is contained in:
badblocks 2025-03-28 15:50:20 -07:00
parent d5f8345581
commit 05a279fa3a
10 changed files with 264 additions and 23 deletions

View file

@ -103,7 +103,11 @@ def render_trade_offer_png(context, offer, show_friend_code=False):
image_width = base_width
image_height = int(round(image_width / aspect_ratio))
base_url = context.get('request').build_absolute_uri('/')
request = context.get("request")
if request.get_host().startswith("localhost"):
base_url = "http://{0}".format(request.get_host())
else:
base_url = "https://{0}".format(request.get_host())
return {
'offer_pk': offer.pk,

View file

@ -618,26 +618,60 @@ class TradeOfferPNGView(View):
html = render_to_string("templatetags/trade_offer_png.html", tag_context)
# If there's a query parameter 'debug' set to true, render the HTML to the response.
if request.GET.get('debug'):
return HttpResponse(html, content_type="text/html")
css = render_to_string("static/css/dist/styles.css")
# Launch Playwright to render the HTML and capture a screenshot.
with sync_playwright() as p:
print("Launching browser")
browser = p.chromium.launch(
headless=True,
args=[
"--disable-gpu",
"--no-sandbox",
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--disable-gpu'
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--disable-gpu",
#"--single-process",
"--no-zygote",
"--disable-audio-output",
#"--disable-software-rasterizer",
"--disable-webgl",
#"--disable-web-security",
#"--disable-features=LazyFrameLoading",
#"--disable-features=IsolateOrigins",
#"--disable-background-networking",
"--no-first-run",
]
)
print("Launched browser, creating context")
context_browser = browser.new_context(viewport={"width": image_width, "height": image_height})
print("Created context, creating page")
page = context_browser.new_page()
page.set_content(html, wait_until="networkidle")
print("Created page, setting content")
# Listen for all console logs, errors, and warnings
page.on("console", lambda msg: print(f"Console {msg.type}: {msg.text}"))
page.on("pageerror", lambda err: print(f"Page error: {err}"))
# Listen specifically for failed resource loads
page.on("requestfailed", lambda request: print(f"Failed to load: {request.url} - {request.failure.error_text}"))
# # Instead of using a link tag, let's inject the CSS directly
# css = render_to_string("static/css/dist/styles.css")
# page.add_style_tag(content=css)
page.set_content(html, wait_until="domcontentloaded")
print("Set content, waiting for element")
element = page.wait_for_selector(".trade-offer-card-screenshot")
print("Found element, capturing screenshot")
screenshot_bytes = element.screenshot(type="png", omit_background=True)
print("Captured screenshot, closing browser")
browser.close()
print("Closed browser, returning screenshot")
return HttpResponse(screenshot_bytes, content_type="image/png")