def init_app(username, password): # check if Users.filter_by_query(role_id=1).first(): print('重复初始化!退出') exit(1) # create admin user_obj = Users.create_by_uid() user_obj.username = username user_obj.email = '*****@*****.**' user_obj.role_id = 1 user_obj.set_password(password) user_obj.save() # create base tab with open(os.path.join(BASE_DIR, 'tabs.json'), 'r') as f: tab_data = json.loads(f.read()) for t in tab_data: t_obj = Tabs.create_by_uid() t_obj.name = t['name'] t_obj.zh = t['zh'] if t.get('sort_num'): t_obj.sort_num = t['sort_num'] t_obj.save() for st in t.get('sub_tabs'): st_obj = SubTabs.create_by_uid() st_obj.tab_id = t_obj.id st_obj.name = st['name'] st_obj.zh = st['zh'] if st.get('sort_num'): st_obj.sort_num = st['sort_num'] st_obj.desc = st['desc'] st_obj.save() print(f'init success! admin username: {username}, password: {password}')
def get(self): if self.input.uid: obj = Users.get_by_uid(self.input.uid) data = obj.to_dict(remove_fields_list=['password']) else: data = Users.filter_by_query() if self.input.sk_ and self.input.sv_: data = data.filter(getattr(Users, self.input.sk_).like(f'%{self.input.sv_}%')) if self.input.odb_ and self.input.odt_ in ['asc', 'desc']: data = data.order_by(getattr(getattr(Users, self.input.odb_), self.input.odt_)()) else: data = data.order_by(Users.time_create.desc()) total = None if self.input.page and self.input.page_size: total = data.count() data = data.offset( (int(self.input.page) - 1) * int(self.input.page_size) ).limit(int(self.input.page_size)) data = [_t.to_dict(remove_fields_list=['password']) for _t in data] if total is not None: data = {'total': total, 'list': data} return data
def post(self): if Users.filter_by_query(username=self.input.username).all(): raise LogicError("username重复,请使用其他username") if Users.filter_by_query(email=self.input.email).all(): raise LogicError("email重复,请更换email地址或使用原地址找回密码") email_code_key = f'{session.sid}:email:code' email_code_raw = redis_store.get(email_code_key) if email_code_raw is None: raise LogicError('邮箱验证码不正确,请重新发送') code, email = email_code_raw.decode('utf-8').split('|') if self.input.email_code.upper() != code or self.input.email != email: raise LogicError('邮箱验证码不正确,请重新发送') user_obj = Users.create_by_uid() user_obj.email = email user_obj.username = self.input.username user_obj.set_password(password=self.input.password) update_dict = dict() for k in ['sex', 'avatar_url']: if getattr(self.input, k) is not None: update_dict[k] = getattr(self.input, k) user_obj.update(**update_dict)
def put(self): obj = Users.get_by_uid(self.input.uid) if self.input.status: obj.status = self.input.status if self.input.role_id: obj.role_id = self.input.role_id obj.save()
def get(self): user_obj = Users.get_by_id(session['user_info']['id']) user_dict = user_obj.to_dict(remove_fields_list=['password']) user_dict['fav_sub_tab_count'] = SubTabFav.filter_by_query(user_id=user_dict['id']).count() user_dict['fav_topic_count'] = TopicFav.filter_by_query(user_id=user_dict['id']).count() user_dict['fav_user_count'] = UserFavUser.filter_by_query(user_id=user_dict['id']).count() session['user_info'] = user_dict return user_dict
def check_login(username, password): user_obj = Users.filter_by_query(username=username).first() if not user_obj: raise AuthError("用户不存在") if not user_obj.check_password(password): raise AuthError("密码错误") if not user_obj.status: raise AuthError("账户已经停用") return user_obj
def get(self): if self.input.uid: obj = Comments.get_by_uid(self.input.uid) data = obj.to_dict() data['user'] = Users.get_by_id( data['user_id']).to_dict(remove_fields_list=['password']) data['topic'] = Topics.get_by_id(data['topic_id']).to_dict() else: data = Comments.filter_by_query() if self.input.tab_id: data.filter_by_query(tab_id=self.input.tab_id) elif self.input.sub_tab_id: data.filter_by_query(sub_tab_id=self.input.sub_tab_id) if self.input.sk_ and self.input.sv_: data = data.filter( getattr(Comments, self.input.sk_).like(f'%{self.input.sv_}%')) if self.input.odb_ and self.input.odt_ in ['asc', 'desc']: data = data.order_by( getattr(getattr(Comments, self.input.odb_), self.input.odt_)()) else: data = data.order_by(Comments.time_create.desc()) total = None if self.input.page and self.input.page_size: total = data.count() data = data.offset( (int(self.input.page) - 1) * int(self.input.page_size)).limit(int(self.input.page_size)) data = [_t.to_dict() for _t in data] user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set( i['user_id'] for i in data)), Users.available == 1) } topic_dict = { i.id: i.to_dict() for i in Topics.query.filter( Topics.id.in_(set( i['topic_id'] for i in data)), Topics.available == 1) } for _d in data: _d['user'] = user_dict[_d['user_id']] _d['topic'] = topic_dict[_d['topic_id']] if total is not None: data = {'total': total, 'list': data} return data
def post(self): user_obj = Users.get_by_uid(session['user_info']['uid']) if not user_obj.check_password(self.input.password): raise LogicError('原密码错误!') user_obj.set_password(self.input.new_password) user_obj.save() # 强制下线 token_id_list = Passport.query.with_entities(Passport.token).filter_by(user_id=user_obj.id).all() for _id in token_id_list: current_app.config['SESSION_REDIS'].delete(current_app.config['SESSION_KEY_PREFIX'] + _id[0]) session.clear()
def post(self): user_info = session.get('user_info') user_obj = Users.get_by_uid(user_info['uid']) update_dict = dict() for k in ['sex', 'avatar_url', 'site', 'location', 'company', 'github', 'twitter', 'weibo', 'bio']: if getattr(self.input, k) is not None: update_dict[k] = getattr(self.input, k) if self.input.sex in [0, 1, 2]: update_dict['sex'] = self.input.sex if self.input.privacy_level in [0, 1, 2]: update_dict['privacy_level'] = self.input.privacy_level user_obj.update(**update_dict) user_info.update(update_dict) session['user_info'] = user_info return user_info
def post(self): code_key = f'{session.sid}:code' code = redis_store.get(code_key) if code is None or self.input.code.upper() != code.decode('utf-8'): raise VerifyError('图形验证码不正确,请重试') if not self.is_valid_email(self.input.email): raise ParamsError('email 不合法') if Users.filter_by_query(email=self.input.email).all(): raise LogicError("该email地址已经注册过,请更换email地址") # 加计数锁 lock_key_name = f'lock:send_mail_count:{session.sid}' lock_value = self.generate_hash_uuid(12) set_status = redis_store.set(lock_key_name, lock_value, ex=600, nx=True) if not set_status: return LogicError('正在发送,请稍后重试') # 获取发送次数 + 1 send_count_key = f'{session.sid}:email:send' send_count = redis_store.get(send_count_key) if send_count: send_count = int(send_count.decode('utf-8')) if send_count >= 5: raise LogicError('发送次数太频繁,请稍后再发送') else: send_count = 0 send_count += 1 code = self.generate_hash_uuid(5).upper() send_register_mail.delay(self.input.email, code) # 设置发送的验证码 email_code_key = f'{session.sid}:email:code' redis_store.set(email_code_key, f'{code}|{self.input.email}', ex=180) # 设置新的发送次数 redis_store.set(send_count_key, send_count, ex=180) # 释放锁 ret = release_redis_lock(lock_key_name, lock_value) if not ret: raise LogicError('发送解锁失败')
def post(self): obj = Users.get_by_uid(self.input.uid) # 自己不能关注自己 if session['user_info']['uid'] == obj.uid: raise LogicError('自己不能关注自己哦') if self.input.action == 'add': user_fav_obj = UserFavUser.filter_by_query( fav_user_id=obj.id, user_id=session['user_info']['id'], show_deleted=True).first() if user_fav_obj: user_fav_obj.available = 1 else: user_fav_obj = UserFavUser() user_fav_obj.fav_user_id = obj.id user_fav_obj.user_id = session['user_info']['id'] user_fav_obj.save() elif self.input.action == 'cal': user_fav_obj = UserFavUser.filter_by_query( fav_user_id=obj.id, user_id=session['user_info']['id']).first() if user_fav_obj: user_fav_obj.available = 0 user_fav_obj.save()
def get(self): data = dict() sub_tab_dict_list = defaultdict(list) for sub_tab in SubTabs.filter_by_query().order_by( SubTabs.sort_num.asc()): sub_tab_dict_list[sub_tab.tab_id].append(sub_tab.to_dict()) tab_list = [ t.to_dict() for t in Tabs.filter_by_query().order_by(Tabs.sort_num.asc()) ] for _d in tab_list: _d['sub_tabs'] = sub_tab_dict_list.get(_d['id']) or [] data['tabs'] = tab_list if self.input.tab: tab_obj = Tabs.filter_by_query(name=self.input.tab).first() if not tab_obj: tab_obj = Tabs.get_by_query(name='tech') else: tab_obj = Tabs.get_by_query(name='tech') topic_objs = Topics.filter_by_query(tab_id=tab_obj.id).order_by( Topics.time_create.desc()) data['tab_topic_count'] = topic_objs.count() topic_list = [_t.to_dict() for _t in topic_objs[:80]] user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set( i['user_id'] for i in topic_list)), Users.available == 1) } last_reply_user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set(i['last_reply_user_id'] for i in topic_list)), Users.available == 1) } tab_dict = { i.id: i.to_dict() for i in Tabs.query.filter( Tabs.id.in_(set( i['tab_id'] for i in topic_list)), Tabs.available == 1) } sub_tab_dict = { i.id: i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set( i['sub_tab_id'] for i in topic_list)), SubTabs.available == 1) } comment_count_dict = dict() for c in Comments.query.filter( Comments.topic_id.in_(set(i['id'] for i in topic_list)), Comments.available == 1): if comment_count_dict.get(c.topic_id): comment_count_dict[c.topic_id] += 1 else: comment_count_dict[c.topic_id] = 1 up_down_dict_list = defaultdict(list) for up_down_obj in TopicUpDown.query.filter( TopicUpDown.topic_id.in_(set(i['id'] for i in topic_list)), TopicUpDown.available == 1): up_down_dict_list[up_down_obj.topic_id].append(up_down_obj.action) for _t in topic_list: _t['user'] = user_dict[_t['user_id']] if _t['last_reply_user_id']: _t['last_reply_user'] = last_reply_user_dict[ _t['last_reply_user_id']] _t['tab'] = tab_dict[_t['tab_id']] _t['sub_tab'] = sub_tab_dict[_t['sub_tab_id']] _t['comment_count'] = comment_count_dict.get(_t['id'], 0) # 获取up和down数量 if up_down_dict_list.get(_t['id']): _t['up_count'] = len( [i for i in up_down_dict_list[_t['id']] if i is True]) _t['down_count'] = len( [i for i in up_down_dict_list[_t['id']] if i is False]) else: _t['up_count'] = 0 _t['down_count'] = 0 data['topics'] = topic_list data['user_count'] = Users.filter_by_query().count() data['topic_count'] = Topics.filter_by_query().count() data['comment_count'] = Comments.filter_by_query().count() today_time_start = self.strptime( self.get_datetime_now().strftime('%Y-%m-%d'), '%Y-%m-%d') today_topic_list = [ i.to_dict() for i in Topics.query.filter( Topics.time_create >= today_time_start, Topics.available == 1) ] if today_topic_list: today_comment_dict = dict() for _c in Comments.query.filter( Comments.topic_id.in_( set([i['id'] for i in today_topic_list])), Comments.available == 1): if today_comment_dict.get(_c.topic_id): today_comment_dict[_c.topic_id] += 1 else: today_comment_dict[_c.topic_id] = 1 for today_topic in today_topic_list: today_topic['comment_count'] = today_comment_dict.get( today_topic['id'], 0) today_hot_topic_list = sorted(today_topic_list, key=lambda x: x['comment_count'], reverse=True) if len(today_hot_topic_list) >= 10: today_hot_topic_list = today_hot_topic_list[:10] else: today_hot_topic_list = [] data['hot_topics'] = today_hot_topic_list if session.get('is_login'): fav_sub_tab_id_list = [ i.sub_tab_id for i in SubTabFav.filter_by_query( user_id=session['user_info']['id']) ] sub_tab_list = [ i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set(fav_sub_tab_id_list)), SubTabs.available == 1) ] data['fav_tabs'] = sub_tab_list return data
def get(self): data = dict() user_info = Users.get_by_query(username=self.input.username).to_dict(remove_fields_list=['password']) user_info['fav_user_count'] = UserFavUser.filter_by_query(user_id=user_info['id']).count() user_info['be_fav_user_count'] = UserFavUser.filter_by_query(fav_user_id=user_info['id']).count() data['user'] = user_info data['is_open'] = True data['is_fav'] = False if session.get('is_login'): if UserFavUser.filter_by_query(user_id=session['user_info']['id'], fav_user_id=user_info['id']).first(): data['is_fav'] = True comment_list = [ i.to_dict() for i in Comments.filter_by_query(user_id=user_info['id']).order_by(Comments.time_create.desc())[:10] ] comment_topic_dict = { i.id: i.to_dict() for i in Topics.query.filter( Topics.id.in_(set([i['topic_id'] for i in comment_list])), Topics.available == 1 ) } comment_topic_list = [i for i in comment_topic_dict.values()] user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set(i['user_id'] for i in comment_topic_list)), Users.available == 1 ) } tab_dict = { i.id: i.to_dict() for i in Tabs.query.filter( Tabs.id.in_(set(i['tab_id'] for i in comment_topic_list)), Tabs.available == 1 ) } sub_tab_dict = { i.id: i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set(i['sub_tab_id'] for i in comment_topic_list)), SubTabs.available == 1 ) } for topic in comment_topic_list: comment_topic_dict[topic['id']]['user'] = user_dict[topic['user_id']] comment_topic_dict[topic['id']]['tab'] = tab_dict[topic['tab_id']] comment_topic_dict[topic['id']]['sub_tab'] = sub_tab_dict[topic['sub_tab_id']] for comment in comment_list: comment['is_thank'] = False comment['topic'] = comment_topic_dict[comment['topic_id']] if session.get('user_info'): comment_thank_id_list = [i.comment_id for i in CommentThank.filter_by_query(user_id=session['user_info']['id'])] for comment in comment_list: if comment['id'] in comment_thank_id_list: comment['is_thank'] = True data['comments'] = comment_list if user_info['privacy_level'] == 1 and not session.get('is_login'): data['is_open'] = False data['topics'] = [] return data if user_info['privacy_level'] == 2: if not session.get('user_info') or session.get('user_info')['id'] != user_info['id']: data['is_open'] = False data['topics'] = [] return data topic_list = [ i.to_dict() for i in Topics.filter_by_query(user_id=user_info['id']).order_by(Topics.time_create.desc())[:10] ] last_reply_user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set(i['last_reply_user_id'] for i in topic_list)), Users.available == 1 ) } tab_dict = { i.id: i.to_dict() for i in Tabs.query.filter(Tabs.id.in_(set(i['tab_id'] for i in topic_list)), Tabs.available == 1) } sub_tab_dict = { i.id: i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set(i['sub_tab_id'] for i in topic_list)), SubTabs.available == 1 ) } comment_count_dict = dict() for c in Comments.query.filter( Comments.topic_id.in_(set(i['id'] for i in topic_list)), Comments.available == 1 ): if comment_count_dict.get(c.topic_id): comment_count_dict[c.topic_id] += 1 else: comment_count_dict[c.topic_id] = 1 up_down_dict_list = defaultdict(list) for up_down_obj in TopicUpDown.query.filter( TopicUpDown.topic_id.in_(set(i['id'] for i in topic_list)), TopicUpDown.available == 1 ): up_down_dict_list[up_down_obj.topic_id].append(up_down_obj.action) for _t in topic_list: if _t['last_reply_user_id']: _t['last_reply_user'] = last_reply_user_dict[_t['last_reply_user_id']] _t['tab'] = tab_dict[_t['tab_id']] _t['sub_tab'] = sub_tab_dict[_t['sub_tab_id']] _t['comment_count'] = comment_count_dict.get(_t['id'], 0) # 获取up和down数量 if up_down_dict_list.get(_t['id']): _t['up_count'] = len([i for i in up_down_dict_list[_t['id']] if i is True]) _t['down_count'] = len([i for i in up_down_dict_list[_t['id']] if i is False]) else: _t['up_count'] = 0 _t['down_count'] = 0 data['topics'] = topic_list return data
def get(self): data = dict() user_info = Users.get_by_query(username=self.input.username).to_dict(remove_fields_list=['password']) data['user'] = user_info if user_info['privacy_level'] == 1 and not session.get('is_login'): raise LogicError('没有权限访问此用户的信息哦') if user_info['privacy_level'] == 2: if not session.get('user_info') or session.get('user_info')['id'] != user_info['id']: raise LogicError('没有权限访问此用户的信息哦') topic_objs = Topics.filter_by_query(user_id=user_info['id']) topic_count = topic_objs.count() data['total'] = topic_count if topic_count > 100: page = int(self.input.page) if self.input.page else 1 page_size = 50 topic_objs = topic_objs.order_by( Topics.time_create.desc() ).order_by(Topics.id.desc()).offset((page - 1) * page_size).limit(page_size) else: topic_objs = topic_objs.order_by(Topics.time_modify.desc()).order_by(Topics.id.desc()) topic_list = [_t.to_dict() for _t in topic_objs] last_reply_user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set(i['last_reply_user_id'] for i in topic_list)), Users.available == 1 ) } tab_dict = { i.id: i.to_dict() for i in Tabs.query.filter(Tabs.id.in_(set(i['tab_id'] for i in topic_list)), Tabs.available == 1) } sub_tab_dict = { i.id: i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set(i['sub_tab_id'] for i in topic_list)), SubTabs.available == 1 ) } comment_count_dict = dict() for c in Comments.query.filter( Comments.topic_id.in_(set(i['id'] for i in topic_list)), Comments.available == 1 ): if comment_count_dict.get(c.topic_id): comment_count_dict[c.topic_id] += 1 else: comment_count_dict[c.topic_id] = 1 up_down_dict_list = defaultdict(list) for up_down_obj in TopicUpDown.query.filter( TopicUpDown.topic_id.in_(set(i['id'] for i in topic_list)), TopicUpDown.available == 1 ): up_down_dict_list[up_down_obj.topic_id].append(up_down_obj.action) for _t in topic_list: if _t['last_reply_user_id']: _t['last_reply_user'] = last_reply_user_dict[_t['last_reply_user_id']] _t['tab'] = tab_dict[_t['tab_id']] _t['sub_tab'] = sub_tab_dict[_t['sub_tab_id']] _t['comment_count'] = comment_count_dict.get(_t['id'], 0) # 获取up和down数量 if up_down_dict_list.get(_t['id']): _t['up_count'] = len([i for i in up_down_dict_list[_t['id']] if i is True]) _t['down_count'] = len([i for i in up_down_dict_list[_t['id']] if i is False]) else: _t['up_count'] = 0 _t['down_count'] = 0 data['list'] = topic_list return data
def get(self): data = Topics.get_by_uid(self.input.uid).to_dict() data['user'] = Users.get_by_id(data['user_id']).to_dict() if data['last_reply_user_id']: data['last_reply_user'] = Users.get_by_id( data['last_reply_user_id']).to_dict() data['tab'] = Tabs.get_by_id(data['tab_id']).to_dict() data['sub_tab'] = SubTabs.get_by_id(data['sub_tab_id']).to_dict() data['appends'] = [ i.to_dict() for i in TopicAppends.filter_by_query(topic_id=data['id']) ] comment_objs = Comments.filter_by_query(topic_id=data['id']) data['comment_count'] = comment_objs.count() if data['comment_count'] < 100: comment_list = [i.to_dict() for i in comment_objs] for index, comment in enumerate(comment_list): comment['index'] = index + 1 else: page_size = 50 page = int(self.input.page) if self.input.page else 1 comment_objs = comment_objs.offset( (page - 1) * page_size).limit(page_size) comment_list = [i.to_dict() for i in comment_objs] for index, comment in enumerate(comment_list): comment['index'] = (page - 1) * page_size + index + 1 comment_user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set( i['user_id'] for i in comment_list)), Users.available == 1) } for comment in comment_list: comment['user'] = comment_user_dict[comment['user_id']] comment['is_thank'] = False data['up_count'] = TopicUpDown.filter_by_query(topic_id=data['id'], action=True).count() data['down_count'] = TopicUpDown.filter_by_query(topic_id=data['id'], action=False).count() data['is_fav'] = False data['is_thank'] = False if session.get('user_info'): if TopicFav.filter_by_query( topic_id=data['id'], user_id=session['user_info']['id']).first(): data['is_fav'] = True if TopicThank.filter_by_query( topic_id=data['id'], user_id=session['user_info']['id']).first(): data['is_thank'] = True comment_thank_objs = CommentThank.query.filter( CommentThank.comment_id.in_( set([i['id'] for i in comment_list])), CommentThank.available == 1) comment_thank_id_list = [i.comment_id for i in comment_thank_objs] for comment in comment_list: if comment['id'] in comment_thank_id_list: comment['is_thank'] = True data['comments'] = comment_list return data
def get(self): data = dict() user_info = Users.get_by_query(username=self.input.username).to_dict(remove_fields_list=['password']) data['user'] = user_info comment_objs = Comments.filter_by_query(user_id=user_info['id']) comment_count = comment_objs.count() data['total'] = comment_count if comment_count > 100: page = int(self.input.page) if self.input.page else 1 page_size = 50 topic_objs = comment_objs.order_by( Comments.time_create.desc() ).order_by(Comments.id.desc()).offset((page - 1) * page_size).limit(page_size) else: topic_objs = comment_objs.order_by(Comments.time_modify.desc()).order_by(Comments.id.desc()) comment_list = [_t.to_dict() for _t in topic_objs] comment_topic_dict = { i.id: i.to_dict() for i in Topics.query.filter( Topics.id.in_(set([i['topic_id'] for i in comment_list])), Topics.available == 1 ) } comment_topic_list = [i for i in comment_topic_dict.values()] user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set(i['user_id'] for i in comment_topic_list)), Users.available == 1 ) } tab_dict = { i.id: i.to_dict() for i in Tabs.query.filter( Tabs.id.in_(set(i['tab_id'] for i in comment_topic_list)), Tabs.available == 1 ) } sub_tab_dict = { i.id: i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set(i['sub_tab_id'] for i in comment_topic_list)), SubTabs.available == 1 ) } for topic in comment_topic_list: comment_topic_dict[topic['id']]['user'] = user_dict[topic['user_id']] comment_topic_dict[topic['id']]['tab'] = tab_dict[topic['tab_id']] comment_topic_dict[topic['id']]['sub_tab'] = sub_tab_dict[topic['sub_tab_id']] for comment in comment_list: comment['is_thank'] = False comment['topic'] = comment_topic_dict[comment['topic_id']] if session.get('user_info'): comment_thank_id_list = [i.comment_id for i in CommentThank.filter_by_query(user_id=session['user_info']['id'])] for comment in comment_list: if comment['id'] in comment_thank_id_list: comment['is_thank'] = True data['list'] = comment_list return data
def get(self): if self.input.uid: obj = Topics.get_by_uid(self.input.uid) data = obj.to_dict() data['user'] = Users.get_by_id( data['user_id']).to_dict(remove_fields_list=['password']) if data['last_reply_user_id']: data['last_reply_user'] = Users.get_by_id( data['last_reply_user_id']).to_dict( remove_fields_list=['password']) data['tab'] = Tabs.get_by_id(data['tab_id']).to_dict() data['sub_tab'] = SubTabs.get_by_id(data['sub_tab_id']).to_dict() data['appends'] = [ i.to_dict() for i in TopicAppends.filter_by_query(topic_id=data['id']) ] comment_list = [ i.to_dict() for i in Comments.filter_by_query(topic_id=data['id']) ] comment_user_dict = { i.id: i.to_dict() for i in Users.query.filter( Users.id.in_(set( i['user_id'] for i in comment_list)), Users.available == 1) } for comment in comment_list: comment['user'] = comment_user_dict[comment['user_id']] data['comments'] = comment_list data['comment_count'] = len(comment_list) data['up_count'] = TopicUpDown.filter_by_query( topic_id=data['id'], action=True).count() data['down_count'] = TopicUpDown.filter_by_query( topic_id=data['id'], action=False).count() else: data = Topics.filter_by_query() if self.input.tab_id: data.filter_by_query(tab_id=self.input.tab_id) elif self.input.sub_tab_id: data.filter_by_query(sub_tab_id=self.input.sub_tab_id) if self.input.sk_ and self.input.sv_: data = data.filter( getattr(Topics, self.input.sk_).like(f'%{self.input.sv_}%')) if self.input.odb_ and self.input.odt_ in ['asc', 'desc']: data = data.order_by( getattr(getattr(Topics, self.input.odb_), self.input.odt_)()) else: data = data.order_by(Topics.time_create.desc()) total = None if self.input.page and self.input.page_size: total = data.count() data = data.offset( (int(self.input.page) - 1) * int(self.input.page_size)).limit(int(self.input.page_size)) data = [_t.to_dict() for _t in data] user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set( i['user_id'] for i in data)), Users.available == 1) } last_reply_user_dict = { i.id: i.to_dict(remove_fields_list=['password']) for i in Users.query.filter( Users.id.in_(set(i['last_reply_user_id'] for i in data)), Users.available == 1) } tab_dict = { i.id: i.to_dict() for i in Tabs.query.filter( Tabs.id.in_(set( i['tab_id'] for i in data)), Tabs.available == 1) } sub_tab_dict = { i.id: i.to_dict() for i in SubTabs.query.filter( SubTabs.id.in_(set(i['sub_tab_id'] for i in data)), SubTabs.available == 1) } comment_count_dict = dict() for c in Comments.query.filter( Comments.topic_id.in_(set(i['id'] for i in data)), Comments.available == 1): if comment_count_dict.get(c.topic_id): comment_count_dict[c.topic_id] += 1 else: comment_count_dict[c.topic_id] = 1 up_down_dict_list = defaultdict(list) for up_down_obj in TopicUpDown.query.filter( TopicUpDown.topic_id.in_(set(i['id'] for i in data)), TopicUpDown.available == 1): up_down_dict_list[up_down_obj.topic_id].append( up_down_obj.action) for _d in data: _d['user'] = user_dict[_d['user_id']] if _d['last_reply_user_id']: _d['last_reply_user'] = last_reply_user_dict[ _d['last_reply_user_id']] _d['tab'] = tab_dict[_d['tab_id']] _d['sub_tab'] = sub_tab_dict[_d['sub_tab_id']] _d['comment_count'] = comment_count_dict.get(_d['id'], 0) # 获取up和down数量 if up_down_dict_list.get(_d['id']): _d['up_count'] = len( [i for i in up_down_dict_list[_d['id']] if i is True]) _d['down_count'] = len( [i for i in up_down_dict_list[_d['id']] if i is False]) else: _d['up_count'] = 0 _d['down_count'] = 0 if total is not None: data = {'total': total, 'list': data} return data