示例#1
0
文件: handler.py 项目: anstones/yong
    def post(self):
        response_data = {}

        form = TopicLikeForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        topic_id = form.topic_id.data

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if not topic:
            raise HTTPError(404)

        can_afford = yield UserDocument.can_afford(self.current_user['_id'],
                                                   WEALTH_SETTINGS['like'])

        if not can_afford and str(self.current_user['_id']) != str(
                topic['author'].id):
            response_data.update({'error': '金币不足!'})

        topic_dbref = DBRef(TopicDocument.meta['collection'],
                            ObjectId(topic_id))
        liker_dbref = DBRef(UserDocument.meta['collection'],
                            ObjectId(self.current_user['_id']))

        document = {'topic': topic_dbref, 'liker': liker_dbref}

        liked = yield TopicLikeDocument.is_liked(topic_id,
                                                 self.current_user['_id'])

        if not liked and not response_data:
            now = datetime.now()
            document.update({'like_time': now})
            like_id = yield TopicLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(topic['author'].id):
                document = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'activity_type':
                    UserActivityDocument.LIKE,
                    'time':
                    now,
                    'data':
                    DBRef(TopicLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }
                activity_id = yield UserActivityDocument.insert(document)

                # 赞者
                document = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'in_out_type':
                    WealthRecordDocument.OUT,
                    'activity':
                    DBRef(UserActivityDocument.meta['collection'],
                          ObjectId(activity_id)),
                    'quantity':
                    WEALTH_SETTINGS['like'],
                    'time':
                    now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(self.current_user['_id'],
                                                 -WEALTH_SETTINGS['like'])

                # 被赞者
                document = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(topic['author'].id)),
                    'in_out_type':
                    WealthRecordDocument.IN,
                    'activity':
                    DBRef(UserActivityDocument.meta['collection'],
                          ObjectId(activity_id)),
                    'quantity':
                    WEALTH_SETTINGS['like'],
                    'time':
                    now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(topic['author'].id,
                                                 WEALTH_SETTINGS['like'])

                document = {
                    'sender':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'recipient':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(topic['author'].id)),
                    'message_type':
                    'like:topic',
                    'time':
                    now,
                    'read':
                    False,
                    'data':
                    DBRef(TopicLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }

                message_id = yield MessageDocument.insert(document)
                WriterManager.pub(MessageTopic.LIKE, message_id)

        like_times = yield TopicLikeDocument.get_like_times(topic_id)
        response_data.update({'like_times': like_times})

        self.write_json(response_data)
