Skip to content

Commit

Permalink
Merge pull request #76 from basxsoftwareassociation/feature/field-inf…
Browse files Browse the repository at this point in the history
…ormation-for-reports

Feature/field information for reports
  • Loading branch information
saemideluxe authored Sep 7, 2021
2 parents 4b0e0fe + 6f9410a commit bc86526
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 138 deletions.
2 changes: 1 addition & 1 deletion bread/contrib/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def preview(self):
return hg.BaseElement(
hg.H3(_("Preview")),
layout.datatable.DataTable.from_queryset(
qs[:25], columns=columns, with_toolbar=False
qs[:25], columns=columns, primary_button=""
),
)

Expand Down
139 changes: 2 additions & 137 deletions bread/contrib/reports/urls.py
Original file line number Diff line number Diff line change
@@ -1,151 +1,16 @@
import datetime

import htmlgenerator as hg
from django.contrib.staticfiles.storage import staticfiles_storage
from django.db import models
from django.http import HttpResponseNotFound
from django.shortcuts import get_object_or_404
from django.urls import path
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView

from bread import formatters
from bread import layout as _layout
from bread import menu, views
from bread.utils import filter_fieldlist, generate_excel, urls, xlsxresponse
from bread.utils import urls
from bread.utils.links import Link, ModelHref
from bread.views.browse import delete
from bread.views.edit import bulkcopy

from ...layout.components.datatable import DataTableColumn, sortingname_for_column
from .models import Report


class EditView(views.EditView):
def get_layout(self):
F = _layout.form.FormField
ret = hg.BaseElement(
hg.LINK(
rel="stylesheet",
type="text/css",
href=staticfiles_storage.url("djangoql/css/completion.css"),
),
hg.SCRIPT(src=staticfiles_storage.url("djangoql/js/completion.js")),
hg.H3(self.object),
_layout.form.Form.wrap_with_form(
hg.C("form"),
hg.BaseElement(
hg.DIV(
_("Base model"),
": ",
hg.C("object.model"),
style="margin: 2rem 0 2rem 0",
),
F("name"),
F("filter"),
F("custom_queryset"),
_layout.form.FormsetField.as_datatable(
"columns",
["column", "name"],
formsetfield_kwargs={
"extra": 1,
"can_order": True,
},
),
),
),
hg.C("object.preview"),
)
return ret

def get_success_url(self):
return self.request.get_full_path()


class ReadView(views.ReadView):
def get_layout(self):

# ordering, copied from bread.views.browse.BrowseView.get_queryset
qs = self.object.queryset
if not qs:
return _layout.notification.InlineNotification(
"Error",
f"Model '{self.object.model}' does no longer exist.",
kind="error",
)

order = self.request.GET.get("ordering")
if order:
if order.endswith("__int"):
order = order[: -len("__int")]
qs = qs.order_by(
models.functions.Cast(order[1:], models.IntegerField()).desc()
if order.startswith("-")
else models.functions.Cast(order, models.IntegerField())
)
else:
qs = qs.order_by(
models.functions.Lower(order[1:]).desc()
if order.startswith("-")
else models.functions.Lower(order)
)

columns = []
for col in self.object.columns.all():
sortingname = None
try:
sortingname_for_column(self.object.model.model_class(), col.column),
except AttributeError:
pass
columns.append(
DataTableColumn(col.name, _layout.FC(f"row.{col.column}"), sortingname)
)
if not columns:
columns = [
DataTableColumn.from_modelfield(col, self.object.model.model_class())
for col in filter_fieldlist(
self.object.model.model_class(), ["__all__"]
)
]

# generate a nice table
return _layout.datatable.DataTable(
columns=columns, row_iterator=qs
).with_toolbar(
title=self.object.name,
helper_text=f"{self.object.queryset.count()} {self.object.model.model_class()._meta.verbose_name_plural}",
primary_button=_layout.button.Button(
label=_("Excel"), icon="download"
).as_href(ModelHref(self.object, "excel")),
)


def exceldownload(request, pk: int):
report = get_object_or_404(Report, pk=pk)
if report.model.model_class() is None:
return HttpResponseNotFound()

columns = {
column.name: lambda row, c=column.column: formatters.format_value(
hg.resolve_lookup(row, c)
)
for column in report.columns.all()
}
if not columns:
columns = {
column: lambda row, c=column: formatters.format_value(
hg.resolve_lookup(row, c)
)
for column in filter_fieldlist(report.model.model_class(), ["__all__"])
}

workbook = generate_excel(report.queryset, columns)
workbook.title = report.name

return xlsxresponse(
workbook, workbook.title + f"-{datetime.date.today().isoformat()}"
)

from .views import EditView, ReadView, exceldownload

urlpatterns = [
*urls.default_model_paths(
Expand Down
Loading

0 comments on commit bc86526

Please sign in to comment.