예제 #1
0
def test_sequence():

    r1 = t_a(b=101)
    r2 = t_a(b=102)
    print('R1: ', r1, '\nR2: ', r2, sep='')
    assert isinstance(r1.a, seq_test) and  isinstance(r2.a, seq_test)
    assert r1.a is not r2.a
    assert r1.a != r2.a

    ASet = dset(t_a)
    s1 = ASet()
    s1 += [r1, r2]
    dmerge(s1)

    print('R1: ', r1, '\nR2: ', r2, sep='')
    assert isinstance(r1.a, seq_test) and  isinstance(r2.a, seq_test)
    assert r1.a is not r2.a
    assert r1.a != r2.a
    assert r1.a.value == 10000 and int(r1.a) == 10000
    assert r2.a.value == 10001 and int(r2.a) == 10001


    dbc << "SELECT * FROM t_a ORDER BY a"
    s1 = ASet(dbc)
    sl = list(s1)
    print(s1)
    assert isinstance(sl[0].a, seq_test)
    assert sl[0].a.value == 10000
    assert sl[0].a == r1.a
예제 #2
0
def test_dobject_dmerge():

    # orginal data
    ASet = dset(t_a)

    ds = ASet([t_a(a=101, b=102, c=103, d=104, e=105),
               t_a(a=201, b=202, c=203, d=204, e=205)])

    t_a1 = t_a._re(_ignore=['c', 'e'])
    A1Set = dset(t_a1)

    ds1 = A1Set(ds)

    dmerge(ds1)

    dbc << "SELECT * FROM t_a"
    ds0 = A1Set(dbc) # original
    ds1 = A1Set(ds0) # to be modified

    ds1 += [t_a1(a=301, b=302, d=304, f=306)] # insert a new item
    ds1[t_a(a=101, b=102)].f = 5555 # update the first item
    del ds1[t_a(a=201, b=202)] # delete the 2nd item

    print('ORIGINAL:', ds0)
    print('MODIFIED:', ds1)

    dmerge(ds1, ds0)
예제 #3
0
def test_dobject_dmerge():

    # orginal data
    ASet = dset(t_a)

    ds = ASet([
        t_a(a=101, b=102, c=103, d=104, e=105),
        t_a(a=201, b=202, c=203, d=204, e=205)
    ])

    t_a1 = t_a._re(_ignore=['c', 'e'])
    A1Set = dset(t_a1)

    ds1 = A1Set(ds)

    dmerge(ds1)

    dbc << "SELECT * FROM t_a"
    ds0 = A1Set(dbc)  # original
    ds1 = A1Set(ds0)  # to be modified

    ds1 += [t_a1(a=301, b=302, d=304, f=306)]  # insert a new item
    ds1[t_a(a=101, b=102)].f = 5555  # update the first item
    del ds1[t_a(a=201, b=202)]  # delete the 2nd item

    print('ORIGINAL:', ds0)
    print('MODIFIED:', ds1)

    dmerge(ds1, ds0)
예제 #4
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))
예제 #5
0
파일: label.py 프로젝트: tjpumis/alchemy
def get_update_labels(repos_sn, label_texts, label_type):

    found_labels = find_labels(repos_sn, label_texts, label_type)

    now = datetime.utcnow();

    LabelSet = dset(ts_label)

    new_labels = LabelSet()
    for text in label_texts:
        if text in found_labels:
            continue

        label = ts_label(repos_sn=repos_sn, label_sn=ts_label_seqno())
        label.label=text
        label.type = label_type
        label.created_ts = now
        label.updated_ts = now

        new_labels._add(label)

    dmerge(new_labels)

    for item in new_labels:
        found_labels[item.label] = item.label_sn

    return found_labels
예제 #6
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
예제 #7
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))
예제 #8
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
예제 #9
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)
예제 #10
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)
예제 #11
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)
예제 #12
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)
예제 #13
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,)
예제 #14
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)
예제 #15
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)
예제 #16
0
def test_array():

    ASet = dset(t_a)
    s1 = ASet()

    r1 = t_a(a=1)
    s1 += [r1]
    s1[0].b = ['a', 'b']
    assert s1[0].b == ['a', 'b']

    dmerge(s1)

    dbc << "SELECT * FROM t_a ORDER BY a"
    s2 = ASet(dbc)
    assert s2[0].b == ['a', 'b']

    print(s2)
예제 #17
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)
예제 #18
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
예제 #19
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
예제 #20
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
예제 #21
0
파일: user.py 프로젝트: tjpumis/alchemy
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)
예제 #22
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)]
예제 #23
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)]
예제 #24
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)
예제 #25
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)
예제 #26
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)