Source

openju / openju / model.py

Full commit
# -*- coding: utf-8 -*-

import datetime
from mongoengine import \
    Document, ReferenceField, StringField, DateTimeField, ListField, \
    DictField, Q

class User(Document):
    jid = StringField(max_length=200, required=True)
    nick = StringField(max_length=200, required=True, unique=True)
    friends = ListField(ReferenceField('self'))
    
    meta = {
        'indexes': ['nick', 'friends']
        }
    
    def friends_ids(self):
        return (x.id for x in self.friends)

    def num_overall_messages(self):
        from openju.bl.message import num_overall_messages
        return num_overall_messages(q=lambda q: q & Q(user=self))

    def num_today_messages(self):
        from openju.bl.message import num_today_messages
        return num_today_messages(lambda q: q & Q(user=self))

    def last_friends_messages(self):
        from openju.bl.message import get_last_messages
        return get_last_messages(q=lambda q: q & Q(user__in = self.friends))

    def last_messages(self):
        from openju.bl.message import get_last_messages
        return get_last_messages(q=lambda q: q & Q(user = self))

    def friend_of(self):
        return User.objects(friends=self)
    
    def get_notifications(self):
        from openju.bl.notify import get_user_notifications
        return get_user_notifications(self)

class Message(Document):
    user = ReferenceField(User, required=True)
    body = StringField(required=True)
    dt = DateTimeField(default=datetime.datetime.utcnow, required=True)
    tags = ListField(StringField(max_length=50))
    reply_to = ReferenceField('self')
    
    meta = {
        'ordering': ['-dt'],
        'indexes': ['user', 'dt', 'reply_to'],
    }
    
    def get_replies(self):
        from openju.bl.message import get_replies
        return get_replies(self.id)
    
    def post_reply(self, user, reply_body):
        from openju.bl.message import post_reply
        return post_reply(self, user, reply_body)

    def is_reply(self):
        return bool(self.reply_to)

class Notification(Document):
    user = ReferenceField(User, required=True)
    action = StringField(required=True)
    info = DictField()