Exemplo n.º 1
0
async def test_peewee(aiohttp_client):
    app = muffin.Application('pw_admin', PEEWEE_CONNECTION='sqlite:///:memory:')
    app.install('muffin_jinja2')
    db = app.install('muffin_peewee')
    admin = app.install('muffin_admin')

    import peewee as pw
    from muffin_peewee.fields import JSONField

    @db.register
    class Model(db.TModel):

        active = pw.BooleanField()
        number = pw.IntegerField(default=1, choices=zip(range(3), range(3)))
        content = pw.CharField()
        config = JSONField(default={})

    @db.register
    class Model2(db.TModel):
        pass

    Model.create_table()
    Model2.create_table()

    from muffin_admin.peewee import PWAdminHandler

    @app.register
    class ModelHandler(PWAdminHandler):
        model = Model
        columns_exclude = 'created',
        columns_filters = 'content', 'number'
        form_exclude = 'number',

    @ModelHandler.action
    def test(handler, request):
        return 'PASSED'

    assert ModelHandler.actions
    assert ModelHandler.columns
    assert ModelHandler.columns == ['id', 'active', 'number', 'content', 'config']
    assert ModelHandler.name == 'model'
    assert ModelHandler.form
    assert ModelHandler.form.config
    assert ModelHandler.can_create
    assert ModelHandler.can_edit
    assert ModelHandler.can_delete
    assert ModelHandler.columns_formatters is not PWAdminHandler.columns_formatters

    # Make admin handler dynamically
    admin.register(Model2, can_delete=False)
    handler = admin.handlers['model2']
    assert handler.model == Model2
    assert handler.can_delete is False

    from mixer.backend.peewee import Mixer
    mixer = Mixer(commit=True)
    models = mixer.cycle(3).blend(Model, number=(n for n in (1, 2, 3)))

    client = await aiohttp_client(app)

    async with client.get('/admin/model?auth=1') as resp:
        assert resp.status == 200
        text = await resp.text()
        assert models[0].content in text
        assert models[1].content in text
        assert models[2].content in text

    async with client.get('/admin/model?pk=1&auth=1') as resp:
        assert resp.status == 200
        text = await resp.text()
        assert 'created' in text

    async with client.post('/admin/model?pk=1&auth=1', data={
            'content': 'new content'
            }) as resp:
        assert resp.status == 200

    async with client.delete('/admin/model?pk=1&auth=1'):
        assert not Model.select().where(Model.id == 1).exists()

    async with client.get('/admin/model2?auth=1') as resp:
        assert resp.status == 200

    async with client.get('/admin/model?auth=1&af-content=%s' % models[1].content) as resp:
        assert resp.status == 200
        text = await resp.text()
        assert models[1].content in text
        assert not models[2].content in text

    async with client.get('/admin/model?auth=1&af-number=%s' % models[1].number) as resp:
        assert resp.status == 200
        text = await resp.text()
        assert models[1].content in text
        assert not models[2].content in text

    async with client.get('/admin/model?auth=1&af-number=invalid') as resp:
        assert resp.status == 200
Exemplo n.º 2
0
def devdata():
    mixer = Mixer(commit=True)
    statuses = [choice[0] for choice in Message.status.choices]
    mixer.cycle(20).blend(Message, status=mixer.RANDOM(*statuses))
