示例#1
0
    def post(self):
        response_data = {}

        login_reward_fetched = yield UserActivityDocument.login_reward_fetched(
            self.current_user['_id']
        )

        if login_reward_fetched:
            response_data.update({'error': '你已经领取了今日的登录奖励!'})
        else:
            now = datetime.now()

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

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id']
            )
            quantity = (1 + continuous_login_days / 7) * 5

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

            yield UserDocument.update_wealth(
                self.current_user['_id'], quantity
            )
            yield UserDocument.update(
                {'_id': ObjectId(self.current_user['_id'])},
                {'$inc': {'continuous_login_days': 1}}
            )

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id']
            )

            response_data.update({
                'wealth': self.current_user['wealth'] + quantity,
                'continuous_login_days': continuous_login_days
            })

        self.write_json(response_data)
示例#2
0
    def get_sidebar_arguments(self):
        '''得到两侧栏的render变量'''

        user_id = self.current_user['_id']

        status_number = yield StatusDocument.get_status_number(user_id)
        topic_number = yield TopicDocument.get_topic_number_by_someone(
            user_id, visitor_id=user_id
        )

        user_setting = yield UserSettingDocument.find_one({
            'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        })
        login_reward_fetched_today = yield UserActivityDocument.login_reward_fetched(
            user_id
        )
        continuous_login_days = yield UserDocument.get_continuous_login_days(
            user_id
        )

        kwargs = {
            'status_number': status_number,
            'topic_number': topic_number,
            'MessageTopic': MessageTopic,
            'user_setting': user_setting,
            'login_reward_fetched_today': login_reward_fetched_today,
            'continuous_login_days': continuous_login_days,
            'HOME_SETTINGS': HOME_SETTINGS
        }

        raise gen.Return(kwargs)
示例#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.write_json(response_data)
示例#4
0
    def post(self):
        form = ShareCommentNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        content = form.content.data
        share_id = form.share_id.data
        anonymous = form.anonymous.data
        replyeder_id = form.replyeder_id.data

        replyeder = None
        if replyeder_id:
            replyeder = yield UserDocument.find_one({
                '_id': ObjectId(replyeder_id)
            })
            if (not replyeder or
                    anonymous or
                    self.current_user['_id'] == replyeder['_id']):
                raise HTTPError(404)

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

        if not response_data:
            now = datetime.now()

            document = {
                'author': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'share': DBRef(
                    ShareDocument.meta['collection'],
                    ObjectId(share['_id'])
                ),
                'comment_time': now,
                'content': content,
                'anonymous': anonymous
            }

            if replyeder:
                document.update({
                    'replyeder': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(replyeder_id)
                    )
                })

            comment_id = yield ShareCommentDocument.insert_one(document)

            activity = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity_type': UserActivityDocument.COMMENT,
                'time': now,
                'data': DBRef(
                    ShareCommentDocument.meta['collection'],
                    ObjectId(comment_id)
                )
            }
            yield UserActivityDocument.insert(activity)

            if replyeder:
                recipient_id = replyeder_id
                message_type = 'reply:share'
                message_share = MessageTopic.REPLY
            else:
                recipient_id = share['uploader'].id
                message_type = 'comment:share'
                message_share = MessageTopic.COMMENT

            if (str(self.current_user['_id']) != str(recipient_id) and
                    not anonymous):
                message = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(recipient_id)
                    ),
                    'message_type': message_type,
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        ShareCommentDocument.meta['collection'],
                        ObjectId(comment_id)
                    )
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_share, message_id)

            comment_times = yield ShareCommentDocument.get_comment_times(
                share_id
            )

            document.update({
                '_id': ObjectId(comment_id),
                'author': self.current_user,
                'floor': comment_times
            })

            if replyeder:
                document.update({'replyeder': replyeder})

            item = self.render_string(
                'share/template/share-comment-list-item.html',
                comment=document
            )
            response_data.update({'item': item})

        self.write_json(response_data)
