def datang_create_video(): """创建视频 (POST&LOGIN) :uri: /datang/videos/new-video :param game_id: 视频所属游戏id :param user_id: 用户ID :param title: 视频标题 :param duration: 视频时长 :param ratio: 视频尺寸 :returns: object """ user_id = request.values['user_id'] game_id = request.values['game_id'] video = DaTangVideo.init() video.author = user_id video.game = game_id video.title = request.values['title'] if not Guard.verify_sig(request.values.to_dict()): return error.AuthFailed # 敏感词检查 if Spam.filter_words(video.title, 'video'): return error.InvalidContent try: duration = int(request.values['duration']) except: return error.InvalidArguments video.duration = duration video.ratio = request.values['ratio'] # 设置为文件上传状态, 文件上传成功之后更改为上线状态 video.status = const.UPLOADING vid = video.create_model() return DaTangVideo.get_one(vid, check_online=False).format()
def activity_create_comment(): """创建评论 (GET|POST&LOGIN) :uri: /activity/user/submit-comment :param activity_id: 被评论活动id :param content: 评论内容 :returns: {'comment': object} """ user = request.authed_user params = request.values aid = params.get('activity_id', None) content = params.get('content', None) if not aid or not content: return error.InvalidArguments if not ActivityConfig.get_one(aid, check_online=False): return error.ActivityNotExist # 敏感词检查 if Spam.filter_words(content, 'comment'): return error.InvalidContent comment = ActivityComment.init() comment.activity = ObjectId(aid) comment.author = ObjectId(str(user._id)) comment.content = content cid = comment.create_model() return ActivityComment.get_one(str(cid)).format()
def format(self): from wanx.models.user import User sender = User.get_one(str(self.sender), check_online=False) data = { 'msg_id': str(self._id), 'sender': sender and sender.format(), 'content': Spam.replace_words(self.content), 'create_at': self.create_at } return data
def modify_info(uid): """修改用户信息 (GET|POST&LOGIN) :uri: /users/<string:uid>/modify-info :param nickname: 昵称 :param phone: 手机 :param birthday: 生日 :param email: 邮箱 :param gender: 性别(1:男, 2:女) :param signature: 签名 :param announcement: 公告 :returns: {'user': object} """ user = request.authed_user params = request.values nickname = params.get('nickname', None) signature = params.get('signature', None) if signature: if Spam.filter_words(signature, 'signature'): return error.InvalidContent announcement = params.get('announcement', None) if announcement: if Spam.filter_words(announcement, 'announcement'): return error.InvalidContent if nickname: invalid_error = User.invalid_nickname(nickname) if invalid_error: return invalid_error info = dict() for key in const.USER_ALLOWED_MODIFY: if params.get(key, None): info[key] = const.USER_ALLOWED_MODIFY[key](params[key]) if 'gender' in info and info['gender'] not in [1, 2]: return error.InvalidArguments info['update_at'] = time.time() user = user.update_model({'$set': info}) return {'user': user.format()}
def create_comment(): """创建评论 (GET|POST&LOGIN) :uri: /user/opt/submit-comment :param video_id: 被评论视频id :param content: 评论内容 :returns: {'comment': object} """ user = request.authed_user params = request.values vid = params.get('video_id', None) content = params.get('content', None) if not vid or not content: return error.InvalidArguments if not Video.get_one(vid): return error.VideoNotExist # 敏感词检查 if Spam.filter_words(content, 'comment'): return error.InvalidContent comment = Comment.init() comment.author = ObjectId(str(user._id)) comment.content = content comment.video = ObjectId(vid) cid = comment.create_model() if cid: # 发送评论消息 Message.send_video_msg(str(user._id), str(vid), 'comment') # 任务检查 if user: UserTask.check_user_tasks(str(user._id), COMMENT_VIDEO, 1) # 更新活动评论数 avideos = ActivityVideo.get_activity_video(vid=vid) for avideo in avideos: ts = time.time() aconfig = ActivityConfig.get_one(str(avideo['activity_id']), check_online=False) if aconfig and aconfig.status == const.ACTIVITY_BEGIN \ and (aconfig.begin_at < ts and aconfig.end_at > ts): avideo = ActivityVideo.get_one(avideo['_id'], check_online=False) avideo.update_model({'$inc': {'comment_count': 1}}) return Comment.get_one(str(cid)).format()
def create_reply(): """创建评论的回复 (GET|POST&LOGIN) :uri: /replies/create :param comment_id: 评论id(可选) :param reply_id: 被用户回复的评论回复id(可选) :param content: 回复内容 :returns: {'reply': object} """ user = request.authed_user params = request.values cid = params.get('comment_id', None) reply_id = params.get('reply_id', None) content = params.get('content', None) if not content or (not cid and not reply_id): return error.InvalidArguments comment = Comment.get_one(cid) if cid else None if cid and not comment: return error.CommentNotExist reply = Reply.get_one(reply_id) if reply_id else None if reply_id and not reply: return error.ReplyNotExist # 敏感词检查 if Spam.filter_words(content, 'comment'): return error.InvalidContent new_reply = Reply.init() new_reply.owner = user._id new_reply.reply = reply._id if reply else None new_reply.comment = reply.comment if reply else comment._id new_reply.content = content rid = new_reply.create_model() if rid: # 发送消息 if reply: Message.send_reply_msg(str(user._id), str(reply._id), 'reply') else: Message.send_comment_msg(str(user._id), str(cid), 'reply') return {'reply': Reply.get_one(str(rid)).format() if rid else None}
def create_video(): """创建视频 (POST&LOGIN) :uri: /videos/new-video :param game_id: 视频所属游戏id :param title: 视频标题 :param duration: 视频时长 :param ratio: 视频尺寸 :returns: object """ user = request.authed_user gid = request.values['game_id'] game = Game.get_one(gid) if not game: return error.GameNotExist video = Video.init() video.author = ObjectId(user._id) video.game = ObjectId(gid) video.title = request.values['title'] # 敏感词检查 if Spam.filter_words(video.title, 'video'): return error.InvalidContent try: duration = int(request.values['duration']) except: return error.InvalidArguments video.duration = duration video.ratio = request.values['ratio'] # 设置为文件上传状态, 文件上传成功之后更改为上线状态 video.status = const.UPLOADING vid = video.create_model() # 任务检查 if user: UserTask.check_user_tasks(str(user._id), CREATE_VIDEO, 1) return Video.get_one(vid, check_online=False).format()
def modify_video(vid): """更新视频信息接口 (POST&LOGIN) :uri: /videos/<string:vid>/modify-video :param title: 视频标题 :return: {'video': <Video>object} """ user = request.authed_user title = request.values.get('title') if not title: return error.InvalidArguments video = Video.get_one(vid, check_online=False) if not video or video.author != user._id: return error.VideoNotExist("视频不存在或视频不属于此用户") # 敏感词检查 if Spam.filter_words(title, 'video'): return error.InvalidContent data = {'title': title} video = video.update_model({"$set": data}) return {'video': video.format()}
def test_spam(self): self.assertFalse(Spam.filter_words(u'不错')) self.assertTrue(Spam.filter_words(u'习近平')) self.assertEqual(Spam.replace_words(u'习近平是个好人'), '***是个好人')