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.
For clarity and ease of maintenance it is recommended to always add your reports in a custom extension app.
As an example we’ll create a report to display some statistics on the size of your model. It will simply display the total number of buffers and operations in your model.
from freppledb.input.models import *
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.admin.views.decorators import staff_member_required
@staff_member_required
def MyStatisticsReport(request):
countOperations = Operation.objects.using(request.database).count()
countBuffers = Buffer.objects.using(request.database).count()
return render_to_response('statistics.html',
RequestContext(request, {
'numOperations': countOperations,
'numBuffers': countBuffers,
'title': 'Model statistics',
}))
{% extends "admin/base_site_nav.html" %}
{% load i18n %}
{% block content %}
<div id="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.
Edit the definition of the urlpatterns variable in the file urls.py to set up a URL for this example:
urlpatterns = patterns('',
...
(r'^statistics.html$', 'statistics.MyStatisticsReport'),
)
The following lines in this file menu.py will do this:
from django.utils.translation import ugettext as _
from freppledb.menu import menu
menu.addItem("admin", "statistics", url="/statistics.html",
label=_('Model statistics'), index=900)
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.
from freppledb.common.report import *
class myReportClass(ListReport):
template = 'myreporttemplate.html'
title = 'title of my report'
basequeryset = ... # A query returning the data to display
frozenColumns = 1
rows = (
('field1', {
'filter': FilterNumber(operator='exact', ),
'title': _('field1'),
}),
('field2', {
'filter': FilterText(size=15),
'title': _('field2')}),
('field3', {
'title': _('field3'),
'filter': FilterDate(),
}),
)
from freppledb.common.report import *
class myReportClass(TableReport):
template = 'myreporttemplate.html'
title = 'title of my report'
basequeryset = ... # A query returning the data to display
model = Operation
rows = (
('field1',{
'filter': FilterNumber(operator='exact', ),
'title': _('field1'),
}),
)
crosses = (
('field2', {'title': 'field2',}),
('field3', {'title': 'field3',}),
)
columns = (
('bucket',{'title': _('bucket')}),
)
@staticmethod
def resultlist1(request, basequery, bucket, startdate, enddate, sortsql='1 asc'):
... # A query returning the data to display as fixed columns on the left hand side.
@staticmethod
def resultlist2(request, basequery, bucket, startdate, enddate, sortsql='1 asc'):
... # A query returning the data to display for all cells in the grid.
{% extends "admin/base_site_list.html" %}
{% load i18n %}
{% block frozendata %}
{% for i in objectlist1 %}
<tr>
<td>{{i.field1}}</td>
</tr>{% endfor %}
{% endblock %}
{% block data %}
{% for i in objectlist1 %}
<tr>
<td>{{i.field2}}</td>
<td>{{i.field3}}</td>
</tr>{% endfor %}
{% endblock %}
For a grid report the template is identical, except that you need to inherit from the admin/base_site_table.html template.
urlpatterns = patterns('',
...
(r'^myreport/([^/]+)/$', 'freppledb.common.report.view_report',
{'report': myReportClass,}),
...
)