示例#2
0
文件: handler.py 项目: anstones/yong
    def post(self):
        form = TopicNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        title = form.title.data
        content = form.content.data
        nodes = form.nodes.data.split(',')
        anonymous = form.anonymous.data

        nodes = list(set(nodes))

        if len(nodes) > 3:
            response_data = {'error': '节点请不要超过3个!'}

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['topic_new'])
        if not can_afford:
            response_data = {'error': '金币不足!'}

        new_nodes = []
        for node in nodes:
            existed = yield NodeDocument.find_one({'name': node})
            if existed:
                node_id = existed['_id']
            else:
                node_id = yield NodeDocument.insert({'name': node})

            new_nodes.append(
                DBRef(NodeDocument.meta['collection'], ObjectId(node_id)))

        now = datetime.now()
        document = {
            'author':
            DBRef(UserDocument.meta['collection'],
                  ObjectId(self.current_user['_id'])),
            'title':
            title,
            'anonymous':
            anonymous,
            'nodes':
            new_nodes,
        }

        existed = yield TopicDocument.find_one(document)
        if existed and (now - existed['publish_time'] < timedelta(minutes=1)):
            response_data.update({'error': '你已经发布了一个相同的帖子!'})
        else:
            document.update({'publish_time': now, 'last_update_time': now})

        if not response_data:
            if content:
                document.update({'content': content})

                images = yield self.get_images(content)
                if images:
                    document.update({'images': images})

            topic_id = yield TopicDocument.insert_one(document)

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'activity_type':
                UserActivityDocument.TOPIC_NEW,
                'time':
                now,
                'data':
                DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
            }
            activity_id = yield UserActivityDocument.insert(document)

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'in_out_type':
                WealthRecordDocument.OUT,
                'activity':
                DBRef(UserActivityDocument.meta['collection'],
                      ObjectId(activity_id)),
                'quantity':
                WEALTH_SETTINGS['topic_new'],
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(self.current_user['_id'],
                                             -WEALTH_SETTINGS['topic_new'])

            if not anonymous:
                friends = yield FriendDocument.get_reached_friends(
                    self.current_user['_id'])

                message_list = []
                for friend in friends:
                    document = {
                        'sender':
                        DBRef(UserDocument.meta['collection'],
                              ObjectId(self.current_user['_id'])),
                        'recipient':
                        DBRef(UserDocument.meta['collection'],
                              ObjectId(friend['_id'])),
                        'message_type':
                        MessageTopic.TOPIC_NEW,
                        'time':
                        now,
                        'read':
                        False,
                        'data':
                        DBRef(TopicDocument.meta['collection'],
                              ObjectId(topic_id))
                    }
                    message_id = yield MessageDocument.insert(document)
                    message_list.append(str(message_id))

                if message_list:
                    try:
                        WriterManager.mpub(MessageTopic.TOPIC_NEW,
                                           message_list)
                    except:
                        pass

        self.write_json(response_data)
示例#3
0
    def post(self):
        form = ShareLikeForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}
        share_id = form.share_id.data

        share = yield ShareDocument.find_one({'_id': ObjectId(share_id)})
        if not share:
            raise HTTPError(404)

        can_afford = yield UserDocument.can_afford(self.current_user['_id'],
                                                   WEALTH_SETTINGS['like'])

        if (not can_afford and
                str(self.current_user['_id']) != str(share['uploader'].id)):
            response_data.update({'error': '金币不足!'})

        share_dbref = DBRef(ShareDocument.meta['collection'],
                            ObjectId(share_id))
        liker_dbref = DBRef(UserDocument.meta['collection'],
                            ObjectId(self.current_user['_id']))

        document = {'share': share_dbref, 'liker': liker_dbref}

        liked = yield ShareLikeDocument.is_liked(share_id,
                                                 self.current_user['_id'])
        if not liked and not response_data:
            now = datetime.now()

            document.update({'like_time': now})
            like_id = yield ShareLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(share['uploader'].id):
                activity = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'activity_type':
                    UserActivityDocument.LIKE,
                    'time':
                    now,
                    'data':
                    DBRef(ShareLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }
                activity_id = yield UserActivityDocument.insert(activity)

                # 赞者
                wealth = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'in_out_type':
                    WealthRecordDocument.OUT,
                    'activity':
                    DBRef(UserActivityDocument.meta['collection'],
                          ObjectId(activity_id)),
                    'quantity':
                    WEALTH_SETTINGS['like'],
                    'time':
                    now
                }
                yield WealthRecordDocument.insert(wealth)
                yield UserDocument.update_wealth(self.current_user['_id'],
                                                 -WEALTH_SETTINGS['like'])

                # 被赞者
                wealth = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(share['uploader'].id)),
                    'in_out_type':
                    WealthRecordDocument.IN,
                    'activity':
                    DBRef(UserActivityDocument.meta['collection'],
                          ObjectId(activity_id)),
                    'quantity':
                    WEALTH_SETTINGS['like'],
                    'time':
                    now
                }
                yield WealthRecordDocument.insert(wealth)
                yield UserDocument.update_wealth(share['uploader'].id,
                                                 WEALTH_SETTINGS['like'])

                message = {
                    'sender':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'recipient':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(share['uploader'].id)),
                    'message_type':
                    'like:share',
                    'time':
                    now,
                    'read':
                    False,
                    'data':
                    DBRef(ShareLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }

                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(MessageTopic.LIKE, str(message_id))

        like_times = yield ShareLikeDocument.get_like_times(share_id)
        response_data.update({'like_times': like_times})

        self.finish(json.dumps(response_data))
