Better handling of concurrent requests

It now supports multiple name tags for different names to be generated
at the same time. Two concurrent requests for the same name will
probably fail.
This commit is contained in:
Simon Bruder 2020-04-29 15:29:35 +02:00
parent 3912d29530
commit 3b5d1a932c
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
2 changed files with 13 additions and 6 deletions

View file

@ -11,7 +11,7 @@ height_plate = 1;
module name_file() { module name_file() {
translate([(width-name_width)/2, (length-name_length)/2, 0]) difference() { translate([(width-name_width)/2, (length-name_length)/2, 0]) difference() {
square([name_width, name_length]); square([name_width, name_length]);
resize([name_width, name_length, 0]) import("static/namensschild.dxf"); resize([name_width, name_length, 0]) import("{{ name }}.dxf");
} }
} }

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from jinja2 import Template
from pyppeteer import launch from pyppeteer import launch
from quart import Quart, request, render_template, url_for, redirect from quart import Quart, request, render_template, url_for, redirect
from subprocess import run from subprocess import run
@ -17,7 +18,7 @@ async def gen_pdf(name):
page = await browser.newPage() page = await browser.newPage()
await page.goto("file://" + os.path.join(os.getcwd(), "namensschild.html")) await page.goto("file://" + os.path.join(os.getcwd(), "namensschild.html"))
await page.evaluate(f"document.querySelector('#name').innerText = '{name}'") await page.evaluate(f"document.querySelector('#name').innerText = '{name}'")
await page.pdf(path="static/namensschild.pdf", width="180px", height="60px") await page.pdf(path=f"static/{name}.pdf", width="180px", height="60px")
await browser.close() await browser.close()
@ -31,7 +32,7 @@ async def generate():
name = request.args["name"] name = request.args["name"]
await gen_pdf(name) await gen_pdf(name)
run_check( run_check(
["inkscape", "-T", "-E", "static/namensschild.eps", "static/namensschild.pdf",] ["inkscape", "-T", "-E", f"static/{name}.eps", f"static/{name}.pdf",]
) )
run_check( run_check(
[ [
@ -42,11 +43,17 @@ async def generate():
"-dt", "-dt",
"-f", "-f",
"dxf: -polyaslines -mm", "dxf: -polyaslines -mm",
"static/namensschild.eps", f"static/{name}.eps",
"static/namensschild.dxf", f"static/{name}.dxf",
] ]
) )
run_check(["openscad", "-o", f"static/{name}.stl", "namensschild.scad"])
with open("namensschild.scad.j2", "r") as f:
template = Template(f.read())
with open(f"static/{name}.scad", "w") as f:
f.write(template.render(name=name))
run_check(["openscad", "-o", f"static/{name}.stl", f"static/{name}.scad"])
return redirect(url_for("static", filename=f"{name}.stl"), code=302) return redirect(url_for("static", filename=f"{name}.stl"), code=302)