Source

openju / openju / views.py

# -*- coding: utf-8 -*-

from openju.util import pretty_date

def render_user(user):
    return u"@%s" % user.nick

def render_message_id(message):
    return u"#%s" % message.id

def render_quote(message):
    def stripped(body):
        if len(body) > 20:
            return u"%s..." % body[:20]
        else:
            return body
    
    return u"> %s" % stripped(message.body)

def render_message(message):
    return (
        u"""%(user)s:\n"""
        u"""%(body)s\n"""
        u"""%(m_id)s posted at %(dt)s""") % dict(
        user = render_user(message.user),
        body = message.body,
        m_id = render_message_id(message),
        dt = pretty_date(message.dt)
        )

def render_messages_list(messages):
    def message_parts(messages):
        return (render_message(x) for x in messages)
    return u"\n\n".join(message_parts(messages))

def render_reply(message):
    return (
        u"""%(user)s replied to %(user_reply)s (%(user_reply_msgid)s):\n"""
        u"""%(quote)s\n"""
        u"""%(body)s\n"""
        u"""%(m_id)s posted at %(dt)s""") % dict(
        user = render_user(message.user),
        user_reply = render_user(message.reply_to.user),
        user_reply_msgid = render_message_id(message.reply_to),
        quote = render_quote(message.reply_to),
        body = message.body,
        m_id = render_message_id(message),
        dt = pretty_date(message.dt)
        )

def render_replies_list(replies):
    return u"\n\n".join(render_reply(x) for x in replies)

def render_message_and_replies(message, replies):
    def filter_empty(stream):
        return (x for x in stream if x)
    
    return u"\n\n".join(filter_empty((
        render_message(message),
        render_replies_list(replies))))