Exemplo n.º 3
0
def test_peewee(app, client):
    import peewee as pw
    from muffin_peewee.fields import JSONField

    @app.ps.peewee.register
    class Model(app.ps.peewee.TModel):

        active = pw.BooleanField()
        number = pw.IntegerField()
        content = pw.CharField()
        config = JSONField(default={})

    @app.ps.peewee.register
    class Model2(app.ps.peewee.TModel):
        pass

    Model.create_table()
    Model2.create_table()

    from muffin_admin.peewee import PWAdminHandler

    @app.register
    class ModelHandler(PWAdminHandler):
        model = Model
        columns_exclude = 'created',
        columns_filters = 'content', 'number'

    @ModelHandler.action
    def test(handler, request):
        return 'PASSED'

    assert ModelHandler.columns
    assert ModelHandler.columns == ['id', 'active', 'number', 'content', 'config']
    assert ModelHandler.name == 'model'
    assert ModelHandler.form
    assert ModelHandler.form.config
    assert ModelHandler.can_create
    assert ModelHandler.can_edit
    assert ModelHandler.can_delete
    assert ModelHandler.columns_formatters is not PWAdminHandler.columns_formatters

    # Make admin handler dynamically
    app.ps.admin.register(Model2, can_delete=False)
    handler = app.ps.admin.handlers['model2']
    assert handler.model == Model2
    assert handler.can_delete is False

    from mixer.backend.peewee import Mixer
    mixer = Mixer(commit=True)
    models = mixer.cycle(3).blend(Model)

    response = client.get('/admin/model?auth=1')
    assert models[0].content in response.text
    assert models[1].content in response.text
    assert models[2].content in response.text

    response = client.get('/admin/model?pk=1&auth=1')
    assert 'created' in response.text

    response = client.post('/admin/model?pk=1&auth=1', {
        'content': 'new content'
    })
    assert response.status_code == 302

    response = client.delete('/admin/model?pk=1&auth=1')
    assert not Model.select().where(Model.id == 1).exists()

    response = client.get('/admin/model2?auth=1')
    assert response.status_code == 200

    response = client.get('/admin/model?auth=1&af-content=%s' % models[1].content)
    assert models[1].content in response.text
    assert not models[2].content in response.text

    response = client.get('/admin/model?auth=1&af-number=%s' % models[1].number)
    assert models[1].content in response.text
    assert not models[2].content in response.text

    response = client.get('/admin/model?auth=1&af-number=invalid')
    assert response.status_code == 200
Exemplo n.º 4
0
def test_peewee(app, client):
    import peewee as pw
    from muffin_peewee.fields import JSONField

    @app.ps.peewee.register
    class Model(app.ps.peewee.TModel):

        active = pw.BooleanField()
        number = pw.IntegerField()
        content = pw.CharField()
        config = JSONField(default={})

    @app.ps.peewee.register
    class Model2(app.ps.peewee.TModel):
        pass

    Model.create_table()
    Model2.create_table()

    from muffin_admin.peewee import PWAdminHandler

    @app.register
    class ModelHandler(PWAdminHandler):
        model = Model
        columns_exclude = 'created',
        columns_filters = 'content', 'number'

    @ModelHandler.action
    def test(handler, request):
        return 'PASSED'

    assert ModelHandler.columns
    assert ModelHandler.columns == [
        'id', 'active', 'number', 'content', 'config'
    ]
    assert ModelHandler.name == 'model'
    assert ModelHandler.form
    assert ModelHandler.form.config
    assert ModelHandler.can_create
    assert ModelHandler.can_edit
    assert ModelHandler.can_delete
    assert ModelHandler.columns_formatters is not PWAdminHandler.columns_formatters

    # Make admin handler dynamically
    app.ps.admin.register(Model2, can_delete=False)
    handler = app.ps.admin.handlers['model2']
    assert handler.model == Model2
    assert handler.can_delete is False

    from mixer.backend.peewee import Mixer
    mixer = Mixer(commit=True)
    models = mixer.cycle(3).blend(Model)

    response = client.get('/admin/model?auth=1')
    assert models[0].content in response.text
    assert models[1].content in response.text
    assert models[2].content in response.text

    response = client.get('/admin/model?pk=1&auth=1')
    assert 'created' in response.text

    response = client.post('/admin/model?pk=1&auth=1',
                           {'content': 'new content'})
    assert response.status_code == 302

    response = client.delete('/admin/model?pk=1&auth=1')
    assert not Model.select().where(Model.id == 1).exists()

    response = client.get('/admin/model2?auth=1')
    assert response.status_code == 200

    response = client.get('/admin/model?auth=1&af-content=%s' %
                          models[1].content)
    assert models[1].content in response.text
    assert not models[2].content in response.text

    response = client.get('/admin/model?auth=1&af-number=%s' %
                          models[1].number)
    assert models[1].content in response.text
    assert not models[2].content in response.text

    response = client.get('/admin/model?auth=1&af-number=invalid')
    assert response.status_code == 200