def render_collapsible(lst, item_role='collapsible-item', title=None, expanded=False, **kwargs): """ Renders a queryset or list of objects Args: lst: List or queryset of objects inside the collapsible list. item_role: Role assigned to each element in the list. Defaults to 'collapsible-item'. title (bool): Title in which the list of object is displayed. expanded (str): If true, start list in the "expanded" state. """ if title is None: if isinstance(lst, QuerySet): title = title.model._meta.verbose_name_plural else: raise TypeError('must provide an explicit title!') data = [render(x, item_role, **kwargs) for x in lst] random_id = str(uuid.uuid4()) display = 'block' if expanded else 'none' return div( class_='CollapsibleList' )[h2(onclick=f"$('#{random_id}').toggle()", children=[title, span(f'({len(data)})'), fa_icon('angle-down')]), html_list(data, style=f'display: {display}', id=random_id), ]
def render_collapsible(lst, item_role='collapsible-item', title=None, expanded=False, **kwargs): """ Renders a queryset or list of objects Args: lst: List or queryset of objects inside the collapsible list. item_role: Role assigned to each element in the list. Defaults to 'collapsible-item'. title (bool): Title in which the list of object is displayed. expanded (str): If true, start list in the "expanded" state. """ if title is None: if isinstance(lst, QuerySet): title = title.model._meta.verbose_name_plural else: raise TypeError('must provide an explicit title!') data = [html(x, item_role, **kwargs) for x in lst] return div(class_='CollapsibleList', is_component=True)[ h2([title, span(f'({len(data)})' ), fa_icon('angle-up')]), div(class_='CollapsibleList-data')[html_list(data), ]]
def menu_section(name, links, **kwargs): """ Wraps a list of elements into a menu section <ul> """ children = [html_list(map(menu_item, links))] if name: children.insert(0, h1(name)) kwargs.setdefault("title", str(name)) return section(children, **kwargs)
def role_model(model): links = [] cls = get_class(model) for role in get_roles(cls): href = reverse("role-model-list", kwargs={"model": model, "role": role}) links.append(a(role, href=href)) if not links: raise Http404 else: return {"data": div([h1(_("List of roles")), html_list(links)])}
def stereotype_list(conversation): base_href = f'{conversation.get_absolute_url()}stereotypes/' return { 'content_title': _('Stereotypes'), 'conversation': conversation, 'stereotypes': html_list( a(str(stereotype), href=f'{base_href}{stereotype.id}/') for stereotype in conversation.stereotypes.all()), }
def role_model(model): links = [] cls = get_class(model) for role in get_roles(cls): href = reverse('role-model-list', kwargs={'model': model, 'role': role}) links.append(a(role, href=href)) if not links: raise Http404 else: return {'data': div([h1(_('List of roles')), html_list(links)])}
def vote_options(comment): def vote_option(choice): kwargs = {'checked': True} if choice == voted else {} return Block([ label([ input_(value=choice.name, name=name, type='radio', **kwargs), choice.description, ]), ]) voted = getattr(comment, 'choice', None) name = f'choice-{comment.id}' choices = [Choice.AGREE, Choice.SKIP, Choice.DISAGREE] return html_list(map(vote_option, choices), class_='unlist')
def render_queryset(qs, role=None, **kwargs): """ Renders a queryset object. Dispatch by role and model. """ model = qs.model key = (model, role) if role: kwargs['role'] = role try: renderer = registry[key] except KeyError: return html_list(render(x, **kwargs) for x in qs) else: return renderer(qs, **kwargs)
def collapsible_list(lst, item_role="list-item", title=None, **kwargs): """ Renders a queryset or list of objects Args: lst: List or queryset of objects inside the collapsible list. item_role: Role assigned to each element in the list. Defaults to 'list-item'. title (bool): Title in which the list of object is displayed. expanded (str): If true, start list in the "expanded" state. """ size = len(lst) title = _title(lst) if title is None else title title = Block([title, span(f" ({size})", class_="text-accent")]) items = [html(x, item_role, **kwargs) for x in lst] data = html_list(items, class_="list-reset") return collapsible(data, title=title)
def test_render_html_list(self): assert (html_list([1, 2]).__html__() == '<ul><li>1</li><li>2</li></ul>') assert (html_list( [1, 2], ordered=True).__html__() == '<ol><li>1</li><li>2</li></ol>')