Source

openju / openju / views.py

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

from openju.util import pretty_date

def render_user_nick(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_nick(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_nick(message.user),
        user_reply = render_user_nick(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))))

def render_user_info(user):
    def render_users_list(users):
        return u", ".join(x.nick for x in users)
        
    return (
        u"""User %(nick)s\n"""
        u"""Messages:\n"""
        u"""  overall: %(overall)s\n"""
        u"""  today: %(today)s\n\n"""
        u"""Friends: %(friends)s""") % dict(
        nick = render_user_nick(user),
        overall = user.num_overall_messages(),
        today = user.num_today_messages(),
        friends = render_users_list(user.friends),
        )