diff --git a/scribeengine/controllers/blog.py b/scribeengine/controllers/blog.py index 3f87f02..6ff585a 100644 --- a/scribeengine/controllers/blog.py +++ b/scribeengine/controllers/blog.py @@ -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): diff --git a/scribeengine/controllers/post.py b/scribeengine/controllers/post.py index 242cd0c..447fa94 100644 --- a/scribeengine/controllers/post.py +++ b/scribeengine/controllers/post.py @@ -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) diff --git a/scribeengine/lib/helpers.py b/scribeengine/lib/helpers.py index d020ee9..b751c7a 100644 --- a/scribeengine/lib/helpers.py +++ b/scribeengine/lib/helpers.py @@ -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): diff --git a/scribeengine/lib/utils.py b/scribeengine/lib/utils.py index 434b252..d8cca2f 100644 --- a/scribeengine/lib/utils.py +++ b/scribeengine/lib/utils.py @@ -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 + } diff --git a/scribeengine/model/__init__.py b/scribeengine/model/__init__.py index 854ff67..6af5860 100644 --- a/scribeengine/model/__init__.py +++ b/scribeengine/model/__init__.py @@ -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, diff --git a/scribeengine/model/classes.py b/scribeengine/model/classes.py index 49ca412..69c75d5 100644 --- a/scribeengine/model/classes.py +++ b/scribeengine/model/classes.py @@ -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): """ diff --git a/scribeengine/public/styles/style.css b/scribeengine/public/styles/style.css index d6fbe3d..2c83039 100644 --- a/scribeengine/public/styles/style.css +++ b/scribeengine/public/styles/style.css @@ -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 { diff --git a/scribeengine/templates/base.mako b/scribeengine/templates/base.mako index ed63f0a..c5c7a5b 100644 --- a/scribeengine/templates/base.mako +++ b/scribeengine/templates/base.mako @@ -35,6 +35,7 @@
  • New Post
  • % endif
  • Logout
  • +
  • Logged in as ${c.current_user.nick}
  • % else:
  • Login
  • % endif diff --git a/scribeengine/templates/blog/index.mako b/scribeengine/templates/blog/index.mako index 011f27a..27c0921 100644 --- a/scribeengine/templates/blog/index.mako +++ b/scribeengine/templates/blog/index.mako @@ -1,5 +1,6 @@ <%inherit file="/base.mako"/> <%include file="/flash.mako"/> + <%include file="/pagination.mako"/> % for post in c.posts:

    ${post.title}

    @@ -19,3 +20,4 @@

    % endfor + <%include file="/pagination.mako"/> diff --git a/scribeengine/templates/pagination.mako b/scribeengine/templates/pagination.mako new file mode 100644 index 0000000..3b5c8aa --- /dev/null +++ b/scribeengine/templates/pagination.mako @@ -0,0 +1,11 @@ +