diff --git a/abrechenbarkeit.lua b/abrechenbarkeit.lua index 4eb26a8..c209415 100755 --- a/abrechenbarkeit.lua +++ b/abrechenbarkeit.lua @@ -128,9 +128,9 @@ end local function format_amount(amount, tag, classes) local s = format("{+price.amount}", { - sign = amount >= 0 and "+" or "-", - amount = string.format("%.2f", math.abs(amount / 100)), - unit = config.unit or "€" + sign = amount >= 0 and "+" or "-", + amount = string.format("%.2f", math.abs(amount / 100)), + unit = config.unit or "€" }) if tag == nil then return s end return format( @@ -223,13 +223,13 @@ local function respond(status, title, body) print("") end -local function error_box(message) - return string.format([[

Error: %s

]], escape(message)) +local function error_box(message, params) + return string.format([[

Error: %s

]], escape(format(message, params))) end local function respond_error(message) respond(400, "Error", function() - print(error_box(message)) + print(error_box("{!x}", { x = message })) end) end @@ -349,25 +349,25 @@ local function r_transaction_post() end end if not exists then - return error_box("unknown product") + return error_box("{+error.unknown_product}") end end user_src = user_src or "@Potential" if amount == nil then - return error_box("amount invalid") + return error_box("{+error.invalid_amount}") end if comment == nil or comment:match(matchers_global.comment_opt) == nil then - return error_box("comment invalid") + return error_box("{+error.invalid_comment}") end if user_src == nil or user_src:match(matchers_global.user) == nil then - return error_box("source user invalid") + return error_box("{+error.invalid_user_src}") end if user_dst == nil or user_dst:match(matchers_global.user) == nil then - return error_box("destination user invalid") + return error_box("{+error.invalid_user_dst}") end local log = io.open("log", "a+") if log == nil then - return error_box("failed to open log") + return error_box("{+error.open_log}") end local time = os.time() log:write(string.format("%d,%s,%s,%d,%s,%s,%s\n", @@ -393,7 +393,8 @@ local function r_user(username) return respond(200, string.format("Abrechenbarheit: %s", username), function() local is_special = username:sub(1, 1) == "@" local username_display = username:gsub("@", "") - print(format(is_special and "

{!username}

" or "

{!username}

", { username = username_display })) + print(format(is_special and "

{!username}

" or "

{!username}

", + { username = username_display })) local balance = balances()[username] local last_txn = last_txns()[username] local new_user = balance == nil @@ -563,7 +564,7 @@ end local function r_create_user() local username = query.create_user if username:match(matchers_global.user) == nil then - return respond_error("invalid username " .. username) + return respond_error(format("{+error.invalid_user}")) end return redirect(string.format("/%s", urlencode(username))) end @@ -572,12 +573,12 @@ local function r_products_post() local data = form_data() local barcode = data.barcode if barcode == nil or barcode:match("^[%w_-]*$") == nil then - return error_box("barcode invalid") + return error_box("{+error.invalid_barcode}") end if data.delete then local new_products = io.open("products.new", "w+") if new_products == nil then - return error_box("failed to open new products") + return error_box("{+error.open_new_products}") end for a_barcode, price, user, name in read_products() do if barcode ~= a_barcode then @@ -592,17 +593,17 @@ local function r_products_post() local name = data.name local user = data.user if price == nil then - return error_box("price invalid") + return error_box("{+error.invalid_price}") end if name == nil or name:match(matchers_global.name) == nil then - return error_box("name invalid") + return error_box("{+error.invalid_price}") end if user == nil or user:match(matchers_global.user) == nil then - return error_box("user invalid") + return error_box("{+error.invalid_user}") end local products = io.open("products", "a+") if products == nil then - return error_box("failed to open products") + return error_box("{+error.open_products}") end products:write(string.format("%s,%d,%s,%s\n", barcode, price, user, name)) products:flush() @@ -688,7 +689,7 @@ end local function extract_username() if path == nil then - return respond_error("no path") + return respond_error(format("{+error.no_path}")) end local username = urldecode(path:sub(2)) if username == nil or username:match(matchers_global.user) == nil then @@ -716,7 +717,7 @@ if path == "/" then else local username = extract_username() if username == nil then - return respond_error("username invalid") + return respond_error(format("{+error.invalid_user}")) elseif query.log then return r_log(username) else diff --git a/locale/en.ini b/locale/en.ini index bdaf1d5..6b77621 100644 --- a/locale/en.ini +++ b/locale/en.ini @@ -53,3 +53,16 @@ user.form.transaction.success=Transaction successful spus=SPUs users=Users user.special=This is a special user. +error.unknown_product=Unknown product +error.invalid_amount=Amount invalid +error.invalid_comment=Comment invalid +error.invalid_user_src=Source user invalid +error.invalid_user_dst=Destination user invalid +error.invalid_user=Username invalid +error.open_log=Failed to open log file +error.invalid_barcode=Barcode invalid +error.open_new_products=Failed to open new products file +error.invalid_price=Price invalid +error.invalid_name=Name invalid +error.open_products=Failed to open products file +error.no_path=No path