Skip to content

Commit

Permalink
use nickname instead of nick. basic paging implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Dirolf committed May 11, 2009
1 parent f53e96e commit e22c673
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
26 changes: 21 additions & 5 deletions sms.py
Expand Up @@ -2,23 +2,39 @@

import web
from pymongo.connection import Connection
from pymongo import DESCENDING

urls = ("/image/(.*)", "Image",
"/", "Main",
"/page/(.*)", "Main")
"/page/(\d+)", "Main")
app = web.application(urls, globals())
render = web.template.render("templates/")
db = Connection().sms
page_size = 5

class Main:
def GET(self, page=None):
return render.index(db.messages.find())
if page is None:
page = 0
page = int(page)

previous = None
if page > 0:
previous = page - 1

next = None
if db.messages.count() > (page + 1) * page_size:
next = page + 1

return render.index(db.messages.find().sort("date", DESCENDING).limit(page_size).skip(page * page_size),
previous, next)

def POST(self):
post_data = web.input()
db.messages.insert({"nick": post_data.nickname,
"text": post_data.text,
"date": datetime.datetime.utcnow()})
if post_data.nickname and post_data.text:
db.messages.insert({"nickname": post_data.nickname,
"text": post_data.text,
"date": datetime.datetime.utcnow()})
raise web.seeother("/")

class Image:
Expand Down
10 changes: 6 additions & 4 deletions templates/index.html
@@ -1,4 +1,4 @@
$def with (messages)
$def with (messages, previous, next)

<html>
<head>
Expand Down Expand Up @@ -55,7 +55,7 @@
$# thumbnail here
</td>
<td valign="top">
<div class="message-nickname">$message["nick"]</div>
<div class="message-nickname">$message["nickname"]</div>
<div class="message-text">$message["text"]</div>
</td>
</tr>
Expand All @@ -70,8 +70,10 @@
</div>
<div class="separator"></div>
<div class="navigation">
<a href="/page/0">&lt; Back</a>
<a href="/page/2">Next &gt;</a>
$if previous != None:
<a href="/page/$previous">&lt; Back</a>
$if next != None:
<a href="/page/$next">Next &gt;</a>
</div>
</body>
</html>

0 comments on commit e22c673

Please sign in to comment.