2010-01-15 20:55:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# ScribeEngine - Open Source Blog Software #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2010 Raoul Snyman #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License along #
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
2010-02-22 20:46:35 +00:00
|
|
|
from pprint import pformat
|
2010-01-15 20:55:30 +00:00
|
|
|
|
2010-02-17 21:10:21 +00:00
|
|
|
from sqlalchemy.sql import or_
|
|
|
|
|
2010-01-15 20:55:30 +00:00
|
|
|
from scribeengine.lib.base import *
|
|
|
|
from scribeengine.lib import utils
|
2010-01-22 11:07:27 +00:00
|
|
|
from scribeengine.model import Post, Comment, Tag
|
2010-01-15 20:55:30 +00:00
|
|
|
from scribeengine.model.meta import Session
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class BlogController(BaseController):
|
|
|
|
|
2010-02-24 11:40:39 +00:00
|
|
|
def __before__(self):
|
|
|
|
BaseController.__before__(self)
|
|
|
|
self._add_javascript(u'jquery.elastic.js')
|
|
|
|
self._add_javascript(u'ScribeEngine.Blog.js')
|
|
|
|
|
2010-01-15 20:55:30 +00:00
|
|
|
def index(self):
|
2010-01-31 13:45:20 +00:00
|
|
|
posts = Session.query(Post)\
|
2010-01-15 20:55:30 +00:00
|
|
|
.filter_by(status=u'published')\
|
2010-01-31 13:45:20 +00:00
|
|
|
.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']
|
2010-01-15 20:55:30 +00:00
|
|
|
return render(u'/blog/index.mako')
|
|
|
|
|
|
|
|
def archive(self, year=None, month=None, day=None):
|
|
|
|
if day and month and year:
|
|
|
|
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)
|
2010-02-27 21:18:16 +00:00
|
|
|
c.datestring = start_date.strftime('%d %B %Y')
|
|
|
|
if c.datestring[0] == u'0':
|
|
|
|
c.datestring = c.datestring[1:]
|
2010-01-15 20:55:30 +00:00
|
|
|
elif month and year and not day:
|
|
|
|
start_date = utils.month_first_day(datetime(int(year), int(month), 1))
|
|
|
|
end_date = utils.month_last_day(datetime(int(year), int(month), 1))
|
2010-02-27 21:18:16 +00:00
|
|
|
c.datestring = start_date.strftime('%B %Y')
|
2010-01-15 20:55:30 +00:00
|
|
|
elif year and not month:
|
|
|
|
start_date = datetime(int(year), 1, 1, 0, 0, 0, 0)
|
|
|
|
end_date = datetime(int(year), 12, 31, 23, 59, 59, 99999)
|
2010-02-27 21:18:16 +00:00
|
|
|
c.datestring = start_date.strftime('%Y')
|
2010-01-15 20:55:30 +00:00
|
|
|
else:
|
|
|
|
start_date = None
|
|
|
|
end_date = None
|
2010-02-27 21:18:16 +00:00
|
|
|
c.datestring = u'all time'
|
|
|
|
c.page_title = u'Archive for %s.' % c.datestring
|
|
|
|
posts = Session.query(Post)
|
2010-01-15 20:55:30 +00:00
|
|
|
if start_date and end_date:
|
2010-02-27 21:18:16 +00:00
|
|
|
posts = posts\
|
2010-01-15 20:55:30 +00:00
|
|
|
.filter(Post.created >= start_date)\
|
|
|
|
.filter(Post.created <= end_date)
|
2010-02-27 21:18:16 +00:00
|
|
|
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']
|
2010-01-15 20:55:30 +00:00
|
|
|
return render(u'/blog/archive.mako')
|
|
|
|
|
|
|
|
def view(self, url):
|
|
|
|
c.post = Session.query(Post)\
|
|
|
|
.filter_by(url=url)\
|
|
|
|
.filter_by(status=u'published')\
|
|
|
|
.first()
|
|
|
|
c.page_title = c.post.title
|
|
|
|
return render(u'/blog/view.mako')
|
2010-01-18 20:16:46 +00:00
|
|
|
|
2010-01-22 11:07:27 +00:00
|
|
|
def tag(self, id=None):
|
|
|
|
if not id:
|
|
|
|
h.redirect_to('/')
|
2010-01-27 08:33:06 +00:00
|
|
|
c.tag = Session.query(Tag).filter_by(url=id).first()
|
2010-01-22 11:07:27 +00:00
|
|
|
if not c.tag:
|
|
|
|
h.redirect_to('/')
|
|
|
|
c.page_title = u'Blog posts with tag: %s' % c.tag.name
|
|
|
|
return render('/blog/tag.mako')
|
|
|
|
|
2010-01-19 19:51:45 +00:00
|
|
|
@authenticate()
|
2010-01-18 20:16:46 +00:00
|
|
|
def comment_POST(self, id):
|
|
|
|
if not id:
|
|
|
|
h.flash.set_message(u'There was a problem submitting your comment.', u'error')
|
|
|
|
h.redirect_to('/')
|
|
|
|
post = Session.query(Post).get(id)
|
|
|
|
if not post or post.comment_status != u'open':
|
|
|
|
h.flash.set_message(u'There was a problem submitting your comment.', u'error')
|
|
|
|
h.redirect_to('/')
|
|
|
|
comment = Comment(
|
|
|
|
user = c.current_user,
|
|
|
|
title = c.form_values[u'title'],
|
|
|
|
body = c.form_values[u'body']
|
|
|
|
)
|
|
|
|
post.comments.append(comment)
|
|
|
|
Session.add(post)
|
|
|
|
Session.commit()
|
|
|
|
h.flash.set_message(u'Successfully submitted your comment.', u'success')
|
2010-01-18 22:29:50 +00:00
|
|
|
h.redirect_to(h.url_for_post(post))
|
2010-01-18 20:16:46 +00:00
|
|
|
|
2010-02-17 21:10:21 +00:00
|
|
|
def search(self):
|
|
|
|
c.querystring = request.GET.get(u'q')
|
|
|
|
if not c.querystring:
|
|
|
|
h.flash.set_message(u'You didn\'t supply anything to search for.', u'error')
|
|
|
|
h.redirect_to('/')
|
2010-02-22 20:46:35 +00:00
|
|
|
kwprocessor = utils.KeywordProcessor(
|
|
|
|
groups=[None, '+', '-'],
|
|
|
|
group=tuple,
|
|
|
|
normalize=lambda s: s.strip(' \"\'')
|
|
|
|
)
|
|
|
|
keywords, ands, nots = kwprocessor.split(c.querystring)
|
|
|
|
or_clauses = []
|
|
|
|
for kw in keywords:
|
|
|
|
or_clauses.append(Post.body.contains(kw.strip()))
|
|
|
|
or_clauses.append(Post.title.contains(kw.strip()))
|
|
|
|
and_clauses = [or_(Post.body.contains(aw.strip()),
|
|
|
|
Post.title.contains(aw.strip())) for aw in ands]
|
|
|
|
not_clauses = [or_(Post.body.contains(nw.strip()),
|
|
|
|
Post.title.contains(nw.strip())) for nw in nots]
|
2010-02-18 07:02:46 +00:00
|
|
|
c.posts = Session.query(Post)
|
2010-02-22 20:46:35 +00:00
|
|
|
if len(or_clauses) > 0:
|
|
|
|
c.posts = c.posts.filter(or_(*or_clauses))
|
|
|
|
if len(and_clauses) > 0:
|
|
|
|
for and_clause in and_clauses:
|
|
|
|
c.posts = c.posts.filter(and_clause)
|
|
|
|
if len(not_clauses) > 0:
|
|
|
|
for not_clause in not_clauses:
|
|
|
|
c.posts = c.posts.filter(~not_clause)
|
|
|
|
c.posts = c.posts.order_by(Post.created.desc()).all()
|
|
|
|
c.page_title = u'Search'
|
2010-02-17 21:10:21 +00:00
|
|
|
return render(u'/blog/search.mako')
|
|
|
|
|
2010-02-28 21:15:33 +00:00
|
|
|
def calendar(self, year, month):
|
|
|
|
#c.calendar = Calendar(6)
|
|
|
|
#c.today = datetime.today()
|
|
|
|
c.thismonth = datetime.now().replace(int(year), int(month))
|
|
|
|
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)
|
|
|
|
return render(u'/calendar.mako')
|
|
|
|
|