Esempio n. 1
0
def get_quest(quest_sn: int):
    """ """
    quest = drecall(ts_quest(quest_sn=quest_sn))

    data = {}
    data['quest_sn']   = quest.quest_sn
    data['repos_sn']   = quest.repos_sn
    data['created_ts'] = quest.created_ts
    data['updated_ts'] = quest.updated_ts

    data['editing_text'] = quest.editing_text
    data['testing_text'] = quest.testing_text

    data['purpose'] = {
        'testing': bool(quest.purpose_testing),
        'exercising': bool(quest.purpose_exercising)
    }

    # question_style     = datt(int,  doc='题型')
    # #
    if quest.question_style is not None:
        label = drecall(ts_label(label_sn=quest.question_style))
        if not label:
            busilogic.fail('找不到题型标签:%d' % quest.question_style)

        data['question_style'] = label.label

    data['saveforlater'] = get_saveforlater(quest_sn)
    data['tags']         = get_labels(quest_sn, 'tags')
    data['categories']   = get_labels(quest_sn, 'categories')

    return data
Esempio n. 2
0
def purge_quest_(quest_sn: int):
    """  """

    quest = drecall(ts_quest(quest_sn = quest_sn))
    if not quest:
        busilogic.fail('所删除的试题(%s)不存在' % quest_sn)

    dbc << "DELETE FROM ts_quest WHERE quest_sn=%(quest_sn)s"
    dbc << (quest_sn,)
Esempio n. 3
0
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
Esempio n. 4
0
def get_labels(quest_sn: int, target):
    """"""

    if target == 'tags':
        label_type = 'T'
    elif target == 'categories':
        label_type = 'C'
    else:
        busilogic.fail('!');

    return _get_labels(quest_sn, label_type)
Esempio n. 5
0
def delete_repos(repos_sn: int):
    """  """

    repos = drecall(ts_repos(repos_sn = repos_sn))
    if not repos:
        busilogic.fail('所删除的题目库(%s)不存在' % repos_sn)

    dbc << "DELETE FROM ts_repos WHERE repos_sn=%s"
    dbc << (repos_sn,)
    if dbc.rowcount < 1:
        busilogic.fail('无删除对象: %s' % repos_sn)
Esempio n. 6
0
def recycle_quest_(quest_sn: int):
    """  """

    trash = drecall(ts_quest_trashed(quest_sn = quest_sn))
    if not trash:
        busilogic.fail('所删除的试题(%s)不存在' % quest_sn)

    quest = ts_quest(trash)
    dmerge(quest)

    dbc << "DELETE FROM ts_quest_trashed WHERE quest_sn=%(quest_sn)s"
    dbc << (quest_sn,)
Esempio n. 7
0
def trash_quest(quest_sn: int):
    """  """

    quest = drecall(ts_quest(quest_sn = quest_sn))
    if not quest:
        busilogic.fail('所删除的试题(%s)不存在' % quest_sn)


    trash = ts_quest_trashed(quest)
    dmerge(trash)

    dbc << "DELETE FROM ts_quest WHERE quest_sn=%(quest_sn)s"
    dbc << dict(quest_sn=quest_sn)
Esempio n. 8
0
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)
Esempio n. 9
0
def put_labels(quest_sn: int, target, json_arg):
    """"""

    quest = drecall(ts_quest(quest_sn=quest_sn))
    if not quest:
        busilogic.fail('没找到试题: %d', quest_sn)

    if target == 'tags':
        label_type = 'T'

    elif target == 'categories':
        label_type = 'C'
    else:
        busilogic.fail('!');

    _put_labels(quest.repos_sn, quest_sn, json_arg, label_type)

    return _get_labels(quest.quest_sn, label_type)
Esempio n. 10
0
def _create_user(user : NewUserForm) -> UserInfo:
    """创建用户"""

    sys_roles = ['sysuser']
    if user.sys_roles:
        sys_roles += user.sys_roles

    user = NewUserForm(user,
                       user_sn = sys_user_seqno(),
                       created = datetime.now(),
                       state = 'normal',
                       sys_roles = sys_roles)

    dbc << "SELECT user_sn FROM sys_user WHERE login_id=%(login_id)s"
    dbc << dict(login_id=user.login_id)
    used = next(dbc)
    if used:
        err = '登录ID(%s)已经被(%d)使用' % (user.login_id, used.user_sn)
        busilogic.fail(err)

    dmerge(user)

    return get_user(user.user_sn)