def test_get_style_error(): with pytest.raises(Exception) as e: get_style('does_not_exist') assert str(e.value).startswith( 'No registered style does_not_exist. Register a style with register_style().' )
def test_apply_checkbox_style(): from iommi import Form from iommi import Field class MyForm(Form): class Meta: iommi_style = 'bootstrap' foo = Field.boolean() form = MyForm() form = form.bind(request=None) assert get_iommi_style_name(form.fields.foo) == 'bootstrap' assert ( get_style_data_for_object( get_style('bootstrap'), obj=form.fields.foo, is_root=False, )['attrs'] == {'class': {'form-group': True, 'form-check': True}} ) assert render_attrs(form.fields.foo.attrs) == ' class="form-check form-group"' assert ( render_attrs(form.fields.foo.input.attrs) == ' class="form-check-input" id="id_foo" name="foo" type="checkbox"' ) assert render_attrs(form.fields.foo.label.attrs) == ' class="form-check-label" for="id_foo"'
def render_root(*, part, context, **render): assert part._is_bound root_style_name = get_iommi_style_name(part) root_style = get_style(root_style_name) template_name = root_style.base_template content_block_name = root_style.content_block # Render early so that all the binds are forced before we look at all_assets, # since they are populated as a side-effect content = part.__html__(**render) assets = part.iommi_collected_assets() assert template_name, f"{root_style_name} doesn't have a base_template defined" assert content_block_name, f"{root_style_name} doesn't have a content_block defined" title = get_title(part) from iommi.debug import iommi_debug_panel from iommi import Page from iommi.fragment import Container context = dict( container=Container(_name='Container').bind(parent=part), content=content, title=title if title not in (None, MISSING) else '', iommi_debug_panel=iommi_debug_panel(part) if iommi_debug_on() and '_iommi_disable_debug_panel' not in part.get_request().GET else '', assets=assets, **(part.context if isinstance(part, Page) else {}), **context, ) template_string = ('{% extends "' + template_name + '" %} {% block ' + content_block_name + ' %}{{ iommi_debug_panel }}{{ content }}{% endblock %}') return get_template_from_string(template_string).render( context=context, request=part.get_request())
def render_root(*, part, context, **render): assert part._is_bound root_style_name = get_style_name_for(part) root_style = get_style(root_style_name) template_name = root_style.base_template content_block_name = root_style.content_block assert template_name, f"{root_style_name} doesn't have a base_template defined" assert content_block_name, f"{root_style_name} doesn't have a content_block defined" title = getattr(part, 'title', None) if title is None: parts = getattr(part, 'parts', None) if parts is not None: for p in parts.values(): title = getattr(p, 'title', None) if title is not None: break from iommi.debug import iommi_debug_panel from iommi import Page from iommi.fragment import Container context = dict( container=Container(_name='Container').bind(parent=part), content=part.__html__(**render), title=title if title not in (None, MISSING) else '', iommi_debug_panel=iommi_debug_panel(part) if iommi_debug_on() else '', **(part.context if isinstance(part, Page) else {}), **context, ) template_string = '{% extends "' + template_name + '" %} {% block ' + content_block_name + ' %}{{ iommi_debug_panel }}{{ content }}{% endblock %}' return get_template_from_string(template_string).render( context=context, request=part.get_request())