Skip to content
Browse files

post list on the backend with search

  • Loading branch information...
1 parent 8581ddf commit 9ca7e12c695cfb4828a7d229132cdb99363f001e @jmoiron committed Apr 4, 2012
View
33 src/monet/app/admin.go
@@ -11,7 +11,7 @@ import (
"code.google.com/p/gorilla/sessions"
)
-var listPageSize = 20
+var listPageSize = 15
var indexListSize = 6
var adminBase = template.Base {Path: "admin/base.mustache"}
@@ -115,12 +115,35 @@ func postList(ctx *web.Context, page string) string {
if requireAuthentication(ctx) { return "" }
pageNum := 1
- if len(page) != 0 {
- pageNum,_ = strconv.Atoi(page)
+ if len(page) != 0 { pageNum,_ = strconv.Atoi(page) }
+
+ n := listPageSize
+ paginator := NewPaginator(pageNum, n)
+ paginator.Link = "/admin/posts/"
+ cursor := db.Posts()
+
+ var posts []db.Post
+ // do a search, if required, of title and content
+ var err error
+ var numObjects int
+
+ if len(ctx.Params["Search"]) > 0 {
+ term := dict{"$regex": ctx.Params["Search"]}
+ search := dict{"$or": []dict{dict{"title":term}, dict{"content":term}}}
+ err = cursor.Latest(search).Skip(paginator.Skip).Limit(n).All(&posts)
+ numObjects,_ = cursor.Latest(search).Count()
+ } else {
+ err = cursor.Latest(dict{"published":1}).Skip(paginator.Skip).
+ Limit(n).Iter().All(&posts)
+ numObjects,_ = cursor.C.Count()
}
- fmt.Println("Listing page ", pageNum)
- return ""
+ if err != nil {
+ fmt.Println(err)
+ }
+
+ return adminBase.Render("admin/post-list.mustache", dict{
+ "Posts": posts, "Pagination": paginator.Render(numObjects)})
}
func postAdd(ctx *web.Context) string {
View
26 src/monet/app/app.go
@@ -2,7 +2,7 @@ package app
import (
"fmt"
- "time"
+ "strconv"
"github.com/hoisie/web.go"
"monet/template"
"monet/db"
@@ -13,6 +13,7 @@ type dict map[string]interface{}
var base = template.Base {Path: "base.mustache"}
func Attach(url string) {
+ web.Get(url + "blog/page/(\\d+)", blogPage)
web.Get(url + "blog/([^/]+)/", blogDetail)
web.Get(url + "blog/", blogIndex)
web.Get(url + "([^/]*)", index)
@@ -21,17 +22,11 @@ func Attach(url string) {
// helpers
-func FmtTimestamp(ts uint64) string {
- ut := time.Unix(int64(ts), 0)
- return ut.Format("Jan _2")
-}
-
func RenderPost(post *db.Post) string {
if len(post.ContentRendered) == 0 {
post.Update()
}
- return template.Render("post.mustache", post, dict{
- "NaturalTime": FmtTimestamp(post.Timestamp)})
+ return template.Render("post.mustache", post)
}
// views
@@ -51,16 +46,27 @@ func index(s string) string {
}
func blogIndex() string {
+ return blogPage("1")
+}
+
+func blogPage(page string) string {
+ pn,_ := strconv.Atoi(page)
+ perPage := 10
+ paginator := NewPaginator(pn, perPage)
+ paginator.Link = "/blog/page/"
+
var posts []db.Post
- err := db.Posts().Latest(dict{"published":1}).Limit(10).Iter().All(&posts)
+ err := db.Posts().Latest(dict{"published":1}).Skip(paginator.Skip).Limit(10).Iter().All(&posts)
if err != nil {
fmt.Println(err)
}
+ numObjects,_ := db.Posts().C.Count()
rendered := []dict{}
for _,p := range posts {
rendered = append(rendered, dict{"Body": RenderPost(&p)})
}
- return base.Render("post-list.mustache", dict{"Posts": rendered})
+ return base.Render("post-list.mustache", dict{
+ "Posts": rendered, "Pagination": paginator.Render(numObjects)})
}
func blogDetail(ctx *web.Context, slug string) string {
View
13 src/monet/db/db.go
@@ -83,6 +83,19 @@ func Pages() *PageCursor {
// -- Posts --
+func FmtTimestamp(ts uint64) string {
+ now := time.Now()
+ ut := time.Unix(int64(ts), 0)
+ if now.Year() == ut.Year() {
+ return ut.Format("Jan _2")
+ }
+ return ut.Format("Jan _2 2006")
+}
+
+func (p Post) NaturalTime() string {
+ return FmtTimestamp(p.Timestamp)
+}
+
func (p *Post) Update() error {
p.ContentRendered = template.RenderMarkdown(p.Content)
if len(p.Id) > 0 {
View
8 static/static/js/admin.js
@@ -82,6 +82,14 @@ $(function() {
}
});
});
+ if (window.location.search.length > 0) {
+ var qs = window.location.search;
+ $("ul.paginator a").each(function() {
+ console.log("Hiya");
+ var $this = $(this);
+ $this.attr("href", $this.attr("href") + qs);
+ });
+ }
});
View
47 static/static/style.less
@@ -68,23 +68,28 @@ a { color: @bluelink; font-weight: bold; text-decoration: none;
}
.admin {
+ ul.shortlist { list-style: none; margin: 0; padding: 5px 0;
+ li { padding: 5px;
+ a { padding: 5px 10px; margin-left: 5px; }
+ a.del { padding: 0; margin-top: -1px; display:none; float: right; font-size: 22px}
+ &:hover {
+ a.del { display: inline-block; color: #fa2a00;
+ &:hover { color: darken(#fa2a00, 15%); }
+ }
+ }
+ }
+ }
+ ul.listpage {
+ a { color: #000; &:hover { color: @bluelink; }}
+ li {
+ span.date { margin-right: 32px; }
+ &:hover { span.date { margin-right: 0; }}
+ }
+ }
.panel-sep { height: 1em; border-bottom: 1px solid #fbfbfb; margin-bottom: 1em; }
.panel { color: #999;
a { color: #999; &:visited { color: #999 }}
&:hover a { color: #000; &:hover { color: @bluelink; }}
- ul { list-style: none; margin: 0; padding: 5px 0;
- li { padding: 5px;
- a { padding: 5px 10px; margin-left: 5px; }
- a.del { padding: 0; margin-top: -1px; }
- }
- }
- a.del { display: none; }
-
- li:hover {
- a.del { display: inline-block; float: right; color: #fa2a00; font-size: 22px;
- &:hover { color: darken(#fa2a00, 15%); }
- }
- }
}
.left-panel { float: left; width: (@site-width/2); }
.right-panel { float: right; width: (@site-width/2); }
@@ -125,6 +130,7 @@ a { color: @bluelink; font-weight: bold; text-decoration: none;
.extras { padding: 1em 0; border-top: 1px solid #eee;
input { color: #444; }
}
+ input.search { width: 100%; }
}
.posts-form {
@@ -148,3 +154,18 @@ a { color: @bluelink; font-weight: bold; text-decoration: none;
.page-content-input { height: @site-width - 100px; width: @site-width - 20px; color: #444; }
}
+.paginator {
+ text-align: center;
+ margin: 1em 0;
+ padding: 0;
+ list-style: none;
+ li { display: inline-block;
+ a { padding: 3px 8px; border: 1px solid @bluelink;
+ &:hover { background-color: @bluehover; color: white; text-shadow: 2px 2px darken(@bluehover, 20%); }
+ }
+ &.num { padding: 1px 9px; border: 1px solid #ccc;
+ &:hover { background-color: #eee; text-shadow: 1px 1px 2px darken(#eee, 20%); }
+ }
+ &.inter { border: 0; &:hover { background-color: transparent; text-shadow: none; }}
+ }
+}
View
8 templates/admin/index.mustache
@@ -1,7 +1,7 @@
<div class="left-panel panel">
<h3><a href="/admin/posts/">Posts</a></h3>
- <ul class="post-list list">
+ <ul class="post-list shortlist">
<li><form method="GET" action="/admin/posts/add/"><input type="text" data-default="Type a new title..." value="Type a new title..." class="list-new js-clear-default" id="post-title" name="Title"></form></li>
{{#posts}}
<li><a href="/admin/posts/edit/{{Slug}}">{{Title}}</a> <a class="del" href="/admin/posts/delete/{{Slug}}"><i class="icon icon-remove-sign"></i></a></li>
@@ -11,7 +11,7 @@
<div class="right-panel panel">
<h3><a href="/admin/notes/">Notes</a></h3>
- <ul class="note-list list">
+ <ul class="note-list shortlist">
<li><form method="GET" action="/admin/notes/add/"><input type="text" data-default="Type a new note..." value="Type a new note..." class="list-new js-clear-default" id="note-title" name="Title"></form></li>
{{#notes}}
<li><a href="/admin/notes/edit/{{Slug}}">{{Name}}</a></li>
@@ -21,7 +21,7 @@
<div class="clear panel-sep"></div>
<div class="left-panel panel">
<h3><a href="/admin/posts/">Unpublished</a></h3>
- <ul class="post-list list">
+ <ul class="post-list shortlist">
{{#unpublished}}
<li><a href="/admin/posts/edit/{{Slug}}">{{Title}}</a> <a class="del" href="/admin/posts/delete/{{Slug}}"><i class="icon icon-remove-sign"></i></a></li>
{{/unpublished}}
@@ -30,7 +30,7 @@
<div class="right-panel panel">
<h3><a href="/admin/notes/">Pages</a></h3>
- <ul class="note-list list">
+ <ul class="note-list shortlist">
<li><form method="GET" action="/admin/pages/add/"><input type="text" data-default="Type a new URL..." value="Type a new URL..." class="list-new js-clear-default" id="page-url" name="Url"></form></li>
{{#pages}}
<li><a href="/admin/pages/edit/{{Url}}">/{{Url}}</a> <a class="del" href="/admin/pages/delete/{{Url}}"><i class="icon icon-remove-sign"></i></a></li>
View
16 templates/admin/post-list.mustache
@@ -0,0 +1,16 @@
+
+{{^Unpublished}}<h2>Posts</h2>{{/Unpublished}}{{#Unpublished}}<h2>Unpublished</h2>{{/Unpublished}}
+
+<form action="/admin/posts/" method="GET"><input type="text" name="Search" class="search js-clear-default" data-default="Type a search query..." value="{{#Search}}{{Search}}{{/Search}}{{^Search}}Type a search query...{{/Search}}"></form>
+
+<ul class="shortlist listpage">
+{{#Posts}}
+<li>
+<a href="/admin/posts/edit/{{Slug}}">{{Title}}</a>
+<a class="del" href="/admin/posts/delete/{{Slug}}"><i class="icon icon-remove-sign"></i></a>
+<span class="date">{{NaturalTime}}</span>
+</li>
+{{/Posts}}
+</ul>
+
+{{{Pagination}}}
View
1 templates/admin/posts-edit.mustache
@@ -11,6 +11,7 @@
<div>
<label for="timestamp">timestamp</label>
<input type="timestamp" name="timestamp" id="timestamp" value="{{Timestamp}}">
+ <span class="date" style="float:none;">{{NaturalTime}}</span>
</div>
</div>
<div class="published">
View
3 templates/post-list.mustache
@@ -3,5 +3,4 @@
{{{Body}}}
{{/Posts}}
-<div class="pagination">
-</div>
+{{{Pagination}}}

0 comments on commit 9ca7e12

Please sign in to comment.
Something went wrong with that request. Please try again.