This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
old-scribeengine/scribeengine/lib/helpers.py

93 lines
3.5 KiB
Python
Raw Normal View History

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 #
###############################################################################
"""
Helper functions
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
2010-01-15 20:55:30 +00:00
from pylons.controllers.util import redirect_to
class Flash(object):
def set_message(self, message_text, message_type, message_head=None):
2010-01-15 20:55:30 +00:00
session[u'flash.text'] = message_text
session[u'flash.type'] = message_type
session[u'flash.head'] = message_head
2010-01-15 20:55:30 +00:00
session.save()
def has_message(self):
return session.get(u'flash.text')
def has_header(self):
return session.get(u'flash.head')
2010-01-15 20:55:30 +00:00
def get_message_text(self):
message_text = session.pop(u'flash.text', None)
if not message_text:
return None
session.save()
return message_text
def get_message_type(self):
message_type = session.pop(u'flash.type', None)
if not message_type:
return None
session.save()
return message_type
def teaser(text, url):
position = text.find(u'</p>')
if position > 0:
2010-01-22 11:09:20 +00:00
return text[:position + 4]
2010-01-15 20:55:30 +00:00
elif len(text) > 300:
text = text[:297]
position = len(text) - 1
while position > 0 and text[position] not in [u'<', u'>']:
position -= 1
if position != 0 and text[position + 1] == u'/':
position -= 1
while position > 0 and text[position] not in [u'<']:
position -= 1
if position != 0 and text[position] == u'<':
text = text[:position]
return text
def url_for_post(post):
#TODO: this is hard coded.
return url_for(
2010-01-22 11:09:20 +00:00
controller='blog',
action='view',
2010-01-22 11:09:20 +00:00
year=post.created.strftime('%Y'),
month=post.created.strftime('%m'),
day=post.created.strftime('%d'),
url=post.url
)
2010-01-18 20:16:46 +00:00
2010-01-15 20:55:30 +00:00
flash = Flash()