示例#5
0
    def get(self, share_id):
        share = yield ShareDocument.get_share(share_id)

        if not share or not share['passed'] or 'origin_file' not in share:
            raise HTTPError(404)

        self.set_header('Content-Type', self.get_content_type(share))
        self.set_header(
            'Content-Disposition',
            'attachment; filename=%s' % self.get_filename(share)
        )

        fs = self.get_gridfs()
        gridout = yield fs.get(ObjectId(share['origin_file']))
        if gridout.length <= 0:
            raise HTTPError(404)

        if (self.current_user['_id'] != share['uploader']['_id']
                and self.current_user['wealth'] < share['cost']):
            raise HTTPError(404)

        size = 0
        while size < gridout.length:
            content = yield gridout.read(gridout.chunk_size)
            size += len(content)
            self.write(content)

        yield ShareDocument.update(
            {'_id': ObjectId(share_id)},
            {'$inc': {'download_times': 1}}
        )

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

        document = {
            'share': DBRef(
                ShareDocument.meta['collection'],
                ObjectId(share_id)
            ),
            'downloader': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'download_time': now
        }
        yield ShareDownloadDocument.insert(document)

        if (share['cost'] > 0 and
                self.current_user['_id'] != share['uploader']['_id']):
            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': share['cost'],
                'time': now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(
                self.current_user['_id'], -share['cost']
            )

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(share['uploader']['_id'])
                ),
                'in_out_type': WealthRecordDocument.IN,
                'activity': DBRef(
                    UserActivityDocument.meta['collection'],
                    ObjectId(activity_id)
                ),
                'quantity': share['cost'],
                'time': now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(
                share['uploader']['_id'], share['cost']
            )

        self.finish()
示例#6
0
文件: handler.py 项目: xxoxx/Young
    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))
示例#7
0
文件: handler.py 项目: xxoxx/Young
    def post(self):
        form = ShareCommentNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        content = form.content.data
        share_id = form.share_id.data
        anonymous = form.anonymous.data
        replyeder_id = form.replyeder_id.data

        replyeder = None
        if replyeder_id:
            replyeder = yield UserDocument.find_one(
                {'_id': ObjectId(replyeder_id)})
            if (not replyeder or anonymous
                    or self.current_user['_id'] == replyeder['_id']):
                raise HTTPError(404)

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

        if not response_data:
            now = datetime.now()

            document = {
                'author':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'share':
                DBRef(ShareDocument.meta['collection'],
                      ObjectId(share['_id'])),
                'comment_time':
                now,
                'content':
                content,
                'anonymous':
                anonymous
            }

            if replyeder:
                document.update({
                    'replyeder':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(replyeder_id))
                })

            comment_id = yield ShareCommentDocument.insert_one(document)

            activity = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'activity_type':
                UserActivityDocument.COMMENT,
                'time':
                now,
                'data':
                DBRef(ShareCommentDocument.meta['collection'],
                      ObjectId(comment_id))
            }
            yield UserActivityDocument.insert(activity)

            if replyeder:
                recipient_id = replyeder_id
                message_type = 'reply:share'
                message_share = MessageTopic.REPLY
            else:
                recipient_id = share['uploader'].id
                message_type = 'comment:share'
                message_share = MessageTopic.COMMENT

            if (str(self.current_user['_id']) != str(recipient_id)
                    and not anonymous):
                message = {
                    'sender':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'recipient':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(recipient_id)),
                    'message_type':
                    message_type,
                    'time':
                    now,
                    'read':
                    False,
                    'data':
                    DBRef(ShareCommentDocument.meta['collection'],
                          ObjectId(comment_id))
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_share, message_id)

            comment_times = yield ShareCommentDocument.get_comment_times(
                share_id)

            document.update({
                '_id': ObjectId(comment_id),
                'author': self.current_user,
                'floor': comment_times
            })

            if replyeder:
                document.update({'replyeder': replyeder})

            item = self.render_string(
                'share/template/share-comment-list-item.html',
                comment=document)
            response_data.update({'item': item})

        self.finish(json.dumps(response_data))
