def set_config(name, value): config = Config.query.filter(Config.name==name).first() if config is None: config = Config(name = name, value=value) else: config.value = value db.session.commit() return config.value
def set_auth_secret(secret): """ Sets the auth secret. """ from models import Config secret_config = Config() secret_config.key = "auth_secret" secret_config.value = secret secret_config.put() return secret_config
def admin_settings_update(): params = utils.flat_multi(request.form) params.pop("csrf_token") with app.app_context(): for key in params: config = Config.query.filter_by(key=key).first() if config is None: config = Config(key, params[key]) else: new = params[key] if config.value != new: config.value = params[key] db.session.add(config) db.session.commit() return { "success": 1, "message": "Success!" }
def practise(sh, ch_mode, max_index, locale=1): sh.menu.dicts = QUIZMODES sh.header.mode = WELECOMEMODES[ch_mode] # 第二层循环 # 临时存放答案 answer = '' flag = False # 对于ch_mode为 U 和 R 时,当flag等于True时,表示状态需要重置,更新题目 while True: with db_session: if ch_mode == 'Q': now_index = Config.get(key='now_index') if not now_index: now_index = Config(key='now_index', value=1) index = now_index.value if ch_mode == 'U': mark_index = Config.get(key='mark_index') if not mark_index: mark_index = Config(key='mark_index', value=1) index = mark_index.value if flag == True: quiz = Quiz.select(lambda q: q.mark == True)[:][0] index = quiz.id mark_index.value = quiz.id flag = False sh.header.sums = count(q for q in Quiz if q.mark == True) if ch_mode == 'R': rand_index = Config.get(key='rand_index') if not rand_index: rand_index = Config(key='rand_index', value=1) index = rand_index.value if flag == True: rand_index.value = randint(1, max_index) index = rand_index.value flag = False if ch_mode == 'W': ch_mode = 'Q' now_index = Config.get(key='now_index') now_index.value = locale index = locale sh.header.index = index # 显示题目 quiz = Quiz.get(id=index) star = '' if quiz.mark: star = '★' sh.main.title = '{} {}\n\n{}'.format( star, quiz.name, '\n'.join(sorted([x.name for x in quiz.items]))) sh.header.kind = quiz.kind sh.show() # 用于暂停 ch = getwch().upper() # 正确答案 quiz_answer = quiz.answer if quiz_answer == '√': quiz_answer = 'A' if quiz_answer == '×': quiz_answer = 'B' if ch in 'ABCDEFGH': if ch not in answer: answer = '{}{}'.format(answer, ch) answer_sort = ''.join(sorted(answer)) if answer_sort == quiz_answer: sh.state.title = '{}答案[{}]正确{}'.format( Fore.GREEN, answer_sort, Fore.RESET) Done(quiz=quiz, answer=answer_sort, state=True, created=datetime.now()) # 记录做题 continue sh.state.title = '{}答案[{}]错误{}'.format( Fore.RED, answer_sort, Fore.RESET) Done(quiz=quiz, answer=answer_sort, created=datetime.now()) # 记录做题 continue if ch == 'Q': sh.state.title = '正确答案[{}]'.format(quiz_answer) if ch == 'S': answer_trans = lambda x: Fore.GREEN + '正确' + Fore.RESET if x else Fore.RED + '错误' + Fore.RESET result = [ '{}\t|\t{}\t|\t{}'.format( x.created.strftime('%Y-%m-%d %H:%M:%S'), x.answer, answer_trans(x.state)) for x in quiz.dones ] result_s = [x.state for x in quiz.dones] sh.quote.title = '[{}]\n\n{}\n\n<{}>\n\n[该题总计练习{}次,正确{}次,错误{}次]\n\n{}'.format( quiz.source, quiz.memo, Fore.YELLOW + quiz.other + Fore.RESET, len(result_s), result_s.count(True), result_s.count(False), '\n'.join(sorted([x for x in result]))) if ch == 'M': if quiz.mark: quiz.mark = False else: quiz.mark = True if ch == 'R': sh.state.title = '' sh.quote.title = '' answer = '' if ch in ' PL\r': ch = ch_mode sh.state.title = '' sh.quote.title = '' answer = '' if ch_mode == 'R': rand_index.value = randint(1, max_index) flag = True #重置状态 break if ch_mode == 'U': u_list = Quiz.select(lambda q: q.mark == True)[:] if len(u_list) >= 1: i = randint(0, len(u_list) - 1) mark_index.value = u_list[i].id flag = True # 重置状态 break # Q顺序模式 now_index.value = now_index.value + 1 if now_index.value > max_index: now_index.value = max_index break if ch in 'ZK': ch = ch_mode sh.state.title = '' sh.quote.title = '' answer = '' if ch_mode == 'R': rand_index.value = randint(1, max_index) flag = True # 重置状态 break if ch_mode == 'U': u_list = Quiz.select(lambda q: q.mark == True)[:] i = randint(0, len(u_list) - 1) mark_index.value = u_list[i].id flag = True # 重置状态 break # Q now_index.value = now_index.value - 1 if now_index.value < 1: now_index.value = 1 break if ch == 'T': sh.state.title = '' sh.quote.title = '' sh.header.sums = '' answer = '' break return ch