示例#4
0
    def post(self):
        form = ShareLikeForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}
        share_id = form.share_id.data

        share = yield ShareDocument.find_one({
            '_id': ObjectId(share_id)
        })
        if not share:
            raise HTTPError(404)

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['like']
        )

        if (not can_afford and
                str(self.current_user['_id']) != str(share['uploader'].id)):
            response_data.update({'error': '金币不足!'})

        share_dbref = DBRef(
            ShareDocument.meta['collection'],
            ObjectId(share_id)
        )
        liker_dbref = DBRef(
            UserDocument.meta['collection'],
            ObjectId(self.current_user['_id'])
        )

        document = {
            'share': share_dbref,
            'liker': liker_dbref
        }

        liked = yield ShareLikeDocument.is_liked(
            share_id, self.current_user['_id']
        )
        if not liked and not response_data:
            now = datetime.now()

            document.update({
                'like_time': now
            })
            like_id = yield ShareLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(share['uploader'].id):
                activity = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'activity_type': UserActivityDocument.LIKE,
                    'time': now,
                    'data': DBRef(
                        ShareLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }
                activity_id = yield UserActivityDocument.insert(activity)

                # 赞者
                wealth = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'in_out_type': WealthRecordDocument.OUT,
                    'activity': DBRef(
                        UserActivityDocument.meta['collection'],
                        ObjectId(activity_id)
                    ),
                    'quantity': WEALTH_SETTINGS['like'],
                    'time': now
                }
                yield WealthRecordDocument.insert(wealth)
                yield UserDocument.update_wealth(
                    self.current_user['_id'], -WEALTH_SETTINGS['like']
                )

                # 被赞者
                wealth = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(share['uploader'].id)
                    ),
                    'in_out_type': WealthRecordDocument.IN,
                    'activity': DBRef(
                        UserActivityDocument.meta['collection'],
                        ObjectId(activity_id)
                    ),
                    'quantity': WEALTH_SETTINGS['like'],
                    'time': now
                }
                yield WealthRecordDocument.insert(wealth)
                yield UserDocument.update_wealth(
                    share['uploader'].id, WEALTH_SETTINGS['like']
                )

                message = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(share['uploader'].id)
                    ),
                    'message_type': 'like:share',
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        ShareLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }

                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(MessageTopic.LIKE, str(message_id))

        like_times = yield ShareLikeDocument.get_like_times(share_id)
        response_data.update({'like_times': like_times})

        self.write_json(response_data)
示例#5
0
    def post(self):
        response_data = {}

        form = TopicLikeForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        topic_id = form.topic_id.data

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if not topic:
            raise HTTPError(404)

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['like']
        )

        if not can_afford and str(
                self.current_user['_id']) != str(topic['author'].id):
            response_data.update({'error': '金币不足!'})

        topic_dbref = DBRef(
            TopicDocument.meta['collection'],
            ObjectId(topic_id)
        )
        liker_dbref = DBRef(
            UserDocument.meta['collection'],
            ObjectId(self.current_user['_id'])
        )

        document = {'topic': topic_dbref, 'liker': liker_dbref}

        liked = yield TopicLikeDocument.is_liked(
            topic_id, self.current_user['_id']
        )

        if not liked and not response_data:
            now = datetime.now()
            document.update({'like_time': now})
            like_id = yield TopicLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(topic['author'].id):
                document = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'activity_type': UserActivityDocument.LIKE,
                    'time': now,
                    'data': DBRef(
                        TopicLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }
                activity_id = yield UserActivityDocument.insert(document)

                # 赞者
                document = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'in_out_type': WealthRecordDocument.OUT,
                    'activity': DBRef(
                        UserActivityDocument.meta['collection'],
                        ObjectId(activity_id)
                    ),
                    'quantity': WEALTH_SETTINGS['like'],
                    'time': now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(
                    self.current_user['_id'], -WEALTH_SETTINGS['like']
                )

                # 被赞者
                document = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(topic['author'].id)
                    ),
                    'in_out_type': WealthRecordDocument.IN,
                    'activity': DBRef(
                        UserActivityDocument.meta['collection'],
                        ObjectId(activity_id)
                    ),
                    'quantity': WEALTH_SETTINGS['like'],
                    'time': now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(
                    topic['author'].id, WEALTH_SETTINGS['like']
                )

                document = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(topic['author'].id)
                    ),
                    'message_type': 'like:topic',
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        TopicLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }

                message_id = yield MessageDocument.insert(document)
                WriterManager.pub(MessageTopic.LIKE, message_id)

        like_times = yield TopicLikeDocument.get_like_times(topic_id)
        response_data.update({'like_times': like_times})

        self.write_json(response_data)
