def gets_by_room(cls, room_id, user_id, start, count): db = DbManager().db sql = 'select * from {table} where room_id=%s and (creator_id=%s or status=%s) order by last_comment_time ' \ 'desc limit %s, %s'.format(table=cls.table) room_contents = db.query(sql, room_id, user_id, CONTENT_PUBLIC, start, count) return room_contents and [ cls(**room_content) for room_content in room_contents ]
def gets_all(cls): db = DbManager().db # start = (page - 1) * size if page > 1 else 0 # end = page * size - 1 sql = 'select * from {table} order by id desc'.format(table=cls.table) validate_lists = db.query(sql) return validate_lists and [ cls(**validate) for validate in validate_lists ]
def get_users_by_room(cls, room_id): cache = CacheManager().cache cache_key = ROOM_USER_CACHE_KEY % room_id if cache.exists(cache_key): user_ids = cache.zrevrange(cache_key, 0, -1) return user_ids and [User.get(user_id) for user_id in user_ids] else: db = DbManager().db sql = 'select user_id from {table} where room_id=%s'.format(table=cls.table) user_ids_dict = db.query(sql, room_id) return user_ids_dict and [User.get(user_id_dict.get('user_id')) for user_id_dict in user_ids_dict]
def has_followed(cls, user_id, follow_id): cache = CacheManager().cache cache_key = USER_FOLLOW_KEY % user_id if cache.exists(cache_key): return cache.zscore(cache_key, follow_id) else: db = DbManager().db sql = 'select id from {table} where user_id=%s and follow_id=%s'.format(table=cls.table) try: return db.query(sql, user_id, follow_id)[0] except: return None
def get_tags_by_room(cls, room_id): db = DbManager().db sql = 'select tag_id from {table} where room_id=%s'.format( table=cls.table) tag_ids_dict = db.query(sql, room_id) if tag_ids_dict: return [ Tag.get(tag_id_dict.get('tag_id')) for tag_id_dict in tag_ids_dict ] else: return []
def get_notifies(cls, user_id, start, count): db = DbManager().db read_time = datetime.now() sql = 'select * from {table} where receiver_id=%s order by create_time desc limit %s, %s'.format( table=cls.table) notify_infos = db.query(sql, user_id, start, count) sql = 'update {table} set status=%s, read_time=%s where status=%s and receiver_id=%s'.format( table=cls.table) db.execute(sql, NOTIFY_READ, read_time, NOTIFY_UNREAD, user_id) return notify_infos and [ cls(**notify_info) for notify_info in notify_infos ]
def room_exists_user(cls, room_id, user_id): cache = CacheManager().cache cache_key = ROOM_USER_CACHE_KEY % room_id if cache.exists(cache_key): return cache.zscore(cache_key, user_id) else: db = DbManager().db sql = 'select id from {table} where room_id=%s and user_id=%s'.format(table=cls.table) try: return db.query(sql, room_id, user_id)[0] except: return None
def get_rooms_by_tags(cls, tags): db = DbManager().db params = ['tag_id=%s' % tag.id for tag in tags if tag] union_str = ' or '.join(params) if union_str: sql = 'select room_id from {table} where %s'.format( table=cls.table) % union_str room_ids_dict = db.query(sql) room_ids = [ room_id_dict.get('room_id') for room_id_dict in room_ids_dict ] room_ids = list(set(room_ids)) return room_ids and [Room.get(room_id) for room_id in room_ids] return []
def get_admin_users_by_room(cls, room_id): db = DbManager().db sql = 'select user_id from {table} where room_id=%s and status=%s'.format(table=cls.table) user_ids_dict = db.query(sql, room_id, ROOM_USER_ADMIN) return user_ids_dict and [user_id_dict.get('user_id') for user_id_dict in user_ids_dict]
def gets_all(cls): db = DbManager().db sql = 'select * from {table}'.format(table=cls.table) infos = db.query(sql) return [cls(**info) for info in infos]
def get_options_by_question(cls, question_id): db = DbManager().db sql = 'select * from {table} where question_id=%s'.format( table=cls.table) options = db.query(sql, question_id) return options and [cls(**option) for option in options]
def gets_all(cls): db = DbManager().db sql = 'select * from {table}'.format(table=cls.table) room_lists = db.query(sql) return room_lists and [cls(**room) for room in room_lists]
def gets_by_order_and_type(cls, order_id, order_type): db = DbManager().db sql = 'select * from {table} where order_id=%s and order_type=%s'.format( table=cls.table) trades = db.query(sql, order_id, order_type) return trades and [cls(**trade) for trade in trades]
def get_comment_by_content_and_creator(cls, content_id, creator_id): db = DbManager().db sql = 'select * from comment where content_id=%s and user_id=%s' rst = db.query(sql, content_id, creator_id) return rst and rst[0]
def gets_by_user(cls, user_id, start=0, count=10): db = DbManager().db sql = 'select * from {table} where user_id=%s order by create_time desc limit %s, %s'.format(table=cls.table) records = db.query(sql, user_id, start, count) return records and [cls(**record) for record in records]
def get_liked_message_by_user_and_group(cls, user_id, group_id): db = DbManager().db sql = 'select message_id from {table} where user_id=%s and group_id=%s'.format( table=cls.table) rst = db.query(sql, user_id, group_id) return rst and [d.get('message_id') for d in rst]
def get_ordered_room_users_by_room(cls, room_id, start=0, count=10): db = DbManager().db sql = 'select * from {table} where room_id=%s order by status, join_time asc limit %s, %s'.format(table=cls.table) rst = db.query(sql, room_id, start, count) return rst and [cls(**d) for d in rst]
def get_reward_orders_by_receiver(cls, receiver_id, start=0, count=10): db = DbManager().db sql = 'select * from {table} where receiver_id=%s and status=%s order by create_time limit %s, %s'.format(table=cls.table) reward_orders = db.query(sql, receiver_id, STATUS_COMPLETE, start, count) return reward_orders and [cls(**reward_order) for reward_order in reward_orders]
def get_rooms_by_user_order_by_update_time(cls, user_id, start, count): db = DbManager().db sql = 'select * from room left join room_user on room.id=room_user.room_id where room_user.user_id=%s order by room.update_time desc limit %s, %s' room_ids = db.query(sql, user_id, start, count) return room_ids and [Room.get(room_id.get('room_id')) for room_id in room_ids]
def get_pass_proverb(cls): db = DbManager().db sql = 'select id from {table} where status=%s'.format(table=cls.table) rst = db.query(sql, CHECK_STATUS_PASS) return rst and [cls.get(rst[0].get('id'))]
def gets_by_user(cls, user_id, start=0, count=10): db = DbManager().db sql = 'select * from {table} where user_id=%s order by create_time desc limit %s, %s'.format( table=cls.table) address_list = db.query(sql, user_id, start, count) return address_list and [cls(**address) for address in address_list]
def get_by_group_and_message(cls, group_id, message_id): db = DbManager().db sql = 'select * from {table} where group_id=%s and message_id=%s limit 1'.format( table=cls.table) rst = db.query(sql, group_id, message_id) return rst and cls(**rst[0])
def gets_all(cls): db = DbManager().db sql = 'select * from {table}'.format(table=cls.table) room_user_infos = db.query(sql) return room_user_infos and [cls(**room_user_info) for room_user_info in room_user_infos]
def get_messages_by_time(cls, time): db = DbManager().db sql = 'select * from {table} where create_time<%s'.format( table=cls.table) rst = db.query(sql, time) return rst and [cls(**d) for d in rst]
def get_advices_by_user(cls, user_id, start=0, count=10): db = DbManager().db sql = 'select * from {table} where (user_id=%s or reply_user_id=%s) order by create_time desc limit %s, %s'.format(table=cls.table) advices = db.query(sql, user_id, start, count) return advices and [cls(**advice) for advice in advices]
def get_advices(cls, start=0, count=10): db = DbManager().db sql = 'select * from {table} order by create_time desc limit %s, %s'.format(table=cls.table) advices = db.query(sql, start, count) return advices and [cls(**advice) for advice in advices]
def gets_all(cls): db = DbManager().db sql = 'select * from {table}'.format(table=cls.table) rst = db.query(sql) return rst and [cls(**r) for r in rst]
def gets_all(cls): db = DbManager().db sql = 'select * from {table}'.format(table=cls.table) comments = db.query(sql) return comments and [cls(**comment) for comment in comments]
def gets_option_by_vote(cls, vote_id): db = DbManager().db sql = 'select * from {table} where vote_id=%s'.format(table=cls.table) options = db.query(sql, vote_id) return options and [cls(**option) for option in options]