forked from dataculturegroup/box-designer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
92 lines (76 loc) · 3.41 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import logging
import os
import datetime
from flask import Flask, render_template, request, send_from_directory
import boxmaker
import boxmaker.ads
app = Flask(__name__)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BOX_TMP_DIR = os.path.join(BASE_DIR, 'tmp', 'boxes')
# setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("---------------------------------------------------------------------------")
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
validation_errors = _validate_box_params()
if len(validation_errors) > 0:
error_str = ' '.join(validation_errors)
logger.debug("Errors: "+error_str)
return render_template('home.html', error=error_str)
else:
file_type = request.form['file_type']
notched_top = request.form['notched_top'] == '1'
box_name = _box_name(file_type)
logger.debug('Creating box '+box_name+"...")
# convert it to millimeters
measurements = ['width', 'height', 'depth', 'material_thickness', 'cut_width', 'notch_length']
conversion = 1.0
if request.form['units'] == 'in':
conversion = 25.4
elif request.form['units'] == 'cm':
conversion = 10.0
params = {}
for key in measurements:
params[key] = float(request.form[key])*conversion
# and add bounding box option
params['bounding_box'] = True if 'bounding_box' in request.form else False
# now render it
logger.info(request.remote_addr + " - " + box_name)
_render_box(box_name, file_type, params, notched_top)
return send_from_directory(BOX_TMP_DIR, box_name, as_attachment=True)
else:
return render_template("home.html",
boxmaker_version=boxmaker.APP_VERSION,
matomo_tracker_url=os.getenv('MATOMO_TRACKER_URL', None),
matomo_site_id=os.getenv('MATOMO_SITE_ID', None),
ads=boxmaker.ads.visible_ads(),
)
def _render_box(file_name, file_type, params, notched_top):
out_file_path = os.path.join(BOX_TMP_DIR, file_name)
boxmaker.render(out_file_path,
params['width'], params['height'], params['depth'],
params['material_thickness'], params['cut_width'], params['notch_length'],
params['bounding_box'], file_type, not notched_top
)
def _box_name(file_type):
return 'box-'+datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")+'.'+file_type
def _validate_box_params():
errors = []
errors += _numeric_errors(request.form['width'], 'Width')
errors += _numeric_errors(request.form['height'], 'Height')
errors += _numeric_errors(request.form['depth'], 'Depth')
errors += _numeric_errors(request.form['material_thickness'], 'Material thickness')
errors += _numeric_errors(request.form['cut_width'], 'Cut width')
errors += _numeric_errors(request.form['notch_length'], 'Notch length')
return errors
def _numeric_errors(string, name):
try:
float(string)
return []
except ValueError:
return [name + " must be a number!"]
if __name__ == "__main__":
app.debug = False
app.run()