def on_message(self, message): json_data = get_cleaned_json_data_websocket(message, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] if opt == 'update_recent_user_list': current_user_list = ChatMessage.get_recent_user_list( self.current_user) current_user_list['code'] = 'update_recent_user_list' self.write_message(json_result(0, current_user_list)) elif opt == 'update_recent_user_list_and_open': current_user_list = ChatMessage.get_recent_user_list( self.current_user) current_user_list['code'] = 'update_recent_user_list_and_open' self.write_message(json_result(0, current_user_list)) elif opt == 'send_message': other_id = data['user_id'] other = User.get(User.id == other_id) content = data['content'] cl = ChatMessage.create(sender=self.current_user, receiver=other, content=content) other_websocket = WebsocketChatHandler.is_online(other.username) self.write_message( json_result( 0, { 'code': 'receive_message', 'other_id': other.id, 'msg': ['>', cl.content, TimeUtil.datetime_delta(cl.time)] })) other_websocket.write_message( json_result( 0, { 'code': 'receive_message', 'other_id': self.current_user.id, 'msg': ['<', cl.content, TimeUtil.datetime_delta(cl.time)] })) elif opt == 'recent_chat_message': other_id = data['user_id'] other = User.get(User.id == other_id) recent_message = ChatMessage.get_recent_chat_message( self.current_user, other) recent_message['code'] = 'recent_chat_message' self.write_message(json_result(0, recent_message))
def on_message(self, message): json_data = get_cleaned_json_data_websocket(message, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] if opt == 'update_recent_user_list': logger.debug('update_recent_user_list...') recent_user_list = ChatMessage.get_recent_user_list(self.current_user) self.write_message(json_result(0,{'code': 'recent_user_list', 'data': recent_user_list})) elif opt == 'update_recent_user_list_and_open': recent_user_list = ChatMessage.get_recent_user_list(self.current_user) self.write_message(json_result(0,recent_user_list)) elif opt == 'send_message': other_id = data['user_id'] other = User.get(User.id == other_id) content = data['content'] cl = ChatMessage.create(sender=self.current_user, receiver=other, content=content) self.write_message(json_result(0, {'code': 'receive_a_message', 'data': { 'id': other.id, 'name': other.username, 'avatar': other.avatar, 'msg': ['>', cl.content, TimeUtil.datetime_delta(cl.time)]}})) # send to other user other_websocket = WebsocketChatHandler.is_online(other.username) if other_websocket: other_websocket.write_message(json_result(0, {'code': 'receive_a_message', 'data': { 'id': self.current_user.id, 'avatar': self.current_user.avatar, 'name': self.current_user.username, 'msg': ['<', cl.content, TimeUtil.datetime_delta(cl.time)]}})) elif opt == 'update_recent_message_list': other_id = data['user_id'] other = User.get(User.id == other_id) recent_message = ChatMessage.get_recent_chat_message(self.current_user, other) logger.debug(recent_message) self.write_message(json_result(0,{'code': 'recent_message_list', 'data':recent_message}))
def post(self, *args, **kwargs): json_data = get_cleaned_json_data(self, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] # 关注用户 if opt == 'follow-user': try: user = User.get(User.id == data['user']) except: self.write(json_result(1, '没有该用户')) return Follower.create(user=user, follower=self.current_user) self.write(json_result(0, 'success')) # 取关用户 elif opt == 'unfollow-user': try: user = User.get(User.id == data['user']) except: self.write(json_result(1, '没有该用户')) return try: f = Follower.get(Follower.user == user, Follower.follower == self.current_user) except: self.write(json_result(1, '还没有关注他')) return f.delete_instance() self.write(json_result(0, 'success')) # 更新头像 elif opt == 'update-avatar': import base64 avatar = base64.b64decode(data['avatar']) user = self.current_user avatar_file_name = user.username + '.png' avatar_file = open(config.avatar_upload_path + avatar_file_name, 'wb') avatar_file.write(avatar) user.avatar = avatar_file_name user.save() self.write(json_result(0, 'success')) # 更新社区主题 elif opt == 'update-theme': user = self.current_user user.theme = data['theme'] user.save() self.write(json_result(0, 'success')) # 获取聊天记录 elif opt == 'realtime-chat': user = self.current_user other_id = data['other'] other = User.get(User.id == other_id) result = ChatMessage.get_recent_chat_message(user, other) self.write(json_result(0, result)) # 发送消息 elif opt == 'chat-to' : user = self.current_user other_id = data['other'] other = User.get(User.id == other_id) content = data['content'] ChatMessage.create(me=user, other=other, content=content) self.write(json_result(0, 'success')) else: self.write(json_result(1, 'opt不支持'))
def on_message(self, message): json_data = get_cleaned_json_data_websocket(message, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] if opt == 'update_recent_user_list': logger.debug('update_recent_user_list...') recent_user_list = ChatMessage.get_recent_user_list( self.current_user) self.write_message( json_result(0, { 'code': 'recent_user_list', 'data': recent_user_list })) elif opt == 'update_recent_user_list_and_open': recent_user_list = ChatMessage.get_recent_user_list( self.current_user) self.write_message(json_result(0, recent_user_list)) elif opt == 'send_message': other_id = data['user_id'] other = User.get(User.id == other_id) content = data['content'] cl = ChatMessage.create(sender=self.current_user, receiver=other, content=content) self.write_message( json_result( 0, { 'code': 'receive_a_message', 'data': { 'id': other.id, 'name': other.username, 'avatar': other.avatar, 'msg': [ '>', cl.content, TimeUtil.datetime_delta(cl.time) ] } })) # send to other user other_websocket = WebsocketChatHandler.is_online(other.username) if other_websocket: other_websocket.write_message( json_result( 0, { 'code': 'receive_a_message', 'data': { 'id': self.current_user.id, 'avatar': self.current_user.avatar, 'name': self.current_user.username, 'msg': [ '<', cl.content, TimeUtil.datetime_delta(cl.time) ] } })) elif opt == 'update_recent_message_list': other_id = data['user_id'] other = User.get(User.id == other_id) recent_message = ChatMessage.get_recent_chat_message( self.current_user, other) logger.debug(recent_message) self.write_message( json_result(0, { 'code': 'recent_message_list', 'data': recent_message }))
def post(self, *args, **kwargs): json_data = get_cleaned_json_data(self, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] # 关注用户 if opt == 'follow-user': try: user = User.get(User.id == data['user']) except: self.write(json_result(1, '没有该用户')) return Follower.create(user=user, follower=self.current_user) self.write(json_result(0, 'success')) # 取关用户 elif opt == 'unfollow-user': try: user = User.get(User.id == data['user']) except: self.write(json_result(1, '没有该用户')) return try: f = Follower.get(Follower.user == user, Follower.follower == self.current_user) except: self.write(json_result(1, '还没有关注他')) return f.delete_instance() self.write(json_result(0, 'success')) # 更新头像 elif opt == 'update-avatar': import base64 avatar = base64.b64decode(data['avatar']) user = self.current_user avatar_file_name = user.username + '.png' avatar_file = open(config.avatar_upload_path + avatar_file_name, 'wb') avatar_file.write(avatar) user.avatar = avatar_file_name user.save() self.write(json_result(0, 'success')) # 更新社区主题 elif opt == 'update-theme': user = self.current_user user.theme = data['theme'] user.save() self.write(json_result(0, 'success')) # 获取聊天记录 elif opt == 'realtime-chat': user = self.current_user other_id = data['other'] other = User.get(User.id == other_id) result = ChatMessage.get_recent_chat_message(user, other) self.write(json_result(0, result)) # 发送消息 elif opt == 'chat-to': user = self.current_user other_id = data['other'] other = User.get(User.id == other_id) content = data['content'] ChatMessage.create(me=user, other=other, content=content) self.write(json_result(0, 'success')) else: self.write(json_result(1, 'opt不支持'))
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatMessage from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 测试用户功能 --------------- user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(""" 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 """) postcategory0 = PostCategory.create(name='学习', str='study') postcategory1 = PostCategory.create(name='专业', str='major') postcategory2 = PostCategory.create(name='生活', str='live') postcategory3 = PostCategory.create(name='爱好', str='hobby') posttopic0 = PostTopic.create(category=postcategory0, name='学习资料', str='study-material') posttopic1 = PostTopic.create(category=postcategory0, name='考研资料', str='study-advance-material') posttopic2 = PostTopic.create(category=postcategory0, name='竞赛', str='study-competition') posttopic3 = PostTopic.create(category=postcategory0, name='请教', str='study-advice') posttopic4 = PostTopic.create(category=postcategory1, name='计算机', str='major-computer') posttopic5 = PostTopic.create(category=postcategory2, name='电影资源', str='live-movie') posttopic6 = PostTopic.create(category=postcategory2, name='共享账号', str='live-account') posttopic7 = PostTopic.create(category=postcategory2, name='电脑故障', str='live-computer-repair') posttopic8 = PostTopic.create(category=postcategory3, name='摄影', str='hobby-photography') posttopic9 = PostTopic.create(category=postcategory3, name='健身', str='hobby-fitness') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create(topic=posttopic0, title='test', content=tmp_post, user=user_admin) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create(post=post, user=user_test, content='test') post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatMessage.create(sender=user_admin, receiver=user_test, content='self>other') chat_log_0 = ChatMessage.create(sender=user_test, receiver=user_admin, content='other>self')
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatMessage from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 测试用户功能 --------------- user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(""" 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 """) postcategory0 = PostCategory.create(name='学习', str='study') postcategory1 = PostCategory.create(name='专业', str='major') postcategory2 = PostCategory.create(name='生活', str='live') postcategory3 = PostCategory.create(name='爱好', str='hobby') posttopic0 = PostTopic.create(category=postcategory0, name='学习资料', str='study-material') posttopic1 = PostTopic.create(category=postcategory0, name='考研资料', str='study-advance-material') posttopic2 = PostTopic.create(category=postcategory0, name='竞赛', str='study-competition') posttopic3 = PostTopic.create(category=postcategory0, name='请教', str='study-advice') posttopic4 = PostTopic.create(category=postcategory1, name='计算机', str='major-computer') posttopic5 = PostTopic.create(category=postcategory2, name='电影资源', str='live-movie') posttopic6 = PostTopic.create(category=postcategory2, name='共享账号', str='live-account') posttopic7 = PostTopic.create(category=postcategory2, name='电脑故障', str='live-computer-repair') posttopic8 = PostTopic.create(category=postcategory3, name='摄影', str='hobby-photography') posttopic9 = PostTopic.create(category=postcategory3, name='健身', str='hobby-fitness') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create( topic=posttopic0, title='test', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create( post=post, user=user_test, content='test' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatMessage.create(sender=user_admin, receiver=user_test, content='self>other') chat_log_0 = ChatMessage.create(sender=user_test, receiver=user_admin, content='other>self')
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatMessage from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 测试用户功能 --------------- user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(''' 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 ''') postcategory0 = PostCategory.create(name='分类', str='live') posttopic0 = PostTopic.create(category=postcategory0, name='爱学习', str='live-study') posttopic1 = PostTopic.create(category=postcategory0, name='爱生活', str='live-life') posttopic2 = PostTopic.create(category=postcategory0, name='爱管“闲事”', str='live-thing') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create( topic=posttopic0, title='test', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create( post=post, user=user_test, content='test' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatMessage.create(sender=user_admin, receiver=user_test, content='self>other') chat_log_0 = ChatMessage.create(sender=user_test, receiver=user_admin, content='other>self')