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 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)