예제 #1
0
파일: quest.py 프로젝트: tjpumis/alchemy
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
예제 #2
0
def test_recall1():

    ASet = dset(t_b, _key=dict(a=datt(int)))  # define a key of dset

    ds1 = ASet(a=11)
    ds1 += [t_b(b=12, c=13, d=14), t_b(b=22, c=23, d=24)]
    dmerge(ds1)

    r1 = drecall(t_b(a=11, b=12))
    assert r1.a == 11 and r1.b == 12 and r1.c == 13
    r1 = drecall(t_b(a=99, b=12))  # no found and return empty dobject
    assert r1 is not None and not bool(r1)
예제 #3
0
def test_recall1():

    ASet = dset(t_b, _key=dict(a=datt(int))) # define a key of dset

    ds1 = ASet(a=11)
    ds1 += [t_b(b=12, c=13, d= 14), t_b(b=22, c=23, d=24)]
    dmerge(ds1)

    r1 = drecall(t_b(a=11, b=12))
    assert r1.a == 11 and r1.b == 12 and r1.c == 13
    r1 = drecall(t_b(a=99, b=12)) # no found and return empty dobject
    assert r1 is not None and not bool(r1)
예제 #4
0
파일: repos.py 프로젝트: tjpumis/alchemy
def save_repos(repos_sn: int, repos: ts_repos):
    """保存题库信息"""

    now = datetime.utcnow();
    user_sn = webreq.principal_id

    if not repos_sn or repos.repos_sn == 0:
        repos = ts_repos(repos, repos_sn = ts_repos_seqno())
        repos.updated_ts = now
        repos.created_ts = now

        dmerge(repos)

        repos_user = ts_repos_user(repos_sn=repos.repos_sn, user_sn=user_sn)
        repos_user.type = 'O'
        repos_user.updated_ts = now

        dmerge(repos_user)

        return repos

    RestrictedHost = ts_repos._re(_ignore=['created_ts'])
    repos = RestrictedHost(repos, updated_ts = now)
    original = drecall(repos)
    original.updated_ts = now
    dmerge(repos, original)

    return repos
예제 #5
0
파일: user.py 프로젝트: tjpumis/alchemy
def _set_password(passwd: PasswordForm):
    """设置密码"""

    passwd.passwd = hash_passwd(passwd.passwd)
    passwd.passwd_upd = datetime.now()

    dmerge(passwd, origin = drecall(passwd))
예제 #6
0
파일: quest.py 프로젝트: tjpumis/alchemy
def get_saveforlater(quest_sn: int):

    saveforlater = drecall(ts_quest_saveforlater(quest_sn=quest_sn))
    if not saveforlater:
        return None

    return saveforlater
예제 #7
0
파일: label.py 프로젝트: tjpumis/alchemy
def add_tags(repos_sn, json_arg):
    """  """

    now = datetime.utcnow();
    label_texts = re.split('\s*[;,,;/.。、\s]+\s*', json_arg['text'])

    labels = get_update_labels(repos_sn, label_texts, label_type="T")

    new_set = set(labels.values())

    orig_tags = drecall(ts_repos_tags(repos_sn=repos_sn))
    if orig_tags:
        old_set = set(orig_tags.labels)
        if old_set == new_set:
            return

        new_set = new_set.union(old_set)

    tags = ts_repos_tags(repos_sn=repos_sn)
    tags.labels = sorted(int(x) for x in new_set)
    tags.updated_ts = now

    dmerge(tags, orig_tags)

    return
예제 #8
0
파일: trash.py 프로젝트: tjpumis/alchemy
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,)
예제 #9
0
파일: user.py 프로젝트: tjpumis/alchemy
def save_user(user: UserForm):
    """"""

    assert_roles('sysadmin')

    user.sys_roles = list(sorted(set(user.sys_roles)))
    # print(user.sys_roles)

    dmerge(user, drecall(user))
예제 #10
0
파일: user.py 프로젝트: tjpumis/alchemy
def get_user(user_sn: int) -> UserInfo:
    """取得用户信息"""

    # if user_sn == 'me' :
    #     user_sn = webreq.principal_id
    #     if user_sn is None:
    #         busilogic.unauthorized("当前用户未验证身份")

    return drecall(UserInfo(user_sn=user_sn))
예제 #11
0
파일: quest.py 프로젝트: tjpumis/alchemy
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
예제 #12
0
파일: repos.py 프로젝트: tjpumis/alchemy
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)
예제 #13
0
def test_recall_without_dset_key():
    ASet = dset(t_b)  # define a key of dset

    ds1 = ASet()
    ds1 += [t_b(a=1, b=12, c=13, d=14), t_b(a=1, b=22, c=23, d=24)]
    ds1 += [t_b(a=1, b=32, c=33, d=34), t_b(a=1, b=42, c=43, d=44)]
    dmerge(ds1)

    ds2 = drecall(ASet())
    assert len(ds2) == 4
    print(ds2)
