Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 54cac1c2268db633d66eeff1b4faa585 > files > 7

frepple-doc-0.8.1-3.fc15.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  <title>Frepple / Adding or customizing a report</title>
  <link rel='stylesheet' href='../styles.css' type='text/css' />
  <!--PageHeaderFmt-->  
</head>
<body>
<div id="container">

<div id="menubar">
  
<div id="logo" align="center">
<br/><img src='../frepple.bmp' alt="frepple" /><br/>
<a href='http://www.frepple.com/'>
<strong>a Free<br/>Production Planning<br/>Library</strong>
</a></div>
<div id="menu">
<br/>
<h3><a href='../Main/HomePage.html'>Main</a></h3>
<h3><a href='../UI/Main.html'>User Manual</a></h3>
<h3><a href='../Tutorial/Main.html'>Tutorial</a></h3>
<h3><a href='Main.html'>Reference Manual</a></h3>
<h3><a href='../Main/FAQ.html'>FAQ</a></h3>
<h3><a href='../reference/index.html'>C++ API</a></h3>
<br/><div>
</div>  
</div>
</div>

<div id="content">
<br/>
<!--PageText-->
<div id='wikitext'>
<p><a class='wikilink' href='../Main/HomePage.html'>Main</a> &gt; <span class='wikitrail'><a class='wikilink' href='Main.html'>Reference Manual</a> > <a class='wikilink' href='Developer.html'>Information for developers</a> > <a class='selflink' href='DeveloperAddreport.html'>Adding or customizing a report</a></span>
</p>
<p class='vspace'>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.
</p>
<p class='vspace'>The steps outline here are a short and very brief summary of how screens are designed in the Django web application framework. More detailed instructions are available on the Django website <a class='urllink' href='http://www.djangoproject.com' rel='nofollow'>http://www.djangoproject.com</a>, which has an excellent tutorial.
</p>
<p class='vspace'><strong>General case</strong>
</p>
<p class='vspace'>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.
</p>
<div class='vspace'></div><ol><li><strong>Create a view to generate the data.</strong><br />  A view function retrieves the report data from the database (or computes it from another source) and passes a data dictionary with the data to the report template.<br />  A view is a Python function. Here's the view required for our example, which you can put in a file statistics.py:<br /><pre class='escaped'>
  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',
       }))
</pre>
The function decorator staff_member_required is used to assure users are authenticated properly.<br />  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.<br />  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. 
<div class='vspace'></div></li><li><strong>Create a template to visualize the data.</strong><br />  The template file statistics.html will define all aspects of the visualizing the results.<br />  The file templates/statistics.html for our example looks like this:<br /><pre class='escaped'>
  {% extends "admin/base_site_nav.html" %}
  {% load i18n %}
  {% block content %}
  &lt;div id="content-main"&gt;
  {% trans 'Number of operations:' %} {{numOperations}}&lt;br/&gt;
  {% trans 'Number of buffers:' %} {{numBuffers}}&lt;br/&gt;
  &lt;/div&gt;
  {% endblock %}
</pre>
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.<br />  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 targetted languages for the user interface.
<div class='vspace'></div></li><li><strong>Map the view as a URL.</strong><br />  To expose the view as a URL to the users you'll need to map it to a URL pattern. This mapping is defined in the files urls.py, input/urls.py, output/urls.py, common/urls.py and execute/urls.py.<br />  Edit the definition of the urlpatterns variable in the file urls.py to set up a URL for this example:<br /><pre class='escaped'>
  urlpatterns = patterns('',
    ...    
    (r'^statistics.html$', 'statistics.MyStatisticsReport'),
    )
</pre>
<div class='vspace'></div></li><li><strong>Update the menu structure.</strong><br />  To insert the view you'll probably want to insert it in the menu bar and/or the context menus of the different objects.<br />  This requires editing the files:
<ul><li>templates/admin/base_site_nav.html: Menu bar
</li><li>templates/*context.html: Context menu for different entities
</li></ul></li></ol><p class='vspace'><strong>Using the frePPLe generic report</strong>
</p>
<p class='vspace'>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, CSV export).<br />The steps for adding the view are slightly different from the generic case.
</p>
<div class='vspace'></div><ol><li><strong>Define a report class</strong><br />  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.<br />  For a list report this class has the following structure:<br /><pre class='escaped'>
  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(),
        }),
      )
</pre><br />  For a table report this class has the following structure:<br /><pre class='escaped'>
  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.
</pre>
<div class='vspace'></div></li><li><strong>Create a template to visualize the data.</strong><br />  For a list report the template has the following structure:<br /><pre class='escaped'>
  {% extends "admin/base_site_list.html" %}
  {% load i18n %}

  {% block frozendata %}
  {% for i in objectlist1 %}
  &lt;tr class="{% cycle row1,row2 %}"&gt;
  &lt;td&gt;{{i.field1}}&lt;/td&gt;
  &lt;/tr&gt;{% endfor %}
  {% endblock %}

  {% block data %}
  {% for i in objectlist1 %}
  &lt;tr class="{% cycle row1,row2 %}"&gt;
  &lt;td&gt;{{i.field2}}&lt;/td&gt;
  &lt;td&gt;{{i.field3}}&lt;/td&gt;
  &lt;/tr&gt;{% endfor %}
  {% endblock %}
</pre>
For a grid report the template is identical, but inherits from the admin/base_site_table.html template instead.
<div class='vspace'></div></li><li><strong>Map the view as a URL.</strong><br />  The syntax for adding a report now refers to the generic view, and we pass the report class as an argument:<br /><pre class='escaped'>
  urlpatterns = patterns('',
    ... 
    (r'^myreport/([^/]+)/$', 'freppledb.common.report.view_report',
      {'report': myReportClass,}),
    ...
    )
</pre>
<div class='vspace'></div></li><li><strong>Update the menu structure.</strong><br />  This step is identical to the general case.
</li></ol><p class='vspace'><strong>Some further hints</strong>
</p>
<div class='vspace'></div><ul><li>For extensive customizations it is best to store the extensions in a seperate directory. This keeps the extensions modular and makes upgrading to new frePPLe releases easier.<br />  Django supports this very well: you can add a new application which defines the new views and models, and you can override existing templates in an override directory.
<div class='vspace'></div></li><li>Developing reports is best done in the source code install. Adding reports to the packaged installation created from the Windows installer is a bit more complex and not recommended.
</li></ul>
</div>

<!--PageFooterFmt-->
<!--HTMLFooter-->
</div></div>
</body>
</html>