Skip to content

Commit 46de0c0

Browse files
authored
Merge pull request #18 from vshn/async-and-redirect
async printing and redirect after submit
2 parents ef7b0a0 + 9bf1d86 commit 46de0c0

File tree

7 files changed

+172
-109
lines changed

7 files changed

+172
-109
lines changed

contactform/app.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import threading
23
from flask import (
34
Flask,
45
render_template,
@@ -18,7 +19,6 @@
1819
Configuration,
1920
ServerConfiguration,
2021
PrinterConfiguration,
21-
Font,
2222
LabelConfiguration,
2323
WebsiteConfiguration,
2424
)
@@ -151,29 +151,36 @@ def index():
151151
except Exception as e:
152152
logging.error(f"Couldn't create Lead in Odoo: {e}")
153153

154+
# Extract necessary form data
155+
name_data = form.name.data
156+
company_data = form.company.data
157+
email_data = form.email.data
158+
phone_data = form.phone.data
159+
154160
if config.PRINT_APPUIO_VOUCHER:
155-
print_voucher(
156-
form=form,
157-
voucher_code=voucher_code,
158-
config=config,
159-
printer_config=printer_config,
160-
)
161+
threading.Thread(
162+
target=print_voucher,
163+
args=(
164+
name_data,
165+
company_data,
166+
email_data,
167+
phone_data,
168+
voucher_code,
169+
config,
170+
printer_config,
171+
),
172+
).start()
161173

162174
if config.PRINT_RAFFLE_TICKET:
163-
print_raffle(
164-
form=form,
165-
voucher_code=voucher_code,
166-
config=config,
167-
printer_config=printer_config,
168-
)
175+
threading.Thread(
176+
target=print_raffle,
177+
args=(name_data, voucher_code, config, printer_config),
178+
).start()
169179

170180
flash("Thanks for submitting", "success")
171-
return redirect(url_for("index"))
172-
173-
return render_template(
174-
"form.html",
175-
form=form,
176-
)
181+
return render_template("success.html")
182+
else:
183+
return render_template("form.html", form=form)
177184

178185

179186
@app.route("/config", methods=["GET", "POST"])

contactform/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def save_config(config):
104104
def setup_logging(log_level):
105105
logging.basicConfig(
106106
level=logging.getLevelName(log_level),
107-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
107+
format="%(asctime)s [%(threadName)s] %(levelname)s: %(message)s",
108108
datefmt="%Y-%m-%d %H:%M:%S",
109109
)
110110

