summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlo Landmeter <clandmeter@gmail.com>2015-03-19 18:50:51 +0000
committerCarlo Landmeter <clandmeter@gmail.com>2015-03-19 18:50:51 +0000
commit44c702b0e7aaa201de6335db704399eeb8cdcff2 (patch)
tree92e5eb09a5f3b1a4594d3574c6c1c9e1a0600365
parent7afbeee21074065c054ac2a587a209a8eeb20bc3 (diff)
downloadaports-turbo-44c702b0e7aaa201de6335db704399eeb8cdcff2.tar.bz2
aports-turbo-44c702b0e7aaa201de6335db704399eeb8cdcff2.tar.xz
add pager and allow to filter on pkgname in filelist
-rwxr-xr-xaports.lua73
-rw-r--r--tpl/contents.tpl23
-rw-r--r--tpl/package.tpl2
3 files changed, 79 insertions, 19 deletions
diff --git a/aports.lua b/aports.lua
index cd16817..1c2269b 100755
--- a/aports.lua
+++ b/aports.lua
@@ -16,21 +16,29 @@ local ContentsRenderer = class("ContentsRenderer", turbo.web.RequestHandler)
function ContentsRenderer:get()
local args = {
filename = self:get_argument("filename","", true),
+ pkgname = self:get_argument("pkgname", "", true),
arch = self:get_argument("arch", "x86", true),
+ page = self:get_argument("page", "", true),
}
- local table = { [args.arch] = true }
- if args.filename ~= "" then
- local result = QueryContents(args)
- if next(result) ~= nil then
- table.rows = result
- end
+ -- assign different variables for db query
+ local fname = (args.filename == "") and "%" or args.filename
+ local pname = (args.pkgname == "") and "%" or args.pkgname
+ local table = {}
+ if not (fname == "%" and pname == "%") then
+ table.rows = QueryContents(fname, pname, args.arch, args.page)
+ local rows = (table.rows ~= nil) and (#table.rows) or 0
+ table.pager = CreatePagerUri(args, rows)
end
- table.contents = true
+ print(inspect(table.pager))
+ table.page = (args.page == "") and "1" or args.page
table.filename = args.filename
+ table.pkgname = args.pkgname
+ table[args.arch] = true
+ table.contents = true
+ table.pkgname = args.pkgname
table.header = tpl:render("header.tpl", table)
table.footer = tpl:render("footer.tpl", table)
- local page = tpl:render(self.options, table)
- self:write(page)
+ self:write(tpl:render(self.options, table))
end
local PackagesRenderer = class("PackagesRenderer", turbo.web.RequestHandler)
@@ -78,11 +86,12 @@ function PackageRenderer:get(arch, name)
self:write(page)
end
-function QueryContents(terms)
+function QueryContents(filename, pkgname, arch, page)
require('DBI')
+ local offset = (tonumber(page) == nil) and 0 or tonumber(page)*50
local dbh = assert(DBI.Connect('SQLite3', 'db/filelist.db'))
- local sth = assert(dbh:prepare('select * from filelist where file like ? and arch like ? limit 100'))
- sth:execute(terms.filename, terms.arch)
+ local sth = assert(dbh:prepare('select * from filelist where file like ? and pkgname like ? and arch like ? limit ?,50'))
+ sth:execute(filename, pkgname, arch, offset)
local r = {}
for row in sth:rows(true) do
r[#r + 1] = {
@@ -93,7 +102,9 @@ function QueryContents(terms)
}
end
sth:close()
- return r
+ if next(r) ~= nil then
+ return r
+ end
end
function QueryPackages(terms)
@@ -103,7 +114,7 @@ function QueryPackages(terms)
sth:execute(terms.package)
local r = {}
for row in sth:rows(true) do
- r[#r + 1] = {
+ r[#r+1] = {
package = row.name,
version = row.version,
project = row.url,
@@ -199,6 +210,40 @@ function QuerySubPackages(origin, name)
end
end
+function CreatePagerUri(args, rows)
+ local r,p,n = {},{},{};
+ for get,value in pairs (args) do
+ if (get == 'page') then
+ value = (tonumber(value)) and tonumber(value) or 1
+ -- do not include page on first page
+ if value > 2 then
+ p[#p + 1] = get.."="..(value-1)
+ end
+ n[#n + 1] = get.."="..(value+1)
+ else
+ p[#p + 1] = get.."="..(value)
+ n[#n + 1] = get.."="..(value)
+ end
+ end
+
+ -- show pager when rows are 50+
+ if rows >= 50 then
+ r.next = table.concat(n, '&amp;')
+ r.prev = table.concat(p, '&amp;')
+ end
+ -- do not show prev on first page
+ if args.page == "" and rows >= 50 then
+ r.prev = nil
+ end
+ -- show prev on last page
+ if args.page ~= "" and (rows >= 0 and rows <= 50) then
+ r.prev = table.concat(p, '&amp;')
+ end
+ if next(r) ~= nil then
+ return {r}
+ end
+end
+
turbo.web.Application({
{"^/$", turbo.web.RedirectHandler, "/packages"},
{"^/contents$", ContentsRenderer, "contents.tpl"},
diff --git a/tpl/contents.tpl b/tpl/contents.tpl
index 3eaaf3f..9e2eff6 100644
--- a/tpl/contents.tpl
+++ b/tpl/contents.tpl
@@ -9,11 +9,15 @@
<input type="text" class="form-control" id="filename" name="filename" value="{{{filename}}}">
</div>
<div class="form-group">
+ <label for="pkgname">Package name</label>
+ <input type="text" class="form-control" id="pkgname" name="pkgname" value="{{{pkgname}}}">
+ </div>
+ <div class="form-group">
<label for="arch">Architecture</label>
<select name="arch" class="form-control" id="arch">
- <option {{#x86}}selected{{/x86}} >x86</option>
- <option {{#x86_64}}selected{{/x86_64}} >x86_64</option>
- <option {{#armhf}}selected{{/armhf}} >armhf</option>
+ <option{{#x86}} selected {{/x86}}>x86</option>
+ <option{{#x86_64}} selected {{/x86_64}}>x86_64</option>
+ <option{{#armhf}} selected {{/armhf}}>armhf</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
@@ -29,7 +33,7 @@
</tr>{{#rows}}
<tr>
<td>{{{file}}}</td>
- <td>{{{pkgname}}}</td>
+ <td><a href="/package/{{{arch}}}/{{{pkgname}}}">{{{pkgname}}}</a></td>
<td>{{{repo}}}</td>
<td>{{{arch}}}</td>
</tr>{{/rows}}
@@ -40,6 +44,17 @@
{{{/rows}}}
</table>
</div>
+ <div class="panel-footer text-center">{{#pager}}
+ <nav>
+ <ul class="pagination">{{/pager}}{{#pager}}{{#prev}}
+ <li class=""><a href="/contents?{{{prev}}}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>{{/prev}}{{/pager}}{{#pager}}{{^prev}}
+ <li class="disabled"><a href="" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>{{/prev}}{{/pager}}{{#pager}}
+ <li class="active"><a href="#">{{{page}}}</a></li>{{/pager}}{{#pager}}{{#next}}
+ <li><a href="/contents?{{{next}}}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>{{/next}}{{/pager}}{{#pager}}{{^next}}
+ <li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>{{/next}}{{/pager}}{{#pager}}
+ </ul>
+ </nav>{{/pager}}
+ </div>
</div>
</div>
{{{footer}}}
diff --git a/tpl/package.tpl b/tpl/package.tpl
index 03771f3..34f46a9 100644
--- a/tpl/package.tpl
+++ b/tpl/package.tpl
@@ -72,7 +72,7 @@
</tr>
<tr>
<th>Contents:</th>
- <td><a href="/contents?filename={{{name}}}&amp;arch={{{arch}}}">Contents of package</a></td>
+ <td><a href="/contents?pkgname={{{name}}}&amp;arch={{{arch}}}">Contents of package</a></td>
</tr>{{/name}}{{^name}}
<tr>
<td>This package does not exist!</td>