85 lines
3.3 KiB
Python
85 lines
3.3 KiB
Python
|
# -*- 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.controllers.util import redirect_to
|
||
|
|
||
|
class Flash(object):
|
||
|
def set_message(self, message_text, message_type):
|
||
|
session = self._get_session()
|
||
|
session[u'flash.text'] = message_text
|
||
|
session[u'flash.type'] = message_type
|
||
|
session.save()
|
||
|
|
||
|
def has_message(self):
|
||
|
session = self._get_session()
|
||
|
return u'flash.text' in session
|
||
|
|
||
|
def get_message_text(self):
|
||
|
session = self._get_session()
|
||
|
message_text = session.pop(u'flash.text', None)
|
||
|
if not message_text:
|
||
|
return None
|
||
|
session.save()
|
||
|
return message_text
|
||
|
|
||
|
def get_message_type(self):
|
||
|
session = self._get_session()
|
||
|
message_type = session.pop(u'flash.type', None)
|
||
|
if not message_type:
|
||
|
return None
|
||
|
session.save()
|
||
|
return message_type
|
||
|
|
||
|
def _get_session(self):
|
||
|
from pylons import session
|
||
|
return session
|
||
|
|
||
|
|
||
|
def teaser(text, url):
|
||
|
position = text.find(u'</p>')
|
||
|
if position > 0:
|
||
|
return text[:position]
|
||
|
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
|
||
|
|
||
|
flash = Flash()
|