Source

openju / openju / bl / message.py

Full commit
#!/usr/bin/python
# -*- coding: utf-8 -*-

from openju.model import *

def post_message(user, body):
    message = (
        Message(
            user=user,
            body=body))
    message.save()
    return message

def get_last_messages(skip=None, limit=10):
    return Message.objects(reply_to__exists=False)[skip:limit]

def get_message(message_id):
    return Message.objects.get(id=message_id)

def get_replies(message_id):
    return Message.objects(reply_to=message_id)

def post_reply(message, user, body):
    """ Returns newly created message """
    reply = Message(
        user=user,
        body=body,
        reply_to=message,
        )
    reply.save()
    return reply

def num_today_messages(q=lambda x: x):
    """ ``q`` is decorator for query """
    today = datetime.datetime.combine(datetime.date.today(),
                                      datetime.time())
    return len(Message.objects(q(Q(dt__gt = today))))

def num_overall_messages(q=lambda x: x):
    return len(Message.objects(q(Q(user=self))))