예제 #14
0
def test_recall_without_dset_key():
    ASet = dset(t_b) # define a key of dset

    ds1 = ASet()
    ds1 += [t_b(a=1, b=12, c=13, d= 14), t_b(a=1, b=22, c=23, d=24)]
    ds1 += [t_b(a=1, b=32, c=33, d= 34), t_b(a=1, b=42, c=43, d=44)]
    dmerge(ds1)

    ds2 = drecall(ASet())
    assert len(ds2) == 4
    print(ds2)
예제 #15
0
파일: trash.py 프로젝트: tjpumis/alchemy
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,)
예제 #16
0
def test_recall2():

    ASet = dset(t_b, _key=dict(a=datt(int)))  # define a key of dset

    ds1 = ASet(a=1)
    ds1 += [t_b(b=12, c=13, d=14), t_b(b=22, c=23, d=24)]
    dmerge(ds1)

    ds2 = ASet(a=2)
    ds2 += [t_b(b=32, c=33, d=34), t_b(b=42, c=43, d=44)]
    dmerge(ds2)

    ds1 = drecall(ASet(a=1))
    ds1 += [t_b(b=12, c=113, d=114), t_b(b=62, c=63, d=64)]
    del ds1[t_b(b=22)]
    with pytest.raises(KeyError):
        del ds1[t_b(b=55)]
    ds0 = drecall(ASet(a=1))
    print("O: ", ds0)
    print("N: ", ds1)
    dmerge(ds1, ds0)
예제 #17
0
def test_recall2():

    ASet = dset(t_b, _key=dict(a=datt(int))) # define a key of dset


    ds1 = ASet(a=1)
    ds1 += [t_b(b=12, c=13, d= 14), t_b(b=22, c=23, d=24)]
    dmerge(ds1)

    ds2 = ASet(a=2)
    ds2 += [t_b(b=32, c=33, d= 34), t_b(b=42, c=43, d=44)]
    dmerge(ds2)

    ds1 = drecall(ASet(a=1))
    ds1 += [t_b(b=12, c=113, d=114), t_b(b=62, c=63, d=64)]
    del ds1[t_b(b=22)]
    with pytest.raises(KeyError):
        del ds1[t_b(b=55)]
    ds0 = drecall(ASet(a=1))
    print("O: ", ds0)
    print("N: ", ds1)
    dmerge(ds1, ds0)
예제 #18
0
파일: quest.py 프로젝트: tjpumis/alchemy
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)
예제 #19
0
파일: quest.py 프로젝트: tjpumis/alchemy
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)
예제 #20
0
파일: quest.py 프로젝트: tjpumis/alchemy
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)
예제 #21
0
파일: label.py 프로젝트: tjpumis/alchemy
def delete_repos_tags(repos_sn: int, label_sn:int):
    """  """

    now = datetime.utcnow();

    orig_tags = drecall(ts_repos_tags(repos_sn=repos_sn))

    tags = ts_repos_tags(orig_tags)
    tags.labels = [] + tags.labels
    try:
        pos = tags.labels.index(label_sn)
    except:
        return

    del tags.labels[pos]

    tags.updated_ts = now

    dmerge(tags, orig_tags)
예제 #22
0
파일: user.py 프로젝트: tjpumis/alchemy
def login(login: LoginForm):

    if not login.login_id :
        busilogic.unauthorized("登录ID不能为空")

    login_id  = login.login_id
    passwd = login.passwd

    login = drecall(login)
    if not login :
        busilogic.unauthorized("无效登录ID:'%s'" % login_id)

    if not check_password(passwd, login.passwd) :
        busilogic.unauthorized("登录ID(%s)密码验证失败" % login_id)

    webreq.principal_id = str(login.user_sn)

    user = get_user(login.user_sn)
    busilogic.logger.info('User %s logged in' % user.user_name)
    return user
예제 #23
0
파일: label.py 프로젝트: tjpumis/alchemy
def delete_repos_queststyles(repos_sn: int, label_sn:int):
    """  """

    now = datetime.utcnow();

    orig_queststyles = drecall(ts_repos_queststyles(repos_sn=repos_sn))

    queststyles = ts_repos_queststyles(orig_queststyles)
    queststyles.labels = [] + queststyles.labels
    try:
        pos = queststyles.labels.index(label_sn)
    except:
        return

    del queststyles.labels[pos]

    queststyles.updated_ts = now

    dmerge(queststyles, orig_queststyles)

    return
예제 #24
0
파일: quest.py 프로젝트: tjpumis/alchemy
def put_saveforlater(quest_sn: int, json_arg):
    """创建或保存试题"""

    status = bool(json_arg['status']) # true or false
    if status:
        old_sfl = drecall(ts_quest_saveforlater(quest_sn=quest_sn))

        new_sfl = ts_quest_saveforlater(quest_sn=quest_sn)

        new_sfl.updated_ts = datetime.utcnow() if status else None
        dmerge(new_sfl, old_sfl)

        return new_sfl
    else:
        dbc << """
        DELETE FROM ts_quest_saveforlater
        WHERE quest_sn = %(quest_sn)s
        """
        dbc << dict(quest_sn=quest_sn)

        return None
