def example_data(): """ Create example users. """ from mixer.backend.peewee import Mixer from muffin.utils import generate_password_hash from example.models import User mixer = Mixer(commit=True) mixer.guard(User.email == '*****@*****.**').blend( User, email='*****@*****.**', password=generate_password_hash('pass')) mixer.guard(User.email == '*****@*****.**').blend( User, email='*****@*****.**', password=generate_password_hash('pass'), is_super=True)
def import_data(settings): mixer = Mixer() with yaml_loader(settings) as loader: def blend_data(klass, attributes): with tokenize(attributes) as (tokens, attrs): fixture = mixer.blend(klass, **attrs) fixture.save() with set_password(fixture, attrs) as fixture: if fixture.is_dirty: fixture.save() for k, f in tokens.items(): setattr(fixture, k, f(fixture)) fixture.save() # tests/data/*.yml fixtures = [ Plan, Classification, License, Project, Publication, Chapter, Article, Application, Page, Site, User, UserEmail, Membership, Contribution, ] for model in fixtures: # pylint: disable=no-member,protected-access table = model._meta.table_name fixtures_dir = os.path.join(os.path.dirname(__file__), 'data') files = [ f for f in os.listdir(fixtures_dir) if f.startswith(table) and f.endswith('.yml') ] # e.g. classification.yml, classification.1.yml... for f in sorted(files, key=(lambda v: int( re.sub(r'[a-z\_]', '', v).replace('.', '0')))): fixture_yml = os.path.join(fixtures_dir, f) if os.path.isfile(fixture_yml): data = loader(fixture_yml) for attributes in data[table]: blend_data(model, attributes)
async def devdata(): """Generate some fake data.""" mixer = Mixer(commit=True) async with DB.connection(): await User.get_or_create( email='*****@*****.**', defaults=dict(password='******', role='admin', first_name='Admin', last_name='General', picture="https://picsum.photos/id/10/100")) await User.get_or_create( email='*****@*****.**', defaults=dict(password='******', role='manager', first_name='Manager', last_name='Throw', picture="https://picsum.photos/id/20/100")) # Generate 100 users num_users = await User.select().count() for n in range(100 - num_users): await User.create( email=mixer.faker.email(), role='user', picture=f"https://picsum.photos/id/2{n}/100", first_name=mixer.faker.first_name(), last_name=mixer.faker.last_name(), ) # Generate 100 messages statuses = [choice[0] for choice in Message.status.choices] for n in range(100): await Message.create( body=mixer.faker.text(), title=mixer.faker.title(), user=mixer.faker.random.randint(1, 100), status=mixer.faker.random.choice(statuses), )
def mixer(app): if 'peewee' in app.plugins: from mixer.backend.peewee import Mixer return Mixer(commit=True)
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
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
def devdata(): mixer = Mixer(commit=True) statuses = [choice[0] for choice in Message.status.choices] mixer.cycle(20).blend(Message, status=mixer.RANDOM(*statuses))
"""Setup data generator.""" from mixer.backend.peewee import Mixer mixer = Mixer(commit=True) gen_tc_id = lambda: mixer.faker.md5()[:24] @mixer.middleware("salesmeter.models.Operator") def set_password(operator): """Encrypt user's password.""" operator.set_password(operator.password) return operator mixer.register("salesmeter.models.Ticket", tc_id=gen_tc_id, barcode=lambda: mixer.faker.random_number(12)) mixer.register("salesmeter.models.Partner", tc_id=gen_tc_id) mixer.register("salesmeter.models.Event", tc_id=gen_tc_id) mixer.register("salesmeter.models.TicketSet", tc_id=gen_tc_id) mixer.register("salesmeter.models.Order", tc_id=gen_tc_id, buyer=mixer.faker.name)
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
def mixer(app): from mixer.backend.peewee import Mixer return Mixer(commit=True)