contactform/label_raffle.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2-
from flask import flash
3-
from wtforms.fields import *
2+
import uuid
3+
import os
44
from html2image import Html2Image
55
from brother_ql_web.labels import (
66
LabelParameters,
@@ -10,8 +10,9 @@
1010
from brother_ql.backends.network import BrotherQLBackendNetwork
1111

1212

13-
def print_raffle(form, voucher_code, config, printer_config):
14-
label_filename = "label_raffle.png"
13+
def print_raffle(name_data, voucher_code, config, printer_config):
14+
unique_id = uuid.uuid4().hex
15+
label_filename = f"label_raffle_{unique_id}.png"
1516

1617
label_css = """
1718
body, html {
@@ -35,47 +36,61 @@ def print_raffle(form, voucher_code, config, printer_config):
3536
"""
3637
label_html = f"""\
3738
<div>
38-
<h1>{form.name.data}</h1>
39+
<h1>{name_data}</h1>
3940
<p class="big">{config.LABEL_HEADER}</p>
4041
<p class="small">{voucher_code}</p>
4142
</div>
4243
"""
4344

44-
hti = Html2Image(
45-
size=(590, 500),
46-
custom_flags=[
47-
"--default-background-color=FFFFFF",
48-
"--hide-scrollbars",
49-
],
50-
)
51-
hti.screenshot(
52-
html_str=label_html,
53-
css_str=label_css,
54-
save_as=label_filename,
55-
)
45+
try:
46+
# Generate image from HTML and CSS
47+
hti = Html2Image(
48+
size=(590, 500),
49+
custom_flags=[
50+
"--default-background-color=FFFFFF",
51+
"--hide-scrollbars",
52+
],
53+
)
54+
hti.screenshot(
55+
html_str=label_html,
56+
css_str=label_css,
57+
save_as=label_filename,
58+
)
5659

57-
label_image = open(label_filename, "rb")
60+
with open(label_filename, "rb") as label_image:
61+
parameters = LabelParameters(
62+
configuration=printer_config,
63+
image=label_image.read(),
64+
label_size="54",
65+
)
5866

59-
parameters = LabelParameters(
60-
configuration=printer_config,
61-
image=label_image.read(),
62-
label_size="54",
63-
)
67+
logging.info(f"Printing raffle label for {name_data}")
68+
preview_filename = (
69+
f"print-preview-raffle_{unique_id}.png"
70+
if config.LOG_LEVEL == "DEBUG"
71+
else None
72+
)
73+
qlr = generate_label(
74+
parameters=parameters,
75+
configuration=printer_config,
76+
save_image_to=preview_filename,
77+
)
6478

65-
logging.info(f"Printing raffle label for {form.name.data}")
66-
qlr = generate_label(
67-
parameters=parameters,
68-
configuration=printer_config,
69-
save_image_to=(
70-
"print-preview-raffle.png" if config.LOG_LEVEL == "DEBUG" else None
71-
),
72-
)
73-
try:
7479
print_label(
7580
parameters=parameters,
7681
qlr=qlr,
7782
configuration=printer_config,
7883
backend_class=BrotherQLBackendNetwork,
7984
)
8085
except Exception as e:
81-
flash(f"Printing of raffle ticket failed: {e}", "error")
86+
logging.error(f"Printing of raffle ticket failed for {name_data}: {e}")
87+
finally:
88+
# Clean up temporary files
89+
if os.path.exists(label_filename):
90+
os.remove(label_filename)
91+
if (
92+
config.LOG_LEVEL == "DEBUG"
93+
and preview_filename
94+
and os.path.exists(preview_filename)
95+
):
96+
os.remove(preview_filename)

contactform/label_voucher.py

Lines changed: 80 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import segno
22
import urllib
33
import logging
4+
import uuid
5+
import os
46

5-
6-
from flask import flash
7-
from wtforms.fields import *
87
from html2image import Html2Image
98
from brother_ql_web.labels import (
109
LabelParameters,
@@ -14,9 +13,18 @@
1413
from brother_ql.backends.network import BrotherQLBackendNetwork
1514

1615

17-
def print_voucher(form, voucher_code, config, printer_config):
18-
label_filename = "label_voucher.png"
19-
qr_code_filename = "appuio_voucher_qr.png"
16+
def print_voucher(
17+
name_data,
18+
company_data,
19+
email_data,
20+
phone_data,
21+
voucher_code,
22+
config,
23+
printer_config,
24+
):
25+
unique_id = uuid.uuid4().hex
26+
label_filename = f"label_voucher_{unique_id}.png"
27+
qr_code_filename = f"appuio_voucher_qr_{unique_id}.png"
2028

2129
label_css = """
2230
body, html {
@@ -41,7 +49,7 @@ def print_voucher(form, voucher_code, config, printer_config):
4149
label_html = f"""\
4250
<div>
4351
<p><img src="appuio-bw.png" class="logo"></p>
44-
<p class="text">Hi {form.name.data}<p>
52+
<p class="text">Hi {name_data}<p>
4553
<p class="text">Your personal voucher code to try out APPUiO:</p>
4654
<p class="text"><strong>{voucher_code}</strong></p>
4755
<p class="text_small">Register here: {config.APPUIO_SIGNUP_URL}</p>
@@ -51,60 +59,78 @@ def print_voucher(form, voucher_code, config, printer_config):
5159

5260
registration_url_parameters = (
5361
f"?voucher={voucher_code}"
54-
f"&name={urllib.parse.quote(form.name.data)}"
55-
f"&company={urllib.parse.quote(form.company.data)}"
56-
f"&email={urllib.parse.quote(form.email.data)}"
57-
f"&phone={urllib.parse.quote(form.phone.data)}"
62+
f"&name={urllib.parse.quote(name_data)}"
63+
f"&company={urllib.parse.quote(company_data)}"
64+
f"&email={urllib.parse.quote(email_data)}"
65+
f"&phone={urllib.parse.quote(phone_data)}"
5866
f"&utm_campaign={urllib.parse.quote(config.CAMPAIGN_NAME)}"
5967
f"&utm_source=Voucher"
6068
)
6169
signup_url = f"{config.APPUIO_SIGNUP_URL}{registration_url_parameters}"
6270
logging.debug(f"URL: {signup_url}")
63-
qrcode = segno.make_qr(signup_url)
64-
qrcode.save(
65-
qr_code_filename,
66-
scale=5,
67-
)
6871

69-
hti = Html2Image(
70-
size=(590, 1200),
71-
custom_flags=[
72-
"--default-background-color=FFFFFF",
73-
"--hide-scrollbars",
74-
],
75-
)
76-
hti.load_file(config.APPUIO_LOGO_PATH)
77-
hti.load_file(qr_code_filename)
78-
hti.browser.print_command = True if config.LOG_LEVEL == "DEBUG" else False
79-
hti.screenshot(
80-
html_str=label_html,
81-
css_str=label_css,
82-
save_as=label_filename,
83-
)
72+
try:
73+
# Generate QR code
74+
qrcode = segno.make_qr(signup_url)
75+
qrcode.save(
76+
qr_code_filename,
77+
scale=5,
78+
)
79+
80+
hti = Html2Image(
81+
size=(590, 1200),
82+
custom_flags=[
83+
"--default-background-color=FFFFFF",
84+
"--hide-scrollbars",
85+
],
86+
)
87+
hti.load_file(config.APPUIO_LOGO_PATH)
88+
hti.load_file(qr_code_filename)
89+
hti.browser.print_command = True if config.LOG_LEVEL == "DEBUG" else False
90+
hti.screenshot(
91+
html_str=label_html,
92+
css_str=label_css,
93+
save_as=label_filename,
94+
)
8495

85-
label_image = open(label_filename, "rb")
96+
with open(label_filename, "rb") as label_image:
97+
parameters = LabelParameters(
98+
configuration=printer_config,
99+
image=label_image.read(),
100+
label_size="54",
101+
high_quality=True,
102+
)
86103

87-
parameters = LabelParameters(
88-
configuration=printer_config,
89-
image=label_image.read(),
90-
label_size="54",
91-
high_quality=True,
92-
)
104+
logging.info(f"Printing voucher label for {name_data}")
105+
preview_filename = (
106+
f"print-preview-voucher_{unique_id}.png"
107+
if config.LOG_LEVEL == "DEBUG"
108+
else None
109+
)
93110

94-
logging.info(f"Printing voucher label for {form.name.data}")
95-
qlr = generate_label(
96-
parameters=parameters,
97-
configuration=printer_config,
98-
save_image_to=(
99-
"print-preview-voucher.png" if config.LOG_LEVEL == "DEBUG" else None
100-
),
101-
)
102-
try:
103-
print_label(
104-
parameters=parameters,
105-
qlr=qlr,
106-
configuration=printer_config,
107-
backend_class=BrotherQLBackendNetwork,
108-
)
111+
qlr = generate_label(
112+
parameters=parameters,
113+
configuration=printer_config,
114+
save_image_to=preview_filename,
115+
)
116+
117+
print_label(
118+
parameters=parameters,
119+
qlr=qlr,
120+
configuration=printer_config,
121+
backend_class=BrotherQLBackendNetwork,
122+
)
109123
except Exception as e:
110-
flash(f"Printing of voucher failed: {e}", "error")
124+
logging.error(f"Printing of voucher failed for {name_data}: {e}")
125+
finally:
126+
# Clean up temporary files
127+
if os.path.exists(label_filename):
128+
os.remove(label_filename)
129+
if os.path.exists(qr_code_filename):
130+
os.remove(qr_code_filename)
131+
if (
132+
config.LOG_LEVEL == "DEBUG"
133+
and preview_filename
134+
and os.path.exists(preview_filename)
135+
):
136+
os.remove(preview_filename)

contactform/templates/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
<html lang="en">
44

55
<head>
6+
{% block head %}
67
<meta charset="utf-8">
78
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
89
<title>VSHN Conference Booth Hit</title>
910
<link rel="icon" href="{{ url_for('static', filename='favicon.png') }}">
1011
{{ bootstrap.load_css() }}
1112
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
13+
{% endblock %}
1214
</head>
1315

1416
<body class="bg-image">

contactform/templates/success.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends 'base.html' %}
2+
3+
{% block head %}
4+
{{ super() }}
5+
<meta http-equiv="refresh" content="2;url=https://vshn.ch">
6+
{% endblock %}
7+
8+
{% block content %}
9+
<p>You will be redirected to <a href="https://vshn.ch">vshn.ch</a> shortly.</p>
10+
{% endblock %}

0 commit comments

Comments
 (0)