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 get(self): sort = self.get_argument('sort', "time") if sort not in ['time', 'popularity']: sort = 'time' node = self.get_argument('node', None) try: page = max(int(self.get_argument("page", 1)), 1) except: page = 1 query = { 'user_id': self.current_user and self.current_user['_id'], 'sort': sort, 'skip': (page - 1) * COMMUNITY_SETTINGS["topic_number_per_page"], 'limit': COMMUNITY_SETTINGS['topic_number_per_page'] } if node: node = yield NodeDocument.get_node(node) if not node: raise HTTPError(404) query.update({'node_id': node['_id']}) topic_list = yield TopicDocument.get_topic_list(**query) hot_topic_list = yield TopicDocument.get_hot_topic_list( period=timedelta(days=1), limit=COMMUNITY_SETTINGS['hot_topic_number'] ) hot_node_list = yield NodeDocument.get_hot_node_list( size=COMMUNITY_SETTINGS['hot_node_number'] ) top_header_node_list = yield NodeDocument.get_top_header_node_list() total_page, pages = self.paginate( (yield TopicDocument.get_topic_number(node and node['_id'])), COMMUNITY_SETTINGS['topic_number_per_page'], page ) kwargs = {} if self.current_user: kwargs = yield self.get_sidebar_arguments() kwargs.update({ 'sort': sort, 'current_node': node, 'topic_list': topic_list, 'hot_topic_list': hot_topic_list, 'top_header_node_list': top_header_node_list, 'hot_node_list': hot_node_list, 'page': page, 'total_page': total_page, 'pages': pages }) self.render('community/template/community.html', **kwargs)
def post(self): form = TopicEditForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} topic_id = form.topic_id.data title = form.title.data content = form.content.data nodes = form.nodes.data.split(',') anonymous = form.anonymous.data topic = yield TopicDocument.get_topic( topic_id, self.current_user['_id'] ) if (not topic or len(nodes) > 3 or topic['author']['_id'] != self.current_user['_id']): raise HTTPError(404) nodes = list(set(nodes)) node_ids = [] 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}) node_ids.append( DBRef(NodeDocument.meta['collection'], ObjectId(node_id)) ) document = { 'author': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'title': title, 'anonymous': anonymous, 'nodes': node_ids, 'content': content } images = yield self.get_images(content) document.update({'images': images}) topic_id = yield TopicDocument.update( {'_id': ObjectId(topic_id)}, {'$set': document} ) self.write_json(response_data)
def post(self): form = TopicEditForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} topic_id = form.topic_id.data title = form.title.data content = form.content.data nodes = form.nodes.data.split(',') anonymous = form.anonymous.data topic = yield TopicDocument.get_topic(topic_id, self.current_user['_id']) if (not topic or len(nodes) > 3 or topic['author']['_id'] != self.current_user['_id']): raise HTTPError(404) nodes = list(set(nodes)) node_ids = [] 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}) node_ids.append( DBRef(NodeDocument.meta['collection'], ObjectId(node_id))) document = { 'author': DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])), 'title': title, 'anonymous': anonymous, 'nodes': node_ids, 'content': content } images = yield self.get_images(content) document.update({'images': images}) topic_id = yield TopicDocument.update({'_id': ObjectId(topic_id)}, {'$set': document}) self.write_json(response_data)
def get(self): sort = self.get_argument('sort', "time") if sort not in ['time', 'popularity']: sort = 'time' node = self.get_argument('node', None) try: page = max(int(self.get_argument("page", 1)), 1) except: page = 1 query = { 'user_id': self.current_user and self.current_user['_id'], 'sort': sort, 'skip': (page - 1) * COMMUNITY_SETTINGS["topic_number_per_page"], 'limit': COMMUNITY_SETTINGS['topic_number_per_page'] } if node: node = yield NodeDocument.get_node(node) if not node: raise HTTPError(404) query.update({'node_id': node['_id']}) topic_list = yield TopicDocument.get_topic_list(**query) hot_topic_list = yield TopicDocument.get_hot_topic_list( period=timedelta(days=1), limit=COMMUNITY_SETTINGS['hot_topic_number']) hot_node_list = yield NodeDocument.get_hot_node_list( size=COMMUNITY_SETTINGS['hot_node_number']) top_header_node_list = yield NodeDocument.get_top_header_node_list() total_page, pages = self.paginate( (yield TopicDocument.get_topic_number(node and node['_id'])), COMMUNITY_SETTINGS['topic_number_per_page'], page) kwargs = {} if self.current_user: kwargs = yield self.get_sidebar_arguments() kwargs.update({ 'sort': sort, 'current_node': node, 'topic_list': topic_list, 'hot_topic_list': hot_topic_list, 'top_header_node_list': top_header_node_list, 'hot_node_list': hot_node_list, 'page': page, 'total_page': total_page, 'pages': pages }) self.render('community/template/community.html', **kwargs)
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) user_setting = yield UserSettingDocument.find_one({ 'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id)) }) random_user_list = yield UserDocument.get_random_user_list( self.current_user['_id'] ) kwargs = { 'status_number': status_number, 'topic_number': topic_number, 'MessageTopic': MessageTopic, 'user_setting': user_setting, 'random_user_list': random_user_list, 'HOME_SETTINGS': HOME_SETTINGS } raise gen.Return(kwargs)
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)
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)
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) user_setting = yield UserSettingDocument.find_one({ 'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id)) }) random_user_list = yield UserDocument.get_random_user_list( self.current_user['_id'] ) kwargs = { 'status_number': status_number, 'topic_number': topic_number, 'MessageTopic': MessageTopic, 'user_setting': user_setting, 'random_user_list': random_user_list, 'HOME_SETTINGS': HOME_SETTINGS } raise gen.Return(kwargs)
def get(self, node_id): current_node = yield NodeDocument.get_node(node_id) if not current_node: raise HTTPError(404) sort = self.get_argument('sort', "time") if sort not in ['time', 'popularity']: sort = 'time' try: page = max(int(self.get_argument("page", 1)), 1) except: page = 1 node_avatar_url = yield NodeAvatarDocument.get_node_avatar_url( node_id ) topic_list = yield TopicDocument.get_topic_list( node_id=node_id, user_id=self.current_user and self.current_user['_id'], sort=sort, skip=(page - 1) * COMMUNITY_SETTINGS["topic_number_per_page"], limit=COMMUNITY_SETTINGS['topic_number_per_page'] ) active_author_list = yield TopicStatisticsDocument.get_active_author_list( node_id=node_id, period=timedelta(days=100), limit=10 ) total_page, pages = self.paginate( (yield TopicDocument.get_topic_number(current_node["_id"])), COMMUNITY_SETTINGS['topic_number_per_page'], page ) kwargs = { 'current_node': current_node, 'node_avatar_url': node_avatar_url, 'sort': sort, 'topic_list': topic_list, 'active_author_list': active_author_list, 'page': page, 'total_page': total_page, 'pages': pages } self.render('community/template/node-one.html', **kwargs)
def get_header_arguments(self, user_id=None): user_id = user_id or self.current_user["_id"] user = yield UserDocument.find_one({ '_id': ObjectId(user_id) }) if not user: raise HTTPError(404) status_number = yield StatusDocument.get_status_number(user_id) topic_number = yield TopicDocument.get_topic_number_by_someone( user_id, visitor_id=self.current_user['_id'] ) user = yield UserDocument.translate_dbref_in_document(user) can_seen = yield UserDocument.can_seen( user_id, self.current_user['_id'] ) is_friend = yield FriendDocument.is_friend( user_id, self.current_user['_id'] ) user_setting = yield UserSettingDocument.get_user_setting( user_id ) profile_cover = yield UserSettingDocument.get_profile_cover( user_id ) kwargs = { 'user': user, 'can_seen': can_seen, 'is_friend': is_friend, 'user_setting': user_setting, 'status_number': status_number, 'topic_number': topic_number, 'profile_cover': profile_cover, 'PROFILE_SETTINGS': PROFILE_SETTINGS } if not can_seen: messages_func = LeaveMessageDocument.get_leave_message_list leave_message_list = yield messages_func( user_id, self.current_user['_id'], limit=PROFILE_SETTINGS['leave_message_number_per_page'] ) kwargs.update({ 'leave_message_list': leave_message_list }) raise gen.Return(kwargs)
def get(self, node_id): current_node = yield NodeDocument.get_node(node_id) if not current_node: raise HTTPError(404) sort = self.get_argument('sort', "time") if sort not in ['time', 'popularity']: sort = 'time' try: page = max(int(self.get_argument("page", 1)), 1) except: page = 1 node_avatar_url = yield NodeAvatarDocument.get_node_avatar_url(node_id) topic_list = yield TopicDocument.get_topic_list( node_id=node_id, user_id=self.current_user and self.current_user['_id'], sort=sort, skip=(page - 1) * COMMUNITY_SETTINGS["topic_number_per_page"], limit=COMMUNITY_SETTINGS['topic_number_per_page']) active_author_list = yield TopicStatisticsDocument.get_active_author_list( node_id=node_id, period=timedelta(days=100), limit=10) total_page, pages = self.paginate( (yield TopicDocument.get_topic_number(current_node["_id"])), COMMUNITY_SETTINGS['topic_number_per_page'], page) kwargs = { 'current_node': current_node, 'node_avatar_url': node_avatar_url, 'sort': sort, 'topic_list': topic_list, 'active_author_list': active_author_list, 'page': page, 'total_page': total_page, 'pages': pages } self.render('community/template/node-one.html', **kwargs)
def get(self): topic_id = self.get_argument('topic_id', None) if not topic_id: raise HTTPError(404) topic = yield TopicDocument.get_topic(topic_id, self.current_user['_id']) if not topic or topic['author']['_id'] != self.current_user['_id']: raise HTTPError(404) self.render('community/template/topic-new.html', action="edit", topic=topic)
def get(self): topic_id = self.get_argument('topic_id', None) if not topic_id: raise HTTPError(404) topic = yield TopicDocument.get_topic( topic_id, self.current_user['_id'] ) if not topic or topic['author']['_id'] != self.current_user['_id']: raise HTTPError(404) self.render( 'community/template/topic-new.html', action="edit", topic=topic )
def get_header_arguments(self, user_id=None): user_id = user_id or self.current_user["_id"] user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user: raise HTTPError(404) status_number = yield StatusDocument.get_status_number(user_id) topic_number = yield TopicDocument.get_topic_number_by_someone( user_id, visitor_id=self.current_user['_id']) user = yield UserDocument.translate_dbref_in_document(user) can_seen = yield UserDocument.can_seen(user_id, self.current_user['_id']) is_friend = yield FriendDocument.is_friend(user_id, self.current_user['_id']) user_setting = yield UserSettingDocument.get_user_setting(user_id) profile_cover = yield UserSettingDocument.get_profile_cover(user_id) kwargs = { 'user': user, 'can_seen': can_seen, 'is_friend': is_friend, 'user_setting': user_setting, 'status_number': status_number, 'topic_number': topic_number, 'profile_cover': profile_cover, 'PROFILE_SETTINGS': PROFILE_SETTINGS } if not can_seen: messages_func = LeaveMessageDocument.get_leave_message_list leave_message_list = yield messages_func( user_id, self.current_user['_id'], limit=PROFILE_SETTINGS['leave_message_number_per_page']) kwargs.update({'leave_message_list': leave_message_list}) raise gen.Return(kwargs)
def get(self, user_id=None): if user_id is None: user_id = self.current_user['_id'] kwargs = yield self.get_header_arguments(user_id) if not kwargs['can_seen']: self.render('profile/template/profile-visitor.html', **kwargs) else: recommend_topic_list = [] topic_list = yield TopicDocument.get_topic_list_by_someone(user_id) if topic_list: index = random.randint(0, len(topic_list) - 1) topic_list_func = TopicDocument.get_recommend_topic_list recommend_topic_list = yield topic_list_func( topic_list[index]['_id']) kwargs.update({ 'topic_list': topic_list, 'recommend_topic_list': recommend_topic_list }) self.render('profile/template/topic/topic.html', **kwargs)
def get(self, user_id=None): if user_id is None: user_id = self.current_user['_id'] kwargs = yield self.get_header_arguments(user_id) if not kwargs['can_seen']: self.render('profile/template/profile-visitor.html', **kwargs) else: recommend_topic_list = [] topic_list = yield TopicDocument.get_topic_list_by_someone(user_id) if topic_list: index = random.randint(0, len(topic_list) - 1) topic_list_func = TopicDocument.get_recommend_topic_list recommend_topic_list = yield topic_list_func( topic_list[index]['_id'] ) kwargs.update({ 'topic_list': topic_list, 'recommend_topic_list': recommend_topic_list }) self.render('profile/template/topic/topic.html', **kwargs)
def get(self, topic_id): yield TopicDocument.delete_one(topic_id) self.redirect('/community')
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)
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)
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)
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)
def get(self, topic_id): yield TopicDocument.delete_one(topic_id) self.redirect('/community')
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)