Adding or customizing a report

This section describes the different steps to add a new report (or update an
existing one) in the user interface. We’ll describe both the general case
as well as the generic view provided by frePPLe.

The steps outline here are a short and very brief summary of how screens
are developed in the Django web application framework. Check out
https://www.djangoproject.com for more information and an excellent tutorial.

The function decorator staff_member_required is used to assure users
are authenticated properly.

Notice how the first 2 statements in the function use the Django
relational mapping to pick the data from the database. This code
is translated by the framework in SQL queries on the database.

The last line in the function passes the data in a dictionary to the
Django template engine. The template engine will generate the HTML
code returned to the user’s browser.

Create a template to visualize the data.

The template file statistics.html will define all aspects of the
visualizing the results.

The file templates/statistics.html for our example looks like this:

{%extends"admin/base_site_nav.html"%}{%loadi18n%}{%blockcontent%}<divid="content-main">{%trans'Number of operations:'%}{{numOperations}}<br>{%trans'Number of buffers:'%}{{numBuffers}}<br></div>{%endblock%}

Templates are inheriting from each other. In this example we inherit
from the base template which already contains the navigation toolbar
and the breadcrumbs trail. We only override the block which contains
the div-element with the main content.

Templates use special tags to pick up data elements or to call special
functions. The {{ }} tag is used to refer to the data elements provided
by the view function. The {% trans %} tag is used to mark text that
should be translated into the different languages for the user interface.

Map the view as a URL.

To expose the view as a URL to the users you’ll need to map it to a
URL pattern.

Edit the definition of the urlpatterns variable in the file urls.py to
set up a URL for this example:

Using the frePPLe generic report

FrePPLe uses a standard view for displaying data in a list or grid layout,
respectively called ListReport and TableReport. With these views you can add
new reports with with less code and more functionality (such as sorting,
filtering, pagination, export and import).

The steps for adding the view are slightly different from the generic case.

Define a report class

Instead of defining a view function we define a class with the report
metadata. See the definition of the report base classes in the file
common/report.py to see all available options for the metadata classes.

For a list report this class has the following structure:

fromfreppledb.common.reportimport*classmyReportClass(ListReport):template='myreporttemplate.html'title='title of my report'basequeryset=...# A query returning the data to displayfrozenColumns=1rows=(('field1',{'filter':FilterNumber(operator='exact',),'title':_('field1'),}),('field2',{'filter':FilterText(size=15),'title':_('field2')}),('field3',{'title':_('field3'),'filter':FilterDate(),}),)

For a table report this class has the following structure:

fromfreppledb.common.reportimport*classmyReportClass(TableReport):template='myreporttemplate.html'title='title of my report'basequeryset=...# A query returning the data to displaymodel=Operationrows=(('field1',{'filter':FilterNumber(operator='exact',),'title':_('field1'),}),)crosses=(('field2',{'title':'field2',}),('field3',{'title':'field3',}),)columns=(('bucket',{'title':_('bucket')}),)@staticmethoddefresultlist1(request,basequery,bucket,startdate,enddate,sortsql='1 asc'):...# A query returning the data to display as fixed columns on the left hand side.@staticmethoddefresultlist2(request,basequery,bucket,startdate,enddate,sortsql='1 asc'):...# A query returning the data to display for all cells in the grid.