示例#6
0
    def post(self):
        form = TopicNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        title = form.title.data
        content = form.content.data
        nodes = form.nodes.data.split(',')
        anonymous = form.anonymous.data

        nodes = list(set(nodes))

        if len(nodes) > 3:
            response_data = {'error': '节点请不要超过3个!'}

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['topic_new']
        )
        if not can_afford:
            response_data = {'error': '金币不足!'}

        new_nodes = []
        for node in nodes:
            existed = yield NodeDocument.find_one({'name': node})
            if existed:
                node_id = existed['_id']
            else:
                node_id = yield NodeDocument.insert({'name': node})

            new_nodes.append(
                DBRef(NodeDocument.meta['collection'], ObjectId(node_id))
            )

        now = datetime.now()
        document = {
            'author': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'title': title,
            'anonymous': anonymous,
            'nodes': new_nodes,
        }

        existed = yield TopicDocument.find_one(document)
        if existed and (now - existed['publish_time'] < timedelta(minutes=1)):
            response_data.update({'error': '你已经发布了一个相同的帖子!'})
        else:
            document.update({'publish_time': now, 'last_update_time': now})

        if not response_data:
            if content:
                document.update({'content': content})

                images = yield self.get_images(content)
                if images:
                    document.update({'images': images})

            topic_id = yield TopicDocument.insert_one(document)

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity_type': UserActivityDocument.TOPIC_NEW,
                'time': now,
                'data': DBRef(
                    TopicDocument.meta['collection'], ObjectId(topic_id)
                )
            }
            activity_id = yield UserActivityDocument.insert(document)

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'in_out_type': WealthRecordDocument.OUT,
                'activity': DBRef(
                    UserActivityDocument.meta['collection'],
                    ObjectId(activity_id)
                ),
                'quantity': WEALTH_SETTINGS['topic_new'],
                'time': now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(
                self.current_user['_id'], -WEALTH_SETTINGS['topic_new']
            )

            if not anonymous:
                friends = yield FriendDocument.get_reached_friends(
                    self.current_user['_id']
                )

                message_list = []
                for friend in friends:
                    document = {
                        'sender': DBRef(
                            UserDocument.meta['collection'],
                            ObjectId(self.current_user['_id'])
                        ),
                        'recipient': DBRef(
                            UserDocument.meta['collection'],
                            ObjectId(friend['_id'])
                        ),
                        'message_type': MessageTopic.TOPIC_NEW,
                        'time': now,
                        'read': False,
                        'data': DBRef(
                            TopicDocument.meta['collection'],
                            ObjectId(topic_id)
                        )
                    }
                    message_id = yield MessageDocument.insert(document)
                    message_list.append(str(message_id))

                if message_list:
                    try:
                        WriterManager.mpub(MessageTopic.TOPIC_NEW, message_list)
                    except:
                        pass

        self.write_json(response_data)