예제 #25
0
def test_case2():

    ASet = dset(test_a, _key=dict(sn=datt(int)))
    ds1 = ASet(sn=101)
    for i in range(10):
        ds1 += [test_a(line=i, name='L%03d' % i)]

    ds1 = ASet(sn=202)
    for i in range(10):
        ds1 += [test_a(line=i, name='L%03d' % i)]

    dmerge(ds1)

    page = DPage(start=2, limit=3, sortable='+sn,-line')
    ds1 = ASet(sn=101, _page=page)
    ds2 = drecall(ds1)

    print('ds: ', ds2)

    assert len(ds2) == 3
    assert ds2[test_a(line=7)].line == 7
    assert not ds2[test_a(line=4)]
    assert not ds2[test_a(line=8)]
예제 #26
0
def test_case2():

    ASet = dset(test_a, _key=dict(sn=datt(int)))
    ds1 = ASet(sn=101)
    for i in range(10):
        ds1 += [test_a(line=i, name='L%03d' % i)]

    ds1 = ASet(sn=202)
    for i in range(10):
        ds1 += [test_a(line=i, name='L%03d' % i)]

    dmerge(ds1)

    page = DPage(start=2, limit=3, sortable='+sn,-line')
    ds1 = ASet(sn=101, _page=page)
    ds2 = drecall(ds1)

    print('ds: ', ds2)

    assert len(ds2) == 3
    assert ds2[test_a(line=7)].line == 7
    assert not ds2[test_a(line=4)]
    assert not ds2[test_a(line=8)]
예제 #27
0
파일: repos.py 프로젝트: tjpumis/alchemy
def get_repos_desc(repos_sn):
    """  """

    repos = drecall(ts_repos(repos_sn = repos_sn))
    return repos
예제 #28
0
파일: label.py 프로젝트: tjpumis/alchemy
def update_category(repos_sn, json_arg):
    """  """

    now = datetime.utcnow();

    text = json_arg['text'];

    print(repos_sn, text)

    labels = []
    level_1 = ''
    for ln in re.split('[\n\r]+', text):
        m = re.search('(^\s+)', ln)
        sp_num = m.end() if m else 0

        if sp_num > 0:
            labels.append(level_1 + '/' + ln.strip())
        else:
            level_1 = ln.strip()
            labels.append(level_1)


    old_labels = get_repos_categories(repos_sn)

    old_set = set(old_labels.keys())
    new_set = set(labels)

    lbl_com_set = old_set.intersection(new_set)
    lbl_ins_set = new_set.difference(lbl_com_set)


    new_labels = get_update_labels(repos_sn, lbl_ins_set, 'C')

    sn_com_list = set(old_labels[n]['label_sn'] for n in lbl_com_set)
    sn_ins_list = set(int(new_labels[n]) for n in lbl_ins_set)


    orig_categories = drecall(ts_repos_categories(repos_sn=repos_sn))



    categories = ts_repos_categories(repos_sn=repos_sn)
    categories.labels = sorted(int(x) for x in sn_ins_list.union(sn_com_list))
    categories.updated_ts = now

    dmerge(categories, orig_categories)

    ##-----------------------------------------------------------------------
    ## 保留文本指定的顺序

    orders = {}
    for i, lbl in enumerate(labels):
        orders[lbl] = i + 1

    dbc << """\
    WITH s AS (
      SELECT UNNEST(labels) AS label_sn
      FROM ts_repos_categories WHERE repos_sn = %(repos_sn)s
    )
    SELECT t.*
    FROM ts_label t JOIN s USING(label_sn)
    """
    dbc << dict(repos_sn=repos_sn)

    LabelSet = dset(ts_label)

    old_label_set = LabelSet(dbc)

    label_set = LabelSet()
    for r in old_label_set:
        r = ts_label(r)
        r.props = copy.deepcopy(r.props) # 需要复制,否则修改的是旧的
        label_set._add(r)

    for r in label_set:
        ordnum = orders[r.label]
        if r.props:
            if 'order' in r.props and r.props['order'] == ordnum:
                continue

            r.props['order'] = ordnum
        else:
            pass
            r.props = {'order': ordnum}
        r.updated_ts = now

    dmerge(label_set, old_label_set)
예제 #29
0
파일: user.py 프로젝트: tjpumis/alchemy
def list_user(page: DPage) -> DSet[UserInfo]:
    """取得用户信息"""

    UserSet = dset(UserInfo)
    users = drecall(UserSet(_page=page))
    return users
예제 #30
0
파일: repos.py 프로젝트: tjpumis/alchemy
def get_repos_brief_summary(repos_sn):
    """  """
    # TODO
    repos = drecall(ts_repos(repos_sn = repos_sn))
    return repos