def get(self, topic_id): topic = yield TopicDocument.get_topic( topic_id, self.current_user and self.current_user['_id']) if not topic: raise HTTPError(404) if self.current_user and topic['author']['_id'] != self.current_user[ '_id']: yield TopicDocument.update({'_id': topic['_id']}, {'$inc': { 'read_times': 1 }}) comment_list = yield TopicCommentDocument.get_comment_list( topic['_id'], limit=COMMUNITY_SETTINGS['topic_comment_number_per_page']) recommend_topic_list = yield TopicDocument.get_recommend_topic_list( topic_id) like_list = yield TopicLikeDocument.get_like_list(topic_id) kwargs = { 'topic': topic, 'comment_list': comment_list, 'like_list': like_list, 'recommend_topic_list': recommend_topic_list, 'COMMUNITY_SETTINGS': COMMUNITY_SETTINGS } self.render('community/template/topic-one.html', **kwargs)
def get(self, topic_id): topic = yield TopicDocument.get_topic( topic_id, self.current_user and self.current_user['_id'] ) if not topic: raise HTTPError(404) if self.current_user and topic['author']['_id'] != self.current_user['_id']: yield TopicDocument.update( {'_id': topic['_id']}, {'$inc': {'read_times': 1}} ) comment_list = yield TopicCommentDocument.get_comment_list( topic['_id'], limit=COMMUNITY_SETTINGS['topic_comment_number_per_page'] ) recommend_topic_list = yield TopicDocument.get_recommend_topic_list( topic_id ) like_list = yield TopicLikeDocument.get_like_list(topic_id) kwargs = { 'topic': topic, 'comment_list': comment_list, 'like_list': like_list, 'recommend_topic_list': recommend_topic_list, 'COMMUNITY_SETTINGS': COMMUNITY_SETTINGS } self.render('community/template/topic-one.html', **kwargs)
def post(self): form = TopicCommentMoreForm(self.request.arguments) if not form.validate(): raise HTTPError(404) page = form.page.data topic_id = form.topic_id.data skip = COMMUNITY_SETTINGS['topic_comment_number_per_page'] * page limit = COMMUNITY_SETTINGS['topic_comment_number_per_page'] comment_list = yield TopicCommentDocument.get_comment_list( topic_id, skip, limit) html = ''.join( self.render_string( 'community/template/topic-comment-list-item.html', comment=comment) for comment in comment_list) self.write_json({'html': html, 'page': page + 1})
def post(self): form = TopicCommentMoreForm(self.request.arguments) if not form.validate(): raise HTTPError(404) page = form.page.data topic_id = form.topic_id.data skip = COMMUNITY_SETTINGS['topic_comment_number_per_page'] * page limit = COMMUNITY_SETTINGS['topic_comment_number_per_page'] comment_list = yield TopicCommentDocument.get_comment_list( topic_id, skip, limit ) html = ''.join( self.render_string( 'community/template/topic-comment-list-item.html', comment=comment ) for comment in comment_list ) self.write_json({'html': html, 'page': page + 1})
def get(self, comment_id): yield TopicCommentDocument.delete_one(comment_id) self.redirect('/community')
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)
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)