Add pagination on moderation page

This commit is contained in:
Las Zenow 2012-10-31 10:24:17 +01:00
parent 5abab5f5dd
commit bbe10b76ff
4 changed files with 51 additions and 15 deletions

View file

@ -5,6 +5,7 @@ import (
"log"
"net/http"
"strings"
"strconv"
)
type settingsData struct {
@ -159,6 +160,9 @@ type newData struct {
S Status
Found int
Books []newBook
Page int
Next string
Prev string
}
func newHandler(w http.ResponseWriter, r *http.Request) {
@ -173,7 +177,16 @@ func newHandler(w http.ResponseWriter, r *http.Request) {
return
}
res, num, _ := db.GetNewBooks()
page := 0
if len(r.Form["p"]) != 0 {
var err error
page, err = strconv.Atoi(r.Form["p"][0])
if err != nil {
page = 0
}
}
res, num, _ := db.GetNewBooks(NEW_ITEMS_PAGE, page*NEW_ITEMS_PAGE)
var data newData
data.S = GetStatus(w, r)
data.Found = num
@ -183,6 +196,13 @@ func newHandler(w http.ResponseWriter, r *http.Request) {
_, data.Books[i].TitleFound, _ = db.GetBooks(buildQuery("title:"+b.Title), 1)
_, data.Books[i].AuthorFound, _ = db.GetBooks(buildQuery("author:"+strings.Join(b.Author, " author:")), 1)
}
data.Page = page+1
if num > (page+1)*NEW_ITEMS_PAGE {
data.Next = "/new/?p=" + strconv.Itoa(page+1)
}
if page > 0 {
data.Prev = "/new/?p=" + strconv.Itoa(page-1)
}
loadTemplate(w, "new", data)
}

View file

@ -10,6 +10,7 @@ const (
PASS_SALT = "ImperialLibSalt"
TAGS_DISPLAY = 50
SEARCH_ITEMS_PAGE = 10
NEW_ITEMS_PAGE = 50
TEMPLATE_PATH = "templates/"
BOOKS_PATH = "books/"
COVER_PATH = "cover/"

View file

@ -151,21 +151,12 @@ func (d *DB) GetDownloadedBooks(num int) (books []Book, err error) {
return
}
/* Returns: list of books, number found and err
/* optional parameters: length and start index
*
* Returns: list of books, number found and err
*/
func (d *DB) GetNewBooks() (books []Book, num int, err error) {
var q *mgo.Query
q = d.books.Find(bson.M{"$nor": []bson.M{{"active": true}}}).Sort("-_id")
num, err = q.Count()
if err != nil {
return
}
err = q.All(&books)
for i, b := range books {
books[i].Id = bson.ObjectId(b.Id).Hex()
}
return
func (d *DB) GetNewBooks(r ...int) (books []Book, num int, err error) {
return d.GetBooks(bson.M{"$nor": []bson.M{{"active": true}}}, r...)
}
func (d *DB) BookActive(id bson.ObjectId) bool {

View file

@ -7,6 +7,18 @@
</div>
{{end}}
<p class="centered">Found {{.Found}} books.<br /></p>
<ul class="pager">
{{if .Prev}}
<li class="previous">
<a href="{{.Prev}}">&larr; Prev</a>
</li>
{{end}}
{{if .Next}}
<li class="next">
<a href="{{.Next}}">Next &rarr;</a>
</li>
{{end}}
</ul>
{{range .Books}}
{{$titleFound := .TitleFound}}
@ -41,6 +53,18 @@
</div>
{{end}}
{{end}}
<ul class="pager">
{{if .Prev}}
<li class="previous">
<a href="{{.Prev}}">&larr; Prev</a>
</li>
{{end}}
{{if .Next}}
<li class="next">
<a href="{{.Next}}">Next &rarr;</a>
</li>
{{end}}
</ul>
{{if .Books}}
<div class="centered btn-group">
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>