def post(self): form = PasswordResetSendmailForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} email = form.email.data user = yield UserDocument.find_one({'email': email}) if not user: raise HTTPError(404) if not user['activated']: response_data.update({'error': '该账号尚未激活, 请先激活账号!'}) else: document = { 'uid': user["_id"], 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta( days=USER_SETTINGS['code_expired_after'] ) } yield CodeDocument.remove({"uid": user["_id"]}, multi=True) code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_RESET_PASSWORD_EMAIL, code_id) self.write_json(response_data)
def post(self): form = PasswordResetSendmailForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} email = form.email.data user = yield UserDocument.find_one({'email': email}) if not user: raise HTTPError(404) if not user['activated']: response_data.update({'error': '该账号尚未激活, 请先激活账号!'}) else: document = { 'uid': user["_id"], 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta(days=USER_SETTINGS['code_expired_after']) } yield CodeDocument.remove({"uid": user["_id"]}, multi=True) code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_RESET_PASSWORD_EMAIL, code_id) self.finish(json.dumps(response_data))
def post(self): form = AccountActiveSendmailForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} email = form.email.data user = yield UserDocument.find_one({'email': email}) if not user: raise HTTPError(404) if user['activated']: response_data.update({'error': '该账号已经激活!'}) else: document = { 'uid': user["_id"], 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta(days=USER_SETTINGS['code_expired_after']) } yield CodeDocument.remove({"uid": user["_id"]}, multi=True) code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_ACTIVATION_EMAIL, code_id) self.write_json(response_data)
def post(self): '''点赞''' form = StatusLikeForm(self.request.arguments) if not form.validate(): raise HTTPError(404) status_id = form.status_id.data status = yield StatusDocument.find_one({'_id': ObjectId(status_id)}) if not status: raise HTTPError(404) status_dbref = DBRef(StatusDocument.meta['collection'], ObjectId(status_id)) liker_dbref = DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])) document = {'status': status_dbref, 'liker': liker_dbref} liked = yield StatusLikeDocument.is_liked(status_id, self.current_user['_id']) if not liked: now = datetime.now() document.update({'like_time': now}) like_id = yield StatusLikeDocument.insert_one(document) if str(self.current_user['_id']) != str(status['author'].id): document = { 'sender': DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])), 'recipient': DBRef(UserDocument.meta['collection'], ObjectId(status['author'].id)), 'message_type': 'like:status', 'time': now, 'read': False, 'data': DBRef(StatusLikeDocument.meta['collection'], ObjectId(like_id)) } message_id = yield MessageDocument.insert(document) WriterManager.pub(MessageTopic.LIKE, message_id) like_times = yield StatusLikeDocument.get_like_times(status_id) like_list = yield StatusLikeDocument.get_like_list( status['_id'], self.current_user['_id']) likers = '、'.join([like['liker']['name'] for like in like_list]) self.write_json({'like_times': like_times, 'likers': likers})
def post(self): response_data = {} form = FriendRequestForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user or user['_id'] == self.current_user['_id']: raise HTTPError(404) is_friend = yield FriendDocument.is_friend( user_id, self.current_user['_id'] ) if is_friend: raise HTTPError(404) document = { 'sender': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'recipient': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'message_type': MessageTopic.FRIEND_REQUEST_NEW, } message = yield MessageDocument.find_one(document) if message: response_data = {'error': '你已经发送了好友请求!'} else: user_setting = yield UserSettingDocument.get_user_setting(user_id) if not user_setting['require_verify_when_add_friend']: yield FriendDocument.add_friend( user_id, self.current_user['_id'] ) response_data.update({'ok': 1}) document.update({'time': datetime.now()}) message_id = yield MessageDocument.insert(document) WriterManager.pub(MessageTopic.FRIEND_REQUEST_NEW, message_id) self.write_json(response_data)
def post(self): response_data = {} form = FriendRequestForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user or user['_id'] == self.current_user['_id']: raise HTTPError(404) is_friend = yield FriendDocument.is_friend(user_id, self.current_user['_id']) if is_friend: raise HTTPError(404) document = { 'sender': DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])), 'recipient': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'message_type': MessageTopic.FRIEND_REQUEST_NEW, } message = yield MessageDocument.find_one(document) if message: response_data = {'error': '你已经发送了好友请求!'} else: user_setting = yield UserSettingDocument.get_user_setting(user_id) if not user_setting['require_verify_when_add_friend']: yield FriendDocument.add_friend(user_id, self.current_user['_id']) response_data.update({'ok': 1}) document.update({'time': datetime.now()}) message_id = yield MessageDocument.insert(document) WriterManager.pub(MessageTopic.FRIEND_REQUEST_NEW, message_id) self.finish(json.dumps(response_data))
def new_message(cls, msg): '''收到XMPPClient端发来的消息''' current_user_id = str(msg['to']).split('@')[0] chat_with_id = str(msg['from']).split('@')[0] _id = ObjectId(str(msg['subject'])) callback = cls.get(current_user_id, chat_with_id) # 在线, 并且打开了聊天窗口 if callback: try: callback(msg) except: logging.error("Error in browser client callback", exc_info=True) ChatMessageDocument.get_collection(pymongo=True).update( {'_id': ObjectId(_id)}, {'$set': {'read': True}}) # 在线但是没有打开聊天窗口. 这种情况下, 交给nsq消息系统处理. else: WriterManager.pub(MessageTopic.CHAT_MESSAGE_NEW, str(msg['subject']))
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 = RegisterForm(self.request.arguments) if form.validate(): name = form.name.data email = form.email.data password = form.password.data if (yield UserDocument.find_one({'name': name})): response_data["error"] = "用户名已被占用" if (yield UserDocument.find_one({"email": email})): response_data["error"] = "邮箱已被注册" if not response_data: password = yield UserDocument.encrypt_password(password) document = { 'email': email, 'name': name, 'password': password, 'user_type': "person", 'register_date': datetime.now() } try: user_id = yield UserDocument.insert(document) except: raise HTTPError(500) # 头像初始化 avatar = open( os.path.join(APPLICATION_SETTINGS['static_path'], 'img/default.jpg')) content = avatar.read() avatar.close() document = { 'name': 'default.jpg', 'upload_time': datetime.now(), 'content_type': 'jpeg', 'owner': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'content': Binary(content), 'thumbnail50x50': Binary(content), 'thumbnail180x180': Binary(content) } yield AvatarDocument.insert(document) # 用户设置初始化 _ = yield OfficialProfileCoverDocument.get_profile_cover_list() profile_cover = random.sample(_, 1)[0] document = { 'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'profile_cover': DBRef(OfficialProfileCoverDocument.meta['collection'], ObjectId(profile_cover['_id'])) } yield UserSettingDocument.insert(document) # Ejabberd注册 try: Ejabberd.register(user_id, password) except: pass # 给用户发送验证邮件 document = { 'uid': user_id, 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta(days=USER_SETTINGS['code_expired_after']) } code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_ACTIVATION_EMAIL, code_id) response_data.update( {'success': '注册成功! 系统已经向你的注册邮箱发送了一封激活' '邮件, 请验证后登录!'}) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.finish(json.dumps(response_data))
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)
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)
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 = StatusLikeForm(self.request.arguments) if not form.validate(): raise HTTPError(404) status_id = form.status_id.data status = yield StatusDocument.find_one({'_id': ObjectId(status_id)}) if not status: raise HTTPError(404) status_dbref = DBRef( StatusDocument.meta['collection'], ObjectId(status_id) ) liker_dbref = DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ) document = { 'status': status_dbref, 'liker': liker_dbref } liked = yield StatusLikeDocument.is_liked( status_id, self.current_user['_id'] ) if not liked: now = datetime.now() document.update({'like_time': now}) like_id = yield StatusLikeDocument.insert_one(document) if str(self.current_user['_id']) != str(status['author'].id): document = { 'sender': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'recipient': DBRef( UserDocument.meta['collection'], ObjectId(status['author'].id) ), 'message_type': 'like:status', 'time': now, 'read': False, 'data': DBRef( StatusLikeDocument.meta['collection'], ObjectId(like_id) ) } message_id = yield MessageDocument.insert(document) WriterManager.pub(MessageTopic.LIKE, message_id) like_times = yield StatusLikeDocument.get_like_times(status_id) like_list = yield StatusLikeDocument.get_like_list( status['_id'], self.current_user['_id'] ) likers = '、'.join([like['liker']['name'] for like in like_list]) self.write_json({ 'like_times': like_times, 'likers': likers })
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 = 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))
def post(self): '''评论状态''' response_data ={} form = StatusCommentNewForm(self.request.arguments) if form.validate(): status_id = form.status_id.data content = form.content.data replyeder_id = form.replyeder_id.data status = yield StatusDocument.find_one({ '_id': ObjectId(status_id) }) if not status: raise HTTPError(404) can_see = yield StatusDocument.can_see( status, self.current_user['_id'] ) if not can_see: raise HTTPError(404) replyeder = None if replyeder_id: replyeder = yield UserDocument.find_one({ '_id': ObjectId(replyeder_id) }) if not replyeder: raise HTTPError(404) is_friend = yield FriendDocument.is_friend( self.current_user['_id'], replyeder_id ) if not is_friend: raise HTTPError(404) now = datetime.now() document = { 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status['_id']) ), 'author': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'comment_time': now, 'content': content } if replyeder: document.update({ 'replyeder': DBRef( UserDocument.meta['collection'], ObjectId(replyeder_id) ) }) comment_id = yield StatusCommentDocument.insert_one(document) if replyeder: recipient_id = replyeder_id message_type = 'reply:status' message_topic = MessageTopic.REPLY else: recipient_id = status['author'].id message_type = 'comment:status' message_topic = MessageTopic.COMMENT if ObjectId(self.current_user['_id']) != ObjectId(recipient_id): 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( StatusCommentDocument.meta['collection'], ObjectId(comment_id) ) } message_id = yield MessageDocument.insert(message) WriterManager.pub(message_topic, message_id) comment = yield StatusCommentDocument.get_comment(comment_id) html = self.render_string( 'home/template/status/status-comment-list-item.html', status=status, status_comment=comment ) response_data.update({'html': html}) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.finish(json.dumps(response_data))
def post(self): response_data = {} form = StatusNewForm(self.request.arguments) if form.validate(): content = form.content.data n = len(content) if n > HOME_SETTINGS['status_max_length']: response_data.update({ 'error': ( '状态内容不能超过%s字' % HOME_SETTINGS['status_max_length'] ) }) elif n <= 0 and not ( self.request.files and 'picture' in self.request.files): response_data.update({'error': '请输入文字内容或者照片'}) else: picture = None if self.request.files and 'picture' in self.request.files: picture = self.request.files['picture'][0] image_types = [ 'image/jpg', 'image/png', 'image/jpeg', 'image/gif' ] if picture['content_type'].lower() not in image_types: response_data.update({ 'error': '请上传jpg/png/gif格式的图片' }) if 'error' not in response_data: now = datetime.now() document = { 'author': DBRef( UserDocument.meta['collection'], self.current_user['_id'] ), 'publish_time': now, 'content': content } status_id = yield StatusDocument.insert(document) 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.STATUS_NEW, 'time': now, 'read': False, 'data': DBRef( StatusDocument.meta['collection'], ObjectId(status_id) ) } message_id = yield MessageDocument.insert(document) message_list.append(str(message_id)) if message_list: WriterManager.mpub( MessageTopic.STATUS_NEW, message_list ) if picture is not None: try: image = Image.open(StringIO(picture['body'])) except: raise HTTPError(404) try: content_type = picture[ 'content_type' ].split('/')[-1].upper() except: content_type = 'JPEG' document = { 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status_id) ), 'name': picture['filename'], 'content_type': content_type, 'upload_time': datetime.now() } width = 1024 if image.size[0] > width: height = width * 1.0 * image.size[1] / image.size[0] image = image.resize( map(int, (width, height)), Image.ANTIALIAS ) output = StringIO() image.save(output, content_type, quality=100) document.update({'body': Binary(output.getvalue())}) output.close() thumbnail_width = 200 thumbnail_height = ( thumbnail_width * 1.0 * image.size[1] / image.size[0] ) output = StringIO() image = image.resize( map(int, (thumbnail_width, thumbnail_height)), Image.ANTIALIAS ) image.save(output, content_type, quality=100) document.update({ 'thumbnail': Binary(output.getvalue()) }) output.close() yield StatusPhotoDocument.insert(document) status = yield StatusDocument.get_status( status_id, self.current_user['_id'] ) html = self.render_string( 'home/template/status/status-list-item.html', status=status ) response_data.update({'html': html}) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.finish(json.dumps(response_data))
def post(self): response_data = {} form = StatusNewForm(self.request.arguments) if form.validate(): content = form.content.data n = len(content) if n > HOME_SETTINGS['status_max_length']: response_data.update({ 'error': ( '状态内容不能超过%s字' % HOME_SETTINGS['status_max_length'] ) }) elif n <= 0 and not ( self.request.files and 'picture' in self.request.files): response_data.update({'error': '请输入文字内容或者照片'}) else: picture = None if self.request.files and 'picture' in self.request.files: picture = self.request.files['picture'][0] image_types = [ 'image/jpg', 'image/png', 'image/jpeg', 'image/gif' ] if picture['content_type'].lower() not in image_types: response_data.update({ 'error': '请上传jpg/png/gif格式的图片' }) if 'error' not in response_data: now = datetime.now() document = { 'author': DBRef( UserDocument.meta['collection'], self.current_user['_id'] ), 'publish_time': now, 'content': content } status_id = yield StatusDocument.insert(document) 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.STATUS_NEW, 'time': now, 'read': False, 'data': DBRef( StatusDocument.meta['collection'], ObjectId(status_id) ) } message_id = yield MessageDocument.insert(document) message_list.append(str(message_id)) if message_list: WriterManager.mpub( MessageTopic.STATUS_NEW, message_list ) if picture is not None: try: image = Image.open(StringIO(picture['body'])) except: raise HTTPError(404) try: content_type = picture[ 'content_type' ].split('/')[-1].upper() except: content_type = 'JPEG' document = { 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status_id) ), 'name': picture['filename'], 'content_type': content_type, 'upload_time': datetime.now() } width = 1024 if image.size[0] > width: height = width * 1.0 * image.size[1] / image.size[0] image = image.resize( map(int, (width, height)), Image.ANTIALIAS ) output = StringIO() image.save(output, content_type, quality=100) document.update({'body': Binary(output.getvalue())}) output.close() thumbnail_width = 200 thumbnail_height = ( thumbnail_width * 1.0 * image.size[1] / image.size[0] ) output = StringIO() image = image.resize( map(int, (thumbnail_width, thumbnail_height)), Image.ANTIALIAS ) image.save(output, content_type, quality=100) document.update({ 'thumbnail': Binary(output.getvalue()) }) output.close() yield StatusPhotoDocument.insert(document) status = yield StatusDocument.get_status( status_id, self.current_user['_id'] ) html = self.render_string( 'home/template/status/status-list-item.html', status=status ) response_data.update({'html': html}) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.write_json(response_data)
def post(self): form = LeaveMessageNewForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data private = form.private.data content = form.content.data replyeder_id = form.replyeder_id.data user_setting = yield UserSettingDocument.get_user_setting(user_id) if not user_setting['enable_leaving_message']: raise HTTPError(404) replyeder = None if replyeder_id: replyeder = yield UserDocument.find_one( {'_id': ObjectId(replyeder_id)}) if (not replyeder or ObjectId(user_id) != ObjectId(self.current_user['_id'])): raise HTTPError(404) user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user: raise HTTPError(404) now = datetime.now() document = { 'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'author': DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])), 'private': private, 'content': content, 'leave_time': now } if replyeder: document.update({ 'replyeder': DBRef(UserDocument.meta['collection'], ObjectId(replyeder_id)) }) leave_message_id = yield LeaveMessageDocument.insert(document) if replyeder: recipient_id = replyeder_id message_type = 'reply:leavemessage' message_topic = MessageTopic.REPLY else: recipient_id = user_id message_type = MessageTopic.LEAVE_MESSAGE_NEW message_topic = MessageTopic.LEAVE_MESSAGE_NEW if ObjectId(self.current_user['_id']) != ObjectId(recipient_id): 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, 'data': DBRef(LeaveMessageDocument.meta['collection'], ObjectId(leave_message_id)) } message_id = yield MessageDocument.insert(message) WriterManager.pub(message_topic, message_id) number_func = LeaveMessageDocument.get_leave_message_number leave_message_number = yield number_func(user_id, self.current_user['_id']) document.update({ '_id': ObjectId(leave_message_id), 'floor': leave_message_number }) if replyeder: document.update({'replyeder': replyeder}) leave_message = yield LeaveMessageDocument.translate_dbref_in_document( document) html = self.render_string( 'profile/template/leavemessage/leavemessage-list-item.html', leave_message=leave_message, user=user) self.finish(json.dumps({'html': html}))
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 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))
def post(self): '''评论状态''' response_data ={} form = StatusCommentNewForm(self.request.arguments) if form.validate(): status_id = form.status_id.data content = form.content.data replyeder_id = form.replyeder_id.data status = yield StatusDocument.find_one({ '_id': ObjectId(status_id) }) if not status: raise HTTPError(404) can_see = yield StatusDocument.can_see( status, self.current_user['_id'] ) if not can_see: raise HTTPError(404) replyeder = None if replyeder_id: replyeder = yield UserDocument.find_one({ '_id': ObjectId(replyeder_id) }) if not replyeder: raise HTTPError(404) is_friend = yield FriendDocument.is_friend( self.current_user['_id'], replyeder_id ) if not is_friend: raise HTTPError(404) now = datetime.now() document = { 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status['_id']) ), 'author': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'comment_time': now, 'content': content } if replyeder: document.update({ 'replyeder': DBRef( UserDocument.meta['collection'], ObjectId(replyeder_id) ) }) comment_id = yield StatusCommentDocument.insert_one(document) if replyeder: recipient_id = replyeder_id message_type = 'reply:status' message_topic = MessageTopic.REPLY else: recipient_id = status['author'].id message_type = 'comment:status' message_topic = MessageTopic.COMMENT if ObjectId(self.current_user['_id']) != ObjectId(recipient_id): 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( StatusCommentDocument.meta['collection'], ObjectId(comment_id) ) } message_id = yield MessageDocument.insert(message) WriterManager.pub(message_topic, message_id) comment = yield StatusCommentDocument.get_comment(comment_id) html = self.render_string( 'home/template/status/status-comment-list-item.html', status=status, status_comment=comment ) response_data.update({'html': html}) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break 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 post(self): form = LeaveMessageNewForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data private = form.private.data content = form.content.data replyeder_id = form.replyeder_id.data user_setting = yield UserSettingDocument.get_user_setting(user_id) if not user_setting['enable_leaving_message']: raise HTTPError(404) replyeder = None if replyeder_id: replyeder = yield UserDocument.find_one({ '_id': ObjectId(replyeder_id) }) if (not replyeder or ObjectId(user_id) != ObjectId(self.current_user['_id'])): raise HTTPError(404) user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user: raise HTTPError(404) now = datetime.now() document = { 'user': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'author': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'private': private, 'content': content, 'leave_time': now } if replyeder: document.update({ 'replyeder': DBRef( UserDocument.meta['collection'], ObjectId(replyeder_id) ) }) leave_message_id = yield LeaveMessageDocument.insert(document) if replyeder: recipient_id = replyeder_id message_type = 'reply:leavemessage' message_topic = MessageTopic.REPLY else: recipient_id = user_id message_type = MessageTopic.LEAVE_MESSAGE_NEW message_topic = MessageTopic.LEAVE_MESSAGE_NEW if ObjectId(self.current_user['_id']) != ObjectId(recipient_id): 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, 'data': DBRef( LeaveMessageDocument.meta['collection'], ObjectId(leave_message_id) ) } message_id = yield MessageDocument.insert(message) WriterManager.pub(message_topic, message_id) number_func = LeaveMessageDocument.get_leave_message_number leave_message_number = yield number_func( user_id, self.current_user['_id'] ) document.update({ '_id': ObjectId(leave_message_id), 'floor': leave_message_number }) if replyeder: document.update({ 'replyeder': replyeder }) leave_message = yield LeaveMessageDocument.translate_dbref_in_document( document ) html = self.render_string( 'profile/template/leavemessage/leavemessage-list-item.html', leave_message=leave_message, user=user ) self.write_json({'html': html})
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): response_data = {} form = RegisterForm(self.request.arguments) if form.validate(): name = form.name.data email = form.email.data password = form.password.data if (yield UserDocument.find_one({'name': name})): response_data["error"] = "用户名已被占用" if (yield UserDocument.find_one({"email": email})): response_data["error"] = "邮箱已被注册" if not response_data: password = yield UserDocument.encrypt_password(password) document = { 'email': email, 'name': name, 'password': password, 'user_type': "person", 'register_date': datetime.now() } try: user_id = yield UserDocument.insert(document) except: raise HTTPError(500) # 头像初始化 avatar = open(os.path.join( APPLICATION_SETTINGS['static_path'], 'img/default.jpg') ) content = avatar.read() avatar.close() document = { 'name': 'default.jpg', 'upload_time': datetime.now(), 'content_type': 'jpeg', 'owner': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'content': Binary(content), 'thumbnail50x50': Binary(content), 'thumbnail180x180': Binary(content) } yield AvatarDocument.insert(document) # 用户设置初始化 _ = yield OfficialProfileCoverDocument.get_profile_cover_list() profile_cover = random.sample(_, 1)[0] document = { 'user': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'profile_cover': DBRef( OfficialProfileCoverDocument.meta['collection'], ObjectId(profile_cover['_id']) ) } yield UserSettingDocument.insert(document) # Ejabberd注册 try: Ejabberd.register(user_id, password) except: pass # 给用户发送验证邮件 document = { 'uid': user_id, 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta( days=USER_SETTINGS['code_expired_after'] ) } code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_ACTIVATION_EMAIL, code_id) response_data.update({ 'success': '注册成功! 系统已经向你的注册邮箱发送了一封激活' '邮件, 请验证后登录!' }) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.write_json(response_data)