def new_or_save_question(repos_sn: int, quest_sn: int, json_arg): """创建或保存试题""" now = datetime.utcnow(); user_sn = webreq.principal_id orig_quest = None if not quest_sn: # 新建的 quest = ts_quest(quest_sn=ts_quest_seqno(), repos_sn=repos_sn) quest.created_ts = now else: # 保存的 quest = drecall(ts_quest(quest_sn=quest_sn)) if not quest: busilogic.fail('找不到试题%d' % quest_sn) orig_quest = ts_quest(quest) purpose = json_arg['purpose'] if purpose: quest.purpose_testing = purpose['testing'] quest.purpose_exercising = purpose['exercising'] quest.editing_text = json_arg['editing_text'] quest.testing_text = json_arg['testing_text'] quest.blank_signature = json.dumps(json_arg['blank_signature']) if 'question_style' in json_arg: question_style = json_arg['question_style'] # 将题型转换成题型的标签号 found_labels = find_labels(repos_sn, [question_style], 'S') if question_style in found_labels: quest.question_style = found_labels[question_style] quest.updated_ts = now dmerge(quest, orig_quest) if not quest_sn: # 如果是新建的还需要,一并处理已经打的分类和标签 if 'saveforlater' in json_arg and json_arg['saveforlater']: put_saveforlater(quest.quest_sn, {'status': True}) if 'tags' in json_arg: _put_labels(repos_sn, quest.quest_sn, json_arg['tags'], 'T') if 'categories' in json_arg: _put_labels(repos_sn, quest.quest_sn, json_arg['categories'], 'C') # After it is created, a value should be returned with a new seqno return quest
def _put_labels(repos_sn, quest_sn, label_texts, label_type): """""" found_labels = find_labels(repos_sn, label_texts, label_type) not_founds = list(filter(lambda t: t not in found_labels, label_texts)) if not_founds: busilogic.fail('没找到标签号: %s' % ', '.join(not_founds)) quest_label = ts_quest_labels(quest_sn=quest_sn, type = label_type) quest_label.labels = [sn for sn in found_labels.values()] quest_label.updated_ts = datetime.utcnow() original = drecall(ts_quest_labels(quest_sn=quest_sn, type = label_type)) dmerge(quest_label, original)