//  [python]
def get_counts(request, project=None, component=None):
    """View for work counts."""
    if project is None:
        obj = None
        kwargs = {}
    elif component is None:
        obj = get_project(request, project)
        kwargs = {"project": obj}
    else:
        obj = get_component(request, project, component)
        kwargs = {"component": obj}

    form = ReportsForm(request.POST)

    if not form.is_valid():
        show_form_errors(request, form)
        return redirect_param(obj or "home", "#reports")

    data = generate_counts(
        None if request.user.has_perm("reports.view", obj) else request.user,
        form.cleaned_data["start_date"],
        form.cleaned_data["end_date"],
        **kwargs,
    )

    if form.cleaned_data["style"] == "json":
        return JsonResponse(data=data, safe=False)

    headers = (
        "Name",
        "Email",
        "Count total",
        "Edits total",
        "Source words total",
        "Source chars total",
        "Target words total",
        "Target chars total",
        "Count new",
        "Edits new",
        "Source words new",
        "Source chars new",
        "Target words new",
        "Target chars new",
        "Count approved",
        "Edits approved",
        "Source words approved",
        "Source chars approved",
        "Target words approved",
        "Target chars approved",
        "Count edited",
        "Edits edited",
        "Source words edited",
        "Source chars edited",
        "Target words edited",
        "Target chars edited",
    )

    if form.cleaned_data["style"] == "html":
        start = HTML_HEADING.format("".join(f"<th>{h}</th>" for h in headers))
        row_start = "<tr>"
        cell_name = cell_count = "<td>{0}</td>\n"
        row_end = "</tr>"
        mime = "text/html"
        end = "</table>"
    else:
        start = "{0}\n{1} {2}\n{0}".format(
            RST_HEADING,
            " ".join(f"{h:40}" for h in headers[:2]),
            " ".join(f"{h:24}" for h in headers[2:]),
        )
        row_start = ""
        cell_name = "{0:40} "
        cell_count = "{0:24} "
        row_end = ""
        mime = "text/plain"
        end = RST_HEADING

    result = [start]

    for item in data:
        if row_start:
            result.append(row_start)
        result.append(
            "".join(
                (
                    cell_name.format(escape(item["name"]) or "Anonymous"),
                    cell_name.format(escape(item["email"]) or ""),
                    cell_count.format(item["count"]),
                    cell_count.format(item["edits"]),
                    cell_count.format(item["words"]),
                    cell_count.format(item["chars"]),
                    cell_count.format(item["t_words"]),
                    cell_count.format(item["t_chars"]),
                    cell_count.format(item["count_new"]),
                    cell_count.format(item["edits_new"]),
                    cell_count.format(item["words_new"]),
                    cell_count.format(item["chars_new"]),
                    cell_count.format(item["t_words_new"]),
                    cell_count.format(item["t_chars_new"]),
                    cell_count.format(item["count_approve"]),
                    cell_count.format(item["edits_approve"]),
                    cell_count.format(item["words_approve"]),
                    cell_count.format(item["chars_approve"]),
                    cell_count.format(item["t_words_approve"]),
                    cell_count.format(item["t_chars_approve"]),
                    cell_count.format(item["count_edit"]),
                    cell_count.format(item["edits_edit"]),
                    cell_count.format(item["words_edit"]),
                    cell_count.format(item["chars_edit"]),
                    cell_count.format(item["t_words_edit"]),
                    cell_count.format(item["t_chars_edit"]),
                )
            )
        )
        if row_end:
            result.append(row_end)

    result.append(end)

    return HttpResponse("\n".join(result), content_type=f"{mime}; charset=utf-8")