示例#8
0
文件: handler.py 项目: xxoxx/Young
    def get(self, share_id):
        share = yield ShareDocument.get_share(share_id)

        if not share or not share['passed'] or 'origin_file' not in share:
            raise HTTPError(404)

        self.set_header('Content-Type', self.get_content_type(share))
        self.set_header('Content-Disposition',
                        'attachment; filename=%s' % self.get_filename(share))

        fs = self.get_gridfs()
        gridout = yield fs.get(ObjectId(share['origin_file']))
        if gridout.length <= 0:
            raise HTTPError(404)

        if (self.current_user['_id'] != share['uploader']['_id']
                and self.current_user['wealth'] < share['cost']):
            raise HTTPError(404)

        size = 0
        while size < gridout.length:
            content = yield gridout.read(gridout.chunk_size)
            size += len(content)
            self.write(content)

        yield ShareDocument.update({'_id': ObjectId(share_id)},
                                   {'$inc': {
                                       'download_times': 1
                                   }})

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

        document = {
            'share':
            DBRef(ShareDocument.meta['collection'], ObjectId(share_id)),
            'downloader':
            DBRef(UserDocument.meta['collection'],
                  ObjectId(self.current_user['_id'])),
            'download_time':
            now
        }
        yield ShareDownloadDocument.insert(document)

        if (share['cost'] > 0
                and self.current_user['_id'] != share['uploader']['_id']):
            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':
                share['cost'],
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(self.current_user['_id'],
                                             -share['cost'])

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(share['uploader']['_id'])),
                'in_out_type':
                WealthRecordDocument.IN,
                'activity':
                DBRef(UserActivityDocument.meta['collection'],
                      ObjectId(activity_id)),
                'quantity':
                share['cost'],
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(share['uploader']['_id'],
                                             share['cost'])

        self.finish()
示例#9
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)
示例#10
0
    def post(self):
        form = TopicCommentNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        content = form.content.data
        topic_id = form.topic_id.data
        anonymous = form.anonymous.data
        replyeder_id = form.replyeder_id.data

        replyeder = None
        if replyeder_id:
            replyeder = yield UserDocument.find_one({
                '_id': ObjectId(replyeder_id)
            })
            if not replyeder:
                raise HTTPError(404)

            if anonymous or self.current_user['_id'] == replyeder['_id']:
                raise HTTPError(404)

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

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

        existed = yield TopicCommentDocument.find_one(document)
        if existed and (now - existed['comment_time'] < timedelta(minutes=1)):
            response_data.update({'error': '请不要重复评论!'})
        else:
            document.update({'comment_time': now})

        if not response_data:
            if replyeder:
                document.update({
                    'replyeder': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(replyeder_id)
                    )
                })

            comment_id = yield TopicCommentDocument.insert_one(document)

            activity = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity_type': UserActivityDocument.COMMENT,
                'time': now,
                'data': DBRef(
                    TopicCommentDocument.meta['collection'],
                    ObjectId(comment_id)
                )
            }
            yield UserActivityDocument.insert(activity)

            if replyeder:
                recipient_id = replyeder_id
                message_type = 'reply:topic'
                message_topic = MessageTopic.REPLY
            else:
                recipient_id = topic['author'].id
                message_type = 'comment:topic'
                message_topic = MessageTopic.COMMENT

            if (str(self.current_user['_id']) != str(recipient_id) and
                    not anonymous and
                    not (message_topic == MessageTopic.COMMENT and
                         topic['anonymous'])):
                message = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(recipient_id)
                    ),
                    'message_type': message_type,
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        TopicCommentDocument.meta['collection'],
                        ObjectId(comment_id)
                    )
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_topic, message_id)

            comment_times = yield TopicCommentDocument.get_comment_times(
                topic_id
            )

            document.update({
                '_id': ObjectId(comment_id),
                'author': self.current_user,
                'floor': comment_times
            })

            if replyeder:
                document.update({'replyeder': replyeder})

            item = self.render_string(
                'community/template/topic-comment-list-item.html',
                comment=document
            )
            response_data.update({'item': item})

        self.write_json(response_data)
示例#11
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)