Added "logged in as" text to identify the current user.

Added pagination to the default post view.
Fixed a bug where the comments were being ordered incorrectly.
This commit is contained in:
Raoul Snyman 2010-01-31 15:45:20 +02:00
parent 00256870f8
commit c0e74e19bb
10 changed files with 84 additions and 19 deletions

View File

@ -33,10 +33,21 @@ log = logging.getLogger(__name__)
class BlogController(BaseController):
def index(self):
c.posts = Session.query(Post)\
posts = Session.query(Post)\
.filter_by(status=u'published')\
.order_by(Post.created.desc())\
.all()
.order_by(Post.created.desc())
pagination = utils.paginate(posts, 10,
int(request.GET.get(u'page', 1)), '/')
c.posts = pagination[u'records']
if pagination[u'prev'] != pagination[u'page']:
c.first_page = pagination[u'first']
c.prev_page = pagination[u'prev']
if pagination[u'next'] != pagination[u'page']:
c.next_page = pagination[u'next']
c.last_page = pagination[u'last']
c.list_start = pagination[u'start']
c.list_total = pagination[u'total']
c.list_end = pagination[u'end']
return render(u'/blog/index.mako')
def archive(self, year=None, month=None, day=None):

View File

@ -68,9 +68,12 @@ class PostController(BaseController):
post.url = url
tags = c.form_values[u'tags']
tag_list = [tag_name.strip() for tag_name in tags.split(u',')]
for tag in post.tags:
if tag.name in tag_list:
del tag_list[tag.name]
tag_urls = [utils.generate_url(tag_name) for tag_name in tags.split(u',')]
db_tags = Session.query(Tag).filter(Tag.url.in_(tag_urls)).all()
post.tags = []
for tag in db_tags:
tag_list.remove(tag.name)
post.tags.append(tag)
for tag in tag_list:
post.tags.append(Tag(name=tag, url=utils.generate_url(tag)))
Session.add(post)

View File

@ -27,10 +27,9 @@ Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to both as 'h'.
"""
from routes import url_for
from webhelpers.html import escape, HTML, literal, url_escape
from webhelpers.date import distance_of_time_in_words
from pylons import session
from pylons import session, url as url_for
from pylons.controllers.util import redirect_to
class Flash(object):

View File

@ -30,7 +30,7 @@ from datetime import datetime
from pylons import config, c
from turbomail import Message
from scribeengine.lib.base import render
from scribeengine.lib.base import render, h
def send_mail(template, mail_to, mail_from, subject, variables={}, attachments=[]):
"""
@ -111,3 +111,37 @@ def month_last_day(datetime):
else:
day = 28
return datetime.replace(day=day, hour=23, minute=59, second=59, microsecond=99999)
def paginate(query, page_size, current_page, base_url=None):
query_count = query.count()
page_count = query_count / page_size
if query_count % page_size > 0:
page_count += 1
offset = (current_page - 1) * page_size
records = query.offset(offset).limit(page_size).all()
prev_page = current_page - 1 if current_page - 1 >= 1 else 1
next_page = current_page + 1 if current_page + 1 <= page_count else page_count
if base_url:
return {
u'page': h.url_for(base_url, page=current_page),
u'first': h.url_for(base_url, page=1),
u'prev': h.url_for(base_url, page=prev_page),
u'next': h.url_for(base_url, page=next_page),
u'last': h.url_for(base_url, page=page_count),
u'start': offset + 1,
u'end': offset + len(records),
u'total': query_count,
u'records': records
}
else:
return {
u'page': current_page,
u'first': 1,
u'prev': prev_page,
u'next': next_page,
u'last': page_count,
u'start': offset + 1,
u'end': offset + len(records),
u'total': query_count,
u'records': records
}

View File

@ -24,7 +24,7 @@
The application's model objects
"""
from sqlalchemy.orm import mapper, relation
from sqlalchemy.orm import mapper, relation, backref
from scribeengine.model import meta
from scribeengine.model.tables import categories_table, comments_table, \
@ -46,8 +46,8 @@ mapper(Permission, permissions_table)
mapper(Post, posts_table,
properties={
u'categories': relation(Category, backref='posts', secondary=categories_posts_table),
u'comments': relation(Comment, backref=u'post', order_by=Comment.created.desc()),
u'tags': relation(Tag, backref=u'posts', secondary=posts_tags_table)
u'comments': relation(Comment, backref=u'post', order_by=Comment.created.asc()),
u'tags': relation(Tag, backref=backref(u'posts', order_by='posts.created DESC'), secondary=posts_tags_table)
}
)
mapper(Role, roles_table,

View File

@ -39,7 +39,7 @@ class BaseModel(object):
def __repr__(self):
if hasattr(self, 'id'):
return '<%s id=%s>' % (self.__name__, self.id)
return '<%s id=%s>' % (self.__class__.__name__, self.id)
class Category(BaseModel):
"""

View File

@ -440,14 +440,12 @@ fieldset {
/* Tag Editor */
.tagEditor
{
.tagEditor {
margin: 4px 0;
padding: 0;
}
.tagEditor li
{
.tagEditor li {
display: inline;
background-image: url(/images/minus_small.png);
background-color: #454545;
@ -461,12 +459,18 @@ fieldset {
-webkit-border-radius: 5px;
}
.tagEditor li:hover
{
.tagEditor li:hover {
background-color: #eee;
color: #000;
}
/* Pagination */
.pagination {
padding-bottom: 0.5em;
text-align: right;
}
/* Miscellaneous Styles */
.hidden {

View File

@ -35,6 +35,7 @@
<li><a href="${h.url_for(controller='post', action='new')}">New Post</a></li>
% endif
<li><a href="${h.url_for(controller='admin', action='logout')}">Logout</a></li>
<li>Logged in as <em>${c.current_user.nick}</em></li>
% else:
<li><a href="${h.url_for(controller='admin', action='login')}">Login</a></li>
% endif

View File

@ -1,5 +1,6 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<%include file="/pagination.mako"/>
% for post in c.posts:
<div class="post">
<h2 class="title"><a href="${h.url_for_post(post)}">${post.title}</a></h2>
@ -19,3 +20,4 @@
</p>
</div>
% endfor
<%include file="/pagination.mako"/>

View File

@ -0,0 +1,11 @@
<div class="pagination">
% if c.prev_page:
<a href="${c.first_page}" title="First">&lt;&lt; First</a>
<a href="${c.prev_page}" title="Previous">&lt; Previous</a> |
% endif
${c.list_start} to ${c.list_end} of ${c.list_total}
% if c.next_page:
| <a href="${c.next_page}" title="Next">Next &gt;</a>
<a href="${c.last_page}" title="Last">Last &gt;&gt;</a>
% endif
</div>