luci-app-mwan3: refactoring warning compilation on the policy config pages
authorFlorian Eckert <fe@dev.tdt.de>
Wed, 17 Jan 2018 16:01:42 +0000 (17:01 +0100)
committerFlorian Eckert <fe@dev.tdt.de>
Thu, 18 Jan 2018 14:09:46 +0000 (15:09 +0100)
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
applications/luci-app-mwan3/luasrc/model/cbi/mwan/policy.lua
applications/luci-app-mwan3/luasrc/model/cbi/mwan/policyconfig.lua

index 6de5ae8..44585d7 100644 (file)
@@ -1,36 +1,36 @@
--- ------ extra functions ------ --
+ds = require "luci.dispatcher"
+sys = require "luci.sys"
+
+function policyCheck()
+       local policy_error = {}
 
 
-function policyCheck() -- check to see if any policy names exceed the maximum of 15 characters
        uci.cursor():foreach("mwan3", "policy",
                function (section)
        uci.cursor():foreach("mwan3", "policy",
                function (section)
+                       policy_error[section[".name"]] = false
                        if string.len(section[".name"]) > 15 then
                        if string.len(section[".name"]) > 15 then
-                               nameTooLong = 1
-                               err_name_list = err_name_list .. section[".name"] .. " "
+                               policy_error[section[".name"]] = true
                        end
                end
        )
                        end
                end
        )
-end
 
 
-function policyWarn() -- display status and warning messages at the top of the page
-       if nameTooLong == 1 then
-               return "<font color=\"ff0000\"><strong>" .. translate("WARNING: Some policies have names exceeding the maximum of 15 characters!") .. "</strong></font>"
-       else
-               return ""
-       end
+       return policy_error
 end
 
 end
 
--- ------ policy configuration ------ --
-
-ds = require "luci.dispatcher"
-sys = require "luci.sys"
-
-nameTooLong = 0
-err_name_list = " "
-policyCheck()
+function policyError(policy_error)
+       local warnings = ""
+       for i, k in pairs(policy_error) do
+               if policy_error[i] == true then
+                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                               translatef("WARNING: Policie %s has exceeding the maximum name of 15 characters", i)
+                               )
+               end
+       end
 
 
+       return warnings
+end
 
 m5 = Map("mwan3", translate("MWAN - Policies"),
 
 m5 = Map("mwan3", translate("MWAN - Policies"),
-       policyWarn())
+       policyError(policyCheck()))
 
 
 mwan_policy = m5:section(TypedSection, "policy", nil,
 
 
 mwan_policy = m5:section(TypedSection, "policy", nil,
@@ -79,15 +79,4 @@ last_resort = mwan_policy:option(DummyValue, "last_resort", translate("Last reso
                end
        end
 
                end
        end
 
-errors = mwan_policy:option(DummyValue, "errors", translate("Errors"))
-       errors.rawhtml = true
-       function errors.cfgvalue(self, s)
-               if not string.find(err_name_list, " " .. s .. " ") then
-                       return ""
-               else
-                       return "<span title=\"Name exceeds 15 characters\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
-               end
-       end
-
-
 return m5
 return m5
index b9ff321..f29a060 100644 (file)
@@ -1,49 +1,19 @@
--- ------ extra functions ------ --
-
-function policyCheck() -- check to see if this policy's name exceed the maximum of 15 characters
-       policyNameLength = string.len(arg[1])
-       if policyNameLength > 15 then
-               nameTooLong = 1
-       end
-end
-
-function policyWarn() -- display status and warning messages at the top of the page
-       if nameTooLong == 1 then
-               return "<font color=\"ff0000\"><strong>" .. translatef("WARNING: This policy's name is %d characters exceeding the maximum of 15!", policyNameLength) .. "</strong></font>"
-       else
-               return ""
-       end
-end
-
-function cbiAddMember(field)
-       uci.cursor():foreach("mwan3", "member",
-               function (section)
-                       field:value(section[".name"])
-               end
-       )
-end
-
--- ------ policy configuration ------ --
-
 dsp = require "luci.dispatcher"
 arg[1] = arg[1] or ""
 
 dsp = require "luci.dispatcher"
 arg[1] = arg[1] or ""
 
-nameTooLong = 0
-policyCheck()
-
-
-m5 = Map("mwan3", translatef("MWAN Policy Configuration - %s", arg[1]),
-       policyWarn())
-       m5.redirect = dsp.build_url("admin", "network", "mwan", "policy")
-
+m5 = Map("mwan3", translatef("MWAN Policy Configuration - %s", arg[1]))
+m5.redirect = dsp.build_url("admin", "network", "mwan", "policy")
 
 mwan_policy = m5:section(NamedSection, arg[1], "policy", "")
        mwan_policy.addremove = false
        mwan_policy.dynamic = false
 
 
 mwan_policy = m5:section(NamedSection, arg[1], "policy", "")
        mwan_policy.addremove = false
        mwan_policy.dynamic = false
 
-
-use_member = mwan_policy:option(DynamicList, "use_member", translate("Member used"))
-       cbiAddMember(use_member)
+member = mwan_policy:option(DynamicList, "use_member", translate("Member used"))
+m5.uci:foreach("mwan3", "member",
+       function(s)
+               member:value(s['.name'], s['.name'])
+       end
+)
 
 last_resort = mwan_policy:option(ListValue, "last_resort", translate("Last resort"),
        translate("When all policy members are offline use this behavior for matched traffic"))
 
 last_resort = mwan_policy:option(ListValue, "last_resort", translate("Last resort"),
        translate("When all policy members are offline use this behavior for matched traffic"))