Exemplo n.º 1
0
def test_expando_create():
    e = Expando(dict(answer=42))
    assert e.answer == 42
    assert e['answer'] == 42
    assert 'answer' in e

    d = expando_to_dict(e)
    assert len(d) == 1
    assert d['answer'] == 42
Exemplo n.º 2
0
async def show_todos(q: Q):
    # Get items for this user.
    db: WaveDB = q.app.db

    # Check if we have any updates, i.e. the user has checked/unchecked any item.
    updates = []
    for key, done in expando_to_dict(q.args).items():
        # We've named each todo item `todo_{id}' (e.g. todo_42, todo_43, and so on)
        # So identify the todo items from their 'todo_' prefix, then extract the ids from the names.
        if key.startswith('todo_'):
            _, id = key.split('_', 1)
            updates.append(('update todo set done=? where id=?',
                            1 if done else 0, int(id)))

    # If we have updates, update our database.
    if len(updates):
        _, err = await db.exec_many(*updates)
        if err:
            raise RuntimeError(f'Failed updating todos: {err}')

    # Fetch latest todos for our user
    rows, err = await db.exec('select id, label, done from todo where user=?',
                              q.auth.subject)
    if err:
        raise RuntimeError(f'Failed fetching todos: {err}')
    todos = [TodoItem(id, label, done) for id, label, done in rows]

    # Create done/not-done checkboxes.
    done = [
        ui.checkbox(name=f'todo_{todo.id}',
                    label=todo.label,
                    value=True,
                    trigger=True) for todo in todos if todo.done
    ]
    not_done = [
        ui.checkbox(name=f'todo_{todo.id}', label=todo.label, trigger=True)
        for todo in todos if not todo.done
    ]

    # Display list
    q.page['form'] = ui.form_card(
        box='1 1 4 10',
        items=[
            ui.text_l('To Do'),
            ui.button(name='new_todo', label='Add To Do...', primary=True),
            *not_done,
            *([ui.separator('Done')] if len(done) else []),
            *done,
        ])
    await q.page.save()
Exemplo n.º 3
0
async def serve(q: Q):
    if not q.client.initialized:
        init(q)
        q.client.filters = {}
        q.client.initialized = True

    args = expando_to_dict(q.args)
    for arg in args:
        if str(arg).startswith('filter_'):
            handle_filter(q, str(arg).replace('filter_', ''), q.args[arg])

    sidebar_items = q.page['sidebar'].items
    if q.args.filter_categories:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[2].dropdown.value = q.args.filter_categories
    if q.args.filter_city:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[3].dropdown.value = q.args.filter_city
    if q.args.filter_country:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[4].dropdown.value = q.args.filter_country
    if q.args.filter_postalCode:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[5].dropdown.value = q.args.filter_postalCode
    if q.args.filter_province:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[6].dropdown.value = q.args.filter_province
    if q.args.filter_rating:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[7].dropdown.value = q.args.filter_rating
    if q.args.filter_userCity:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[8].dropdown.value = q.args.filter_userCity
    if q.args.filter_userProvince:
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[9].dropdown.value = q.args.filter_userProvince
    if q.args.review:
        q.client.review = q.args.review
        # TODO: Remove after https://github.com/h2oai/wave/issues/150 gets resolved.
        sidebar_items[0].dropdown.value = q.client.review
        q.page['original'].image = plot_word_cloud(
            config.dataset[q.client.review], q)

    if q.client.filters:
        render_diff_word_cloud(q)
    else:
        del q.page['diff']

    await q.page.save()
Exemplo n.º 4
0
def test_expando_dict_write():
    e = Expando(dict(answer=42))
    d = expando_to_dict(e)
    d['answer'] = 43
    assert e['answer'] == 43
Exemplo n.º 5
0
def test_expando_empty():
    e = Expando()
    d = expando_to_dict(e)
    assert len(d) == 0