Esempio n. 1
0
    def get_message_list(recipient_id, message_topic=None, read=None,
                         skip=0, limit=None):
        '''得到消息列表'''

        query = MessageDocument.get_query(recipient_id, message_topic)

        if read is not None:
            assert isinstance(read, bool)
            query.update({'read': read})

        cursor = MessageDocument.find(query).sort(
            [('time', pymongo.DESCENDING)]
        ).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        message_list = yield MessageDocument.to_list(cursor)
        for message in message_list:
            if 'sender' in message:
                message['sender'] = yield UserDocument.translate_dbref(
                    message['sender']
                )

            message['recipient'] = yield UserDocument.translate_dbref(
                message['recipient']
            )

            if 'data' not in message:
                continue

            if str(message['data'].collection) == str(
                    StatusDocument.meta['collection']):
                message['data'] = yield StatusDocument.get_status(
                    message['data'].id
                )
                continue

            message['data'] = yield Document.translate_dbref(
                message['data']
            )

            if message['data']:
                message['data'] = yield Document.translate_dbref_in_document(
                    message['data'], depth=2
                )

                if 'status' in message['data']:
                    message['data']['status'] = yield StatusDocument.get_status(
                        message['data']['status']['_id']
                    )

        raise gen.Return(message_list)
Esempio n. 2
0
    def get_message_list(recipient_id,
                         message_topic=None,
                         read=None,
                         skip=0,
                         limit=None):
        '''得到消息列表'''

        query = MessageDocument.get_query(recipient_id, message_topic)

        if read is not None:
            assert isinstance(read, bool)
            query.update({'read': read})

        cursor = MessageDocument.find(query).sort([('time', pymongo.DESCENDING)
                                                   ]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        message_list = yield MessageDocument.to_list(cursor)
        for message in message_list:
            if 'sender' in message:
                message['sender'] = yield UserDocument.translate_dbref(
                    message['sender'])

            message['recipient'] = yield UserDocument.translate_dbref(
                message['recipient'])

            if 'data' not in message:
                continue

            if str(message['data'].collection) == str(
                    StatusDocument.meta['collection']):
                message['data'] = yield StatusDocument.get_status(
                    message['data'].id)
                continue

            message['data'] = yield Document.translate_dbref(message['data'])

            if message['data']:
                message['data'] = yield Document.translate_dbref_in_document(
                    message['data'], depth=2)

                if 'status' in message['data']:
                    message['data'][
                        'status'] = yield StatusDocument.get_status(
                            message['data']['status']['_id'])

        raise gen.Return(message_list)
Esempio n. 3
0
    def get_top_topic_list(user_id=None, skip=0, limit=None):
        '''得到置顶的帖子'''

        query = {'top': True}

        cursor = TopicDocument.find(query).sort(
            [('publish_time', pymongo.DESCENDING)]
        ).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        topic_list = yield TopicDocument.to_list(cursor)
        for topic in topic_list:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author']
            )
            topic['last_comment'] = yield TopicCommentDocument.get_last_comment(
                topic['_id']
            )

            if 'images' in topic and topic['images']:
                topic['images'] = yield TopicDocument._extend_images(topic)

            if user_id is not None:
                topic['liked'] = yield TopicLikeDocument.is_liked(
                    topic['_id'], user_id
                )

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        raise gen.Return(topic_list)
Esempio n. 4
0
    def get_topic(topic_id, user_id):
        '''
        :Parameters:
          - `topic_id`: 话题id
          - `user_id`: 判断该user是否赞了该话题
        '''

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if topic:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author'])

            liked = yield TopicLikeDocument.is_liked(topic_id, user_id)
            last_comment = yield TopicCommentDocument.get_last_comment(
                topic['_id'])

            topic.update({'liked': liked, 'last_comment': last_comment})

            if 'images' in topic and topic['images']:
                topic['images'] = yield TopicDocument._extend_images(topic)

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        raise gen.Return(topic)
Esempio n. 5
0
    def get_topic(topic_id, user_id):
        '''
        :Parameters:
          - `topic_id`: 话题id
          - `user_id`: 判断该user是否赞了该话题
        '''

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if topic:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author']
            )

            liked = yield TopicLikeDocument.is_liked(topic_id, user_id)
            last_comment = yield TopicCommentDocument.get_last_comment(
                topic['_id']
            )

            topic.update({
                'liked': liked,
                'last_comment': last_comment
            })

            if 'images' in topic and topic['images']:
                topic['images'] = yield TopicDocument._extend_images(topic)

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        raise gen.Return(topic)
Esempio n. 6
0
    def get_top_topic_list(user_id=None, skip=0, limit=None):
        '''得到置顶的帖子'''

        query = {'top': True}

        cursor = TopicDocument.find(query).sort([
            ('publish_time', pymongo.DESCENDING)
        ]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        topic_list = yield TopicDocument.to_list(cursor)
        for topic in topic_list:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author'])
            topic[
                'last_comment'] = yield TopicCommentDocument.get_last_comment(
                    topic['_id'])

            if 'images' in topic and topic['images']:
                topic['images'] = yield TopicDocument._extend_images(topic)

            if user_id is not None:
                topic['liked'] = yield TopicLikeDocument.is_liked(
                    topic['_id'], user_id)

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        raise gen.Return(topic_list)
Esempio n. 7
0
    def get_topic_list_by_someone(author_id, skip=0, limit=None):
        '''得到某人的话题'''

        cursor = TopicDocument.find({
            'author': DBRef(
                UserDocument.meta['collection'], ObjectId(author_id)
            )
        }).sort([('publish_time', pymongo.DESCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        topic_list = yield TopicDocument.to_list(cursor)
        for topic in topic_list:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author']
            )
            topic['last_comment'] = yield TopicCommentDocument.get_last_comment(
                topic['_id']
            )

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        raise gen.Return(topic_list)
Esempio n. 8
0
    def get_comment_list(share_id, skip=0, limit=None):
        cursor = ShareCommentDocument.find({
            'share':
            DBRef(ShareDocument.meta['collection'], ObjectId(share_id))
        }).sort([('comment_time', pymongo.ASCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        comment_list = yield ShareCommentDocument.to_list(cursor)

        for i, comment in enumerate(comment_list):
            comment['floor'] = skip + 1 + i
            comment['author'] = yield UserDocument.translate_dbref(
                comment['author'])
            if 'replyeder' in comment:
                comment['replyeder'] = yield UserDocument.translate_dbref(
                    comment['replyeder'])

        raise gen.Return(comment_list)
Esempio n. 9
0
    def get_comment_list(topic_id, skip=0, limit=None):
        cursor = TopicCommentDocument.find({
            'topic': DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
        }).sort([('comment_time', pymongo.ASCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        comment_list = yield TopicCommentDocument.to_list(cursor)
        for i, comment in enumerate(comment_list):
            comment['floor'] = skip + 1 + i
            comment['author'] = yield UserDocument.translate_dbref(
                comment['author']
            )

            if 'replyeder' in comment:
                comment['replyeder'] = yield UserDocument.translate_dbref(
                    comment['replyeder']
                )

        raise gen.Return(comment_list)
Esempio n. 10
0
    def get_like_list(topic_id, skip=0, limit=None):
        cursor = TopicLikeDocument.find({
            'topic': DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
        }).sort([('like_time', pymongo.DESCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        like_list = yield TopicLikeDocument.to_list(cursor)
        for like in like_list:
            like['liker'] = yield UserDocument.translate_dbref(like['liker'])

        raise gen.Return(like_list)
Esempio n. 11
0
    def get_like_list(share_id, skip=0, limit=None):
        cursor = ShareLikeDocument.find({
            'share':
            DBRef(ShareDocument.meta['collection'], ObjectId(share_id))
        }).sort([('like_time', pymongo.DESCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        like_list = yield ShareLikeDocument.to_list(cursor)
        for like in like_list:
            like['liker'] = yield UserDocument.translate_dbref(like['liker'])

        raise gen.Return(like_list)
Esempio n. 12
0
    def get_last_comment(topic_id):
        '''得到某一话题的最后一个回复'''

        cursor = TopicCommentDocument.find({
            'topic':
            DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
        }).sort([('comment_time', pymongo.DESCENDING)]).limit(1)

        comment_list = yield TopicCommentDocument.to_list(cursor)

        last_comment = None
        if comment_list:
            last_comment = comment_list[0]
            last_comment['author'] = yield UserDocument.translate_dbref(
                last_comment['author'])

        raise gen.Return(last_comment)
Esempio n. 13
0
    def get_last_comment(topic_id):
        '''得到某一话题的最后一个回复'''

        cursor = TopicCommentDocument.find({
            'topic': DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
        }).sort([('comment_time', pymongo.DESCENDING)]).limit(1)

        comment_list = yield TopicCommentDocument.to_list(cursor)

        last_comment = None
        if comment_list:
            last_comment = comment_list[0]
            last_comment['author'] = yield UserDocument.translate_dbref(
                last_comment['author']
            )

        raise gen.Return(last_comment)
Esempio n. 14
0
    def get_recommend_topic_list(topic_id, size=10):
        '''根据某一话题推荐话题'''

        topic_list = []

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if topic:
            query = {
                '$and': [{
                    '_id': {
                        '$ne': ObjectId(topic_id)
                    }
                }, {
                    '$or': [{
                        'nodes': node
                    } for node in topic['nodes']]
                }]
            }
            count = yield TopicDocument.find(query).count()
            if count > size:
                skip = random.randint(0, count - size)
                cursor = TopicDocument.find(query).skip(skip).limit(size)
            else:
                cursor = TopicDocument.find(query)

            topic_list = yield TopicDocument.to_list(cursor)
            if not topic_list or len(topic_list) < size:
                query = {'$and': [{'_id': {'$ne': ObjectId(topic_id)}}]}
                count = yield TopicDocument.find(query).count()
                if count > size:
                    skip = random.randint(0, count - size)
                    cursor = TopicDocument.find(query).skip(skip).limit(size)
                else:
                    cursor = TopicDocument.find(query)

                topic_list = yield TopicDocument.to_list(cursor)

            for topic in topic_list:
                topic['author'] = yield UserDocument.translate_dbref(
                    topic['author'])

        raise gen.Return(topic_list)
Esempio n. 15
0
    def get_recommend_topic_list(topic_id, size=10):
        '''根据某一话题推荐话题'''

        topic_list = []

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if topic:
            query = {
                '$and': [
                    {'_id': {'$ne': ObjectId(topic_id)}},
                    {'$or': [{'nodes': node} for node in topic['nodes']]}
                ]
            }
            count = yield TopicDocument.find(query).count()
            if count > size:
                skip = random.randint(0, count - size)
                cursor = TopicDocument.find(query).skip(skip).limit(size)
            else:
                cursor = TopicDocument.find(query)

            topic_list = yield TopicDocument.to_list(cursor)
            if not topic_list or len(topic_list) < size:
                query = {
                    '$and': [
                        {'_id': {'$ne': ObjectId(topic_id)}}
                    ]
                }
                count = yield TopicDocument.find(query).count()
                if count > size:
                    skip = random.randint(0, count - size)
                    cursor = TopicDocument.find(query).skip(skip).limit(size)
                else:
                    cursor = TopicDocument.find(query)

                topic_list = yield TopicDocument.to_list(cursor)

            for topic in topic_list:
                topic['author'] = yield UserDocument.translate_dbref(
                    topic['author']
                )

        raise gen.Return(topic_list)
Esempio n. 16
0
    def get_topic_list_by_someone(author_id, skip=0, limit=None):
        '''得到某人的话题'''

        cursor = TopicDocument.find({
            'author':
            DBRef(UserDocument.meta['collection'], ObjectId(author_id))
        }).sort([('publish_time', pymongo.DESCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        topic_list = yield TopicDocument.to_list(cursor)
        for topic in topic_list:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author'])
            topic[
                'last_comment'] = yield TopicCommentDocument.get_last_comment(
                    topic['_id'])

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        raise gen.Return(topic_list)
Esempio n. 17
0
    def get_topic_list(node_id=None, user_id=None, sort=None,
                       skip=0, limit=None):
        '''
        :Parameters:
          - `node_id`: 如果node_id不为None, 那么得到该节点下的话题
          - `sort`: 排序方式, 只可能为time或者popularity
          - `skip`: 默认0
          - `limit`: 默认None

        NOTE: NEED CACHE !!
        '''

        def score(topic):
            '''
            公式为: score = like_times + comment_times/2 + read_times/5

            即: 1 * like_times = 2 * comment_times = 5 * read_times
            '''
            return (topic['like_times'] + topic['comment_times'] / 2.0 +
                    topic['read_times'] / 5.0)

        top_topic_list = yield TopicDocument.get_top_topic_list(user_id)
        if node_id is not None:
            query = {
                'nodes': DBRef(
                    NodeDocument.meta['collection'], ObjectId(node_id)
                )
            }
        else:
            query = {
                '_id': {'$nin': [ObjectId(t['_id']) for t in top_topic_list]}
            }

        cursor = TopicDocument.find(query)
        if sort == 'time' or sort is None:
            cursor = cursor.sort(
                [('last_update_time', pymongo.DESCENDING)]
            ).skip(skip)

            if limit is not None:
                cursor = cursor.limit(limit)

            topic_list = yield TopicDocument.to_list(cursor)
        else:
            topic_list = yield TopicDocument.to_list(cursor)
            topic_list.sort(
                cmp=lambda x, y: -1 if score(x) < score(y) else 1,
                reverse=True
            )

            if limit is not None:
                topic_list = topic_list[skip: skip + limit]
            else:
                topic_list = topic_list[skip:]

        for topic in topic_list:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author']
            )
            topic['last_comment'] = yield TopicCommentDocument.get_last_comment(
                topic['_id']
            )

            if 'images' in topic and topic['images']:
                topic['images'] = yield TopicDocument._extend_images(topic)

            if user_id is not None:
                topic['liked'] = yield TopicLikeDocument.is_liked(
                    topic['_id'], user_id
                )

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        if not node_id and skip == 0 and top_topic_list:
            topic_list = top_topic_list + topic_list

        raise gen.Return(topic_list)
Esempio n. 18
0
    def get_topic_list(node_id=None,
                       user_id=None,
                       sort=None,
                       skip=0,
                       limit=None):
        '''
        :Parameters:
          - `node_id`: 如果node_id不为None, 那么得到该节点下的话题
          - `sort`: 排序方式, 只可能为time或者popularity
          - `skip`: 默认0
          - `limit`: 默认None

        NOTE: NEED CACHE !!
        '''
        def score(topic):
            '''
            公式为: score = like_times + comment_times/2 + read_times/5

            即: 1 * like_times = 2 * comment_times = 5 * read_times
            '''
            return (topic['like_times'] + topic['comment_times'] / 2.0 +
                    topic['read_times'] / 5.0)

        top_topic_list = yield TopicDocument.get_top_topic_list(user_id)
        if node_id is not None:
            query = {
                'nodes': DBRef(NodeDocument.meta['collection'],
                               ObjectId(node_id))
            }
        else:
            query = {
                '_id': {
                    '$nin': [ObjectId(t['_id']) for t in top_topic_list]
                }
            }

        cursor = TopicDocument.find(query)
        if sort == 'time' or sort is None:
            cursor = cursor.sort([('last_update_time', pymongo.DESCENDING)
                                  ]).skip(skip)

            if limit is not None:
                cursor = cursor.limit(limit)

            topic_list = yield TopicDocument.to_list(cursor)
        else:
            topic_list = yield TopicDocument.to_list(cursor)
            topic_list.sort(cmp=lambda x, y: -1 if score(x) < score(y) else 1,
                            reverse=True)

            if limit is not None:
                topic_list = topic_list[skip:skip + limit]
            else:
                topic_list = topic_list[skip:]

        for topic in topic_list:
            topic['author'] = yield UserDocument.translate_dbref(
                topic['author'])
            topic[
                'last_comment'] = yield TopicCommentDocument.get_last_comment(
                    topic['_id'])

            if 'images' in topic and topic['images']:
                topic['images'] = yield TopicDocument._extend_images(topic)

            if user_id is not None:
                topic['liked'] = yield TopicLikeDocument.is_liked(
                    topic['_id'], user_id)

            for i, node in enumerate(topic['nodes']):
                topic['nodes'][i] = yield NodeDocument.translate_dbref(node)

        if not node_id and skip == 0 and top_topic_list:
            topic_list = top_topic_list + topic_list

        raise gen.Return(topic_list)