def post(self): try: user_id = self.get_argument('user_id') reply_user_id = self.get_argument('reply_user_id', None) room_id = self.get_argument('room_id') content_id = self.get_argument('content_id') text = self.get_argument('text') except MissingArgumentError: return self.error(MISSING_PARAMS) if filter.check_sensitive(text): return self.error(SENSITIVE_WORD_EXISTS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) # if reply_user_id: # reply_user = User.get(reply_user_id) # if not reply_user: # return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if not user.has_room_access(room_id): return self.error(ACCESS_NOT_ALLOWED) if not user.has_room_speak_access(room_id): return self.error(USER_IN_ROOM_SILENT_LIST) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) try: comment = Comment.add(user_id, reply_user_id, content_id, text) if not reply_user_id: if int(user_id) != int(content.creator_id): notify_content_comment.delay(user, content, comment) handle_daily_comment.delay(user_id, comment.id) else: reply_user = User.get(reply_user_id) if not reply_user: return self.error(USER_NOT_FOUND) if int(user.id) != int(reply_user_id): notify_comment_reply.delay(user, reply_user, content, comment) handle_daily_comment.delay(user_id, comment.id) return self.render({ 'status': 0, 'data': comment.jsonify(), }) except Exception as e: logger.error(u'发表评论失败。User:[%s], Content:[%s], Error:[%s]' % (user_id, content_id, e)) return self.error(SYSTEM_ERROR)
def post(self): try: reporter_id = self.get_argument('user_id') content_id = self.get_argument('content_id') room_id = int(self.get_argument('room_id')) reason = self.get_argument('reason', '') except MissingArgumentError: return self.error(MISSING_PARAMS) content = Content.get(content_id) if int(content.room_id) != room_id: return self.error(ACCESS_NOT_ALLOWED) user = User.get(reporter_id) if not user.has_room_access(room_id): return self.error(ACCESS_NOT_ALLOWED) try: Report.add(reporter_id, REPORT_ROOM_CONTENT, content_id, reason) return self.render({ 'status': 0, }) except Exception as e: logger.error(u'添加举报失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR)
def get(self): try: user_id = self.get_argument('user_id') content_id = self.get_argument('content_id') start = int(self.get_argument('start', 0)) count = int(self.get_argument('count', 10)) except AttributeError: return self.error(MISSING_PARAMS) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) if not user.has_room_access(content.room_id): return self.error(ACCESS_NOT_ALLOWED) comments = Comment.get_comments_by_content(content_id, start, count) return self.render({ 'status': 0, 'data': [comment.jsonify() for comment in comments], })
def post(self): try: user_id = self.get_argument('user_id') content_id = self.get_argument('content_id') room_id = self.get_argument('room_id') status = self.get_argument('status') except MissingArgumentError: return self.error(MISSING_PARAMS) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) if not user.has_room_access(room_id): return self.error(ACCESS_NOT_ALLOWED) content.update(status=status) return self.render({ 'status': 0, })
def delete(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') content_id = self.get_argument('content_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) if not user.has_room_access(room_id): return self.error(ACCESS_NOT_ALLOWED) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) try: content.delete() return self.render({ 'status': 0, }) except Exception as e: logger.error(u'删除内容失败。Content:[%s], Error:[%s]' % (content_id, e)) return self.error(SYSTEM_ERROR)
def get(self): try: user_id = self.get_argument('user_id') content_id = self.get_argument('content_id') room_id = self.get_argument('room_id') start = int(self.get_argument('start', 0)) count = int(self.get_argument('count', 20)) except AttributeError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) if not user.has_room_access(room_id) or int( content.room_id) != int(room_id): return self.error(ACCESS_NOT_ALLOWED) liked_users = ContentLiked.get_liked_people(content_id, start, count) return self.render({ 'status': 0, 'data': { 'start': start, 'count': count, 'liked_users': [user.jsonify() for user in liked_users], } })
def add(cls, user_id, reply_user_id, content_id, text): db = DbManager().db create_time = datetime.now() cache = CacheManager().cache cache_key = CONTENT_COMMENT_CACHE_KEY % content_id sql = 'insert into {table} (user_id, reply_user_id, content_id, text, create_time) values ' \ '(%s, %s, %s, %s, %s)'.format(table=cls.table) comment_id = db.execute(sql, user_id, reply_user_id, content_id, text, create_time) if comment_id: content = Content.get(content_id) content.update(last_comment_time=create_time) cache.lpush(cache_key, comment_id) return cls(comment_id, user_id, reply_user_id, content_id, text, create_time)
def get(self): try: user_id = self.get_argument('user_id') content_id = self.get_argument('content_id') room_id = self.get_argument('room_id') start = int(self.get_argument('start', 0)) count = int(self.get_argument('count', 10)) except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if not user.has_room_access(room_id) or int( content.room_id) != int(room_id): return self.error(ACCESS_NOT_ALLOWED) reward_orders = RewardOrder.get_reward_orders_by_content( content_id, start, count) data = [] for reward_order in reward_orders: creator = User.get(reward_order.creator_id) amount = float(reward_order.amount) pay_time = reward_order.pay_time.strftime('%Y-%m-%d %H:%M:%S') data.append({ 'user': { 'id': creator.id, 'name': creator.user_name, 'avatar': creator.avatar_url, }, 'amount': amount, 'pay_time': pay_time }) return self.render({'status': 0, 'data': data})
def receive_trade_payment(self): db = DbManager().db cache = CacheManager().cache trades = self.trades now = datetime.now() score = get_int_date(now) all_trades_complete = True for trade in trades: if trade.status != STATUS_COMPLETE: all_trades_complete = False break if all_trades_complete: try: db.execute('begin;') for trade in trades: if trade.pay_method == PAYMETHOD_ACCOUNT: sql = 'update {table} set balance=balance-%s where user_id=%s'.format(table=Account.table) db.execute(sql, float(trade.amount), self.creator_id) sql = 'update {table} set balance=balance+%s where user_id=%s'.format(table=Account.table) db.execute(sql, float(self.amount), self.receiver_id) sql = 'update {table} set status=%s, pay_time=%s where id=%s'.format(table=self.table) db.execute(sql, STATUS_COMPLETE, now, self.id) sql = 'insert into {table} (user_id, source, order_type, order_id, amount, create_time) values ' \ '(%s, %s, %s, %s, %s, %s)'.format(table=WalletRecord.table) db.execute(sql, self.creator_id, WALLET_RECORD_REWARD_SEND, ORDER_REWARD, self.id, -self.amount, now) db.execute(sql, self.receiver_id, WALLET_RECORD_REWARD_RECEIVE, ORDER_REWARD, self.id, self.amount, now) db.execute('commit;') except: db.execute('rollback;') raise notify_content_reward.delay(self.creator, Content.get(self.content_id), self) cache.zincrby(CONTENT_REWARD_AMOUNT_CACHE_KEY, self.content_id, float(self.amount) * 100) cache_key = ROOM_USER_REWARD_CACHE_KEY % self.room_id cache.zincrby(cache_key, self.creator_id, float(self.amount) * 100) cache_key = USER_REWARD_RANK_CACHE_KEY cache.zincrby(cache_key, self.creator_id, float(self.amount) * 100) cache_key = CONTENT_REWARD_CACHE_KEY % self.content_id cache.lpush(cache_key, self.id) cache.zadd(USER_REWARD_CONTENT_KEY % self.creator_id, score, self.content_id) self.flush_reward_cache() handle_daily_send_reward.delay(self)
def post(self): try: user_id = self.get_argument('user_id') content_id = self.get_argument('content_id') room_id = self.get_argument('room_id') except AttributeError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(MISSING_PARAMS) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) if not user.has_room_access(room_id) or int( content.room_id) != int(room_id): return self.error(ACCESS_NOT_ALLOWED) try: liked_count = ContentLiked.add(content.id, content.content_type, user_id, content.creator_id) has_liked = ContentLiked.has_liked(content.id, user_id) if has_liked and (int(user_id) != int(content.creator_id)): notify_content_like.delay(user, content) except Exception as e: logger.error(u'添加点赞失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR) if has_liked: handle_daily_send_like_integral.delay(user_id, content.creator_id) return self.render({ 'status': 0, 'data': { 'has_liked': bool(has_liked), 'liked_count': liked_count, } })
def get(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') content_id = self.get_argument('content_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) if not user.has_room_access(room_id): return self.error(ACCESS_NOT_ALLOWED) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) return self.render({ "status": 0, "data": content.jsonify(user), })
def post(self): try: user_id = int(self.get_argument('user_id')) room_id = int(self.get_argument('room_id')) content_id = self.get_argument('content_id') text = self.get_argument('text') start_time = self.get_argument('start_time', None) except MissingArgumentError: return self.error(MISSING_PARAMS) now = datetime.now() if filter.check_sensitive(text): return self.error(SENSITIVE_WORD_EXISTS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) content = Content.get(content_id) if not content: return self.error(CONTENT_NOT_FOUND) if int(room.creator_id) != user_id or int(content.room_id) != room_id: return self.error(ACCESS_NOT_ALLOWED) push_count = RoomPush.get_push_count_by_room_and_date(room_id, now) if push_count and push_count >= 1: return self.error(ROOM_PUSH_OVER_LIMIT) push_room_content.delay(room, content, text, start_time) return self.render({ 'status': 0, })
def content(self): return Content.get(self.content_id)
def jsonify(self): action = int(self.action) content = Content.get(self.target_id) data = { 'id': self.id, 'action': action, 'status': self.status, 'create_time': self.create_time if isinstance(self.create_time, basestring) else self.create_time.strftime('%Y-%m-%d %H:%M:%S'), 'notify_type': self.notify_type, 'extra': self.extra, 'extra_info': self.extra_info, } if content: if content.content_type == POSTS_TYPE: images = content.jsonify().get('images') else: images = None data.update({ 'room_id': content.room_id, 'content': { 'id': content.id, 'content_type': content.content_type, 'images': images, } }) if self.sender: data.update({ 'sender': { 'id': self.sender.id, 'name': self.sender.user_name, 'avatar': self.sender.avatar_url, } }) if self.receiver: data.update({ 'receiver': { 'id': self.receiver.id, 'name': self.receiver.user_name, 'avatar': self.receiver.avatar_url, } }) if action == NOTIFY_ACTION_CONTENT_REWARD: data.update({'reward_amount': self.extra_info}) if action in (NOTIFY_ACTION_CONTENT_COMMENT, NOTIFY_ACTION_COMMENT_REPLY): data.update({'comment_text': self.extra_info}) if action == NOTIFY_ACTION_CHAT_ENVELOPE_REWARD: envelope = GroupEnvelope.get(self.target_id) data.update({ 'envelope': { 'id': self.target_id, 'status': envelope.status, 'amount': float(envelope.amount), 'liked_amount': envelope.liked_amount, } }) return data