Calendar Step 1: Made the days work.

This commit is contained in:
Raoul Snyman 2010-02-27 23:18:58 +02:00
commit bd721a35ed
6 changed files with 85 additions and 13 deletions

View File

@ -62,24 +62,40 @@ class BlogController(BaseController):
if day and month and year: if day and month and year:
start_date = datetime(int(year), int(month), int(day), 0, 0, 0, 0) start_date = datetime(int(year), int(month), int(day), 0, 0, 0, 0)
end_date = datetime(int(year), int(month), int(day), 23, 59, 59, 99999) end_date = datetime(int(year), int(month), int(day), 23, 59, 59, 99999)
c.page_title = u'Archive: %s' % start_date.strftime('%d %B %Y') c.datestring = start_date.strftime('%d %B %Y')
if c.datestring[0] == u'0':
c.datestring = c.datestring[1:]
elif month and year and not day: elif month and year and not day:
start_date = utils.month_first_day(datetime(int(year), int(month), 1)) start_date = utils.month_first_day(datetime(int(year), int(month), 1))
end_date = utils.month_last_day(datetime(int(year), int(month), 1)) end_date = utils.month_last_day(datetime(int(year), int(month), 1))
c.page_title = u'Archive: %s' % start_date.strftime('%B %Y') c.datestring = start_date.strftime('%B %Y')
elif year and not month: elif year and not month:
start_date = datetime(int(year), 1, 1, 0, 0, 0, 0) start_date = datetime(int(year), 1, 1, 0, 0, 0, 0)
end_date = datetime(int(year), 12, 31, 23, 59, 59, 99999) end_date = datetime(int(year), 12, 31, 23, 59, 59, 99999)
c.page_title = u'Archive: %s' % start_date.strftime('%Y') c.datestring = start_date.strftime('%Y')
else: else:
start_date = None start_date = None
end_date = None end_date = None
c.posts = Session.query(Post) c.datestring = u'all time'
c.page_title = u'Archive for %s.' % c.datestring
posts = Session.query(Post)
if start_date and end_date: if start_date and end_date:
c.posts = c.posts\ posts = posts\
.filter(Post.created >= start_date)\ .filter(Post.created >= start_date)\
.filter(Post.created <= end_date) .filter(Post.created <= end_date)
c.posts = c.posts.order_by(Post.created.desc()).all() posts = posts.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/archive.mako') return render(u'/blog/archive.mako')
def view(self, url): def view(self, url):

View File

@ -27,7 +27,7 @@ Provides the BaseController class for subclassing.
""" """
from calendar import Calendar from calendar import Calendar
from datetime import datetime from datetime import datetime, timedelta
from decorator import decorator from decorator import decorator
import logging import logging
@ -38,11 +38,12 @@ from pylons.controllers import WSGIController
from pylons.templating import render_mako from pylons.templating import render_mako
from sqlalchemy.sql.expression import asc, desc from sqlalchemy.sql.expression import asc, desc
from formencode import Schema, Invalid from formencode import Schema, Invalid
from monthdelta import monthdelta
from scribeengine.lib import helpers as h from scribeengine.lib import helpers as h
from scribeengine.lib.validation import jsvalidate from scribeengine.lib.validation import jsvalidate
from scribeengine.model.meta import Session from scribeengine.model.meta import Session
from scribeengine.model import Variable, User, Category, Page from scribeengine.model import Variable, User, Category, Page, Post
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -59,6 +60,19 @@ class BaseController(WSGIController):
c.today = datetime.today() c.today = datetime.today()
if not c.thismonth: if not c.thismonth:
c.thismonth = datetime.now() c.thismonth = datetime.now()
c.prev_month = c.thismonth - monthdelta(1)
c.next_month = c.thismonth + monthdelta(1)
month_start = datetime(c.thismonth.year, c.thismonth.month, 1, 0, 0, 0, 0)
month_end = c.next_month.replace(day=1, hour=23, minute=59, second=59, microsecond=9999) - timedelta(seconds=1)
posts = Session.query(Post)\
.filter(Post.created >= month_start)\
.filter(Post.created <= month_end)\
.all()
c.month_posts = {}
for post in posts:
if post.created.day not in c.month_posts:
c.month_posts[post.created.day] = []
c.month_posts[post.created.day].append(post)
self._add_javascript(u'jquery.js') self._add_javascript(u'jquery.js')
if c.jsvalidation: if c.jsvalidation:
self._add_javascript(u'jquery.validate.js') self._add_javascript(u'jquery.validate.js')

