openju / openju / bl / message.py

#!/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):
    """
    Raises ``MultipleObjectsReturned`` or ``DoesNotExist`` errors.
    Need to catch them in controller.
    """
    return Message.objects.get(id=message_id)

def get_replies(message_id):
    """
    Raises ``MultipleObjectsReturned`` or ``DoesNotExist`` errors.
    Need to catch them in controller.
    """
    return Message.objects(reply_to=message_id)

# def post_reply(jid, hrid, message, r_no=None):
#     # hrid = int(hrid)
#     # if r_no is not None:
#     #     r_no = int(r_no)
#     with get_mongo_db() as db:
#         # check is post exist and get id
#         post = (
#             db.messages
#             .find(
#                 dict(
#                     hrid = hrid,
#                     r_no = r_no),
#                 [],
#                 )
#             .limit(1))
#         if post.count() == 0:
#             raise PostNotExistError(u'Message does not exist')
#         post_id = post[0]['_id']
#         next_r_no = get_next_hrid_r_no(hrid)
#         db.messages.insert(dict(
#             jid = jid,
#             message = message,
#             hrid = hrid,
#             r_to = post_id,
#             r_no = next_r_no,
#             dt = datetime.now()))
#         return next_r_no
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.