-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
90 lines (68 loc) · 2.26 KB
/
app.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
import re
import logging
import sys
from flask import Flask, request, render_template
from flask_table import Table, Col
from client import Suma
app = Flask(__name__)
LOGGER = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# regex pattern to match a Suma code (must be entire string)
# ^ start of string
# [a-zA-Z]{2} exactly 2 of latin alphabet, either case
# [0-9]{3} exactly 3 arabic numerals
# $ end of string
CODE_REGEX = r"^[a-zA-Z]{2}[0-9]{3}$"
# every Suma code is 5 characters long
CODE_LEN = 5
@app.route('/')
def input_form():
return render_template('form.html')
@app.route('/', methods=['POST'])
def convert():
"""
Get pricing information for given Suma codes.
"""
text = request.form['text']
# no codes to parse
if len(text) == 0:
return (
"Please enter the Suma product codes you want the price "
"information for in the form like so: AB123 JK456 YZ789"
)
# split form input by whitespace into codes
codes = text.split()
# check codes all match the regex, if not, store them in bad_codes
bad_codes: list = [
code for code in codes if not re.match(CODE_REGEX, code)
]
if bad_codes:
bad_codes_str = ", ".join(bad_codes)
return (
"Some of these codes don't look in the right format to me: "
f"[{bad_codes_str}] aren't valid :("
)
# instantiate suma client object (establishes session)
suma_client = Suma()
# fetch the data and store in list of dicts
results: list = []
for code in codes:
data = suma_client.get_product(code)
data['code'] = code
# insert quantity column to match spreadsheet schema
data['quantity'] = ''
results.append(data)
# Declare your table
class ItemTable(Table):
code = Col('Code')
name = Col('Name')
price = Col('Price ex-VAT (£)')
quantity = Col('Quantity')
currentTax = Col('VAT rate (%)')
# Populate the html table
table = ItemTable(results)
# Print the html
return table.__html__()
if __name__ == "__main__":
# Only for debugging while developing
app.run(host="0.0.0.0", debug=True, port=80)