View File

@ -339,6 +339,10 @@ hr {
background: #111111; background: #111111;
} }
#calendar tbody td#today {
background: #333333;
}
#calendar a { #calendar a {
text-decoration: none; text-decoration: none;
font-weight: bold; font-weight: bold;

View File

@ -0,0 +1,29 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<h2 class="title">Archives for ${c.datestring}</h2>
<%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>
<div class="entry">
${h.literal(h.teaser(post.body))}
</div>
<p class="meta">
<span class="byline">
Posted by ${post.user.nick} on ${post.created.strftime('%B %d, %Y')}
% if c.current_user and c.current_user.id == post.user.id and c.current_user.has_permission(u'Edit My Posts'):
[<a href="${h.url_for(controller=u'post', action=u'edit', id=post.id)}">Edit</a>]
% endif
</span>
<a href="${h.url_for_post(post)}" class="read-more">Read more</a>
% if len(post.comments) == 0:
<a href="${h.url_for_post(post)}#comments" class="comments">No comments</a>
% elif len(post.comments) == 1:
<a href="${h.url_for_post(post)}#comments" class="comments">1 comment</a>
% else:
<a href="${h.url_for_post(post)}#comments" class="comments">${len(post.comments)} comments</a>
% endif
</p>
</div>
% endfor
<%include file="/pagination.mako"/>

View File

@ -15,9 +15,9 @@
</thead> </thead>
<tfoot> <tfoot>
<tr> <tr>
<td abbr="October" colspan="3" id="prev"><a href="#" title="View posts for October 2007">&laquo; Oct</a></td> <td abbr="${c.prev_month.strftime('%B')}" colspan="3" id="prev"><a href="#" title="View posts for ${c.prev_month.strftime('%B %Y')}">&laquo; ${c.prev_month.strftime('%b')}</a></td>
<td class="pad">&nbsp;</td> <td class="pad">&nbsp;</td>
<td abbr="December" colspan="3" id="next"><a href="#" title="View posts for October 2007">Dec &raquo;</a></td> <td abbr="${c.next_month.strftime('%B')}" colspan="3" id="next"><a href="#" title="View posts for ${c.next_month.strftime('%B %Y')}">${c.next_month.strftime('%b')} &raquo;</a></td>
</tr> </tr>
</tfoot> </tfoot>
<tbody> <tbody>
@ -27,10 +27,18 @@
% if day == 0: % if day == 0:
<td class="pad">&nbsp;</td> <td class="pad">&nbsp;</td>
% elif day == c.today.day: % elif day == c.today.day:
% if day in c.month_posts and len(c.month_posts[day]) > 0:
<td id="today"><a href="${h.url_for(controller='blog', action='archive', year=c.thismonth.year, month=c.thismonth.month, day=day)}">${day}</a></td>
% else:
<td id="today">${day}</td> <td id="today">${day}</td>
% endif
% else:
% if day in c.month_posts and len(c.month_posts[day]) > 0:
<td><a href="${h.url_for(controller='blog', action='archive', year=c.thismonth.year, month=c.thismonth.month, day=day)}">${day}</a></td>
% else: % else:
<td>${day}</td> <td>${day}</td>
% endif % endif
% endif
% endfor % endfor
</tr> </tr>
% endfor % endfor

View File

@ -25,9 +25,10 @@ setup(
author_email='', author_email='',
url='', url='',
install_requires=[ install_requires=[
"Pylons>=0.9.7", "Pylons==0.9.7",
"SQLAlchemy>=0.5", "SQLAlchemy>=0.5,<0.6",
"TurboMail>=3.0" "TurboMail>=3.0,<3.1",
"MonthDelta>=0.9,<0.10"
], ],
setup_requires=["PasteScript>=1.6.3"], setup_requires=["PasteScript>=1.6.3"],
packages=find_packages(exclude=['ez_setup']), packages=find_packages(exclude=['ez_setup']),