[luci] Problem with the Button class

Jo-Philipp Wich xm at subsignal.org
Mon Nov 2 08:29:32 CET 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

> 1.Does anyone know what I am missing?

Try to set the property "test.rmempty" to "false".

> 2. Is there useful documentation for this method somewhere?

Only in the source code atm, sorry.

> 3. Assuming it is possible to get the test.write method to work what
> is the best way of displaying a new page with traceroute data on it.

Personally I would issue an HTTP redirect in .write() and then re-render
the page to include the traceroute output.

In the corresponding controller you need something like that:

  page = entry({"admin", "foo", "bar"}, cbi("my/model"), "My Label", 20)
  page.leaf = true
     -- this passes additional url segments as args to the model

Then in your map you add a conditional field:

  -- ip addr to trace
  local addr = s:option(Value, "ipaddr", "IP Address")

  -- find button, rmempty=false to invoke .write()
  local find = s:option(Button, "_test", "Traceroute")
  find.inputstyle = "find"
  find.rmempty = false

  -- if ip is given reload this page with ip addr as arg
  function find.write(self, section, value)
         local ip = addr:formvalue(section)
         if ip then
           luci.http.redirect(luci.dispatcher.build_url(
             "admin/foo/bar", ip
           ))
         end
  end

  -- http://host/luci/admin/foo/bar/192.168.1.2
  --  -> since .leaf=true in the controller, arg[1] = 192.168.1.2
  if arg[1] then
    local output = s:option(DummyValue, "_output")
    output.template = "my/app/traceroute"
    output.ipaddr   = arg[1]
  end

  return m

Now we need a template for the traceroute field. The template
is expected in /usr/lib/lua/luci/view/my/app/traceroute.htm .

  <%+cbi/valueheader%>
  <% if self.ipaddr then %>
    <pre><%-
      local t = io.popen("traceroute -n " .. self.ipaddr, "r")
      if t then
        while true do
          local l = t:read("*l")
          if not l then
            break
          elseif l:match("^%s*%d") then
            write(l .. "\n")
          end
        end
        t:close()
      end
    -%></pre>
  <% else %>
    <strong>No ip address given!</strong>
  <% end %>
  <%+cbi/valuefooter%>


HTH,
JoW
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkruilcACgkQdputYINPTPOk8ACgilIihM4Zwpr4/iMcmn54kh5T
wAEAn00phUt7gfNUgJChakZ3++rDXI8W
=HDmg
-----END PGP SIGNATURE-----


More information about the luci mailing list