Application Stats

The STATS application contains the actions to generate generate an automatic report.

Templates

The STATS application contains 1 template :

  • stats.html : stat page where the automatic report is returned

Key files of the « HOME » application

management.commands

load_region.py

class apps.stats.management.commands.load_region.Command(stdout=None, stderr=None, no_color=False, force_color=False)
handle(*args, **kwargs)

SUMMARY LINE

Import region data

HOW TO USE

docker-compose exec web python manage.py load_region

Add -f docker-compose.prod.yml in Prod

load_departement.py

class apps.stats.management.commands.load_departement.Command(stdout=None, stderr=None, no_color=False, force_color=False)
handle(*args, **kwargs)

SUMMARY LINE

Import departement data

HOW TO USE

docker-compose exec web python manage.py load_departement

Add -f docker-compose.prod.yml in Prod

models.py

class apps.stats.models.departement(*args, **kwargs)

Departement model table

exception DoesNotExist
exception MultipleObjectsReturned
class apps.stats.models.flash(*args, **kwargs)

Flash model table

exception DoesNotExist
exception MultipleObjectsReturned
class apps.stats.models.region(*args, **kwargs)

Region model table

exception DoesNotExist
exception MultipleObjectsReturned
class apps.stats.models.region_dep_mat(departement_id, departement_code, departement_nom, departement_geometry, region_id, region_code, region_nom, region_geometry)
exception DoesNotExist
exception MultipleObjectsReturned

test.py

class apps.stats.tests.statsTestPage(methodName='runTest')

Stats tests

setUp()

Hook method for setting up the test fixture before exercising it.

tearDown()

Hook method for deconstructing the test fixture after testing it.

test_Flash_page_nothing()

Test get flash page with no data

test_flash_get_noLog()

Test get flash page but not log

test_flash_get_no_staff()

Test get flash page but not log

test_flash_page()

Test get flash page with default parameters

test_flash_page_post_dep()

Test post and get flash page with deparement parameter

test_flash_page_post_reg()

Test post and get flash page with region parameter

test_public_page_nothing()

Test get public page with no data

test_stats_get_noLog()

Test get stats page but not log

test_stats_page()

Test get stats page with default parameters

test_stats_page_nothing()

Test get stats page with no data

test_stats_page_post_dep()

Test post and get stats page with deparement parameter

test_stats_page_post_reg()

Test post and get stats page with region parameter

test_str_dep()

Test model : get str from Excel file

url.py

The urls.py file contains :

  • path to stats page

view.py

apps.stats.views.flash_view(request)

Request, preparate and send data to Flash template for automatic report

apps.stats.views.stats_public(request)

Render public stats

apps.stats.views.stats_view(request)

Request, preparate and send data to template for automatic report

Create

Table

We use the datable web library to display the tables.

In view.py

To use the Datatable library in an automated way, the function sends, for each table, 3 elements :

  • A list of lists. Each sub-list corresponds to a row of the table with the name of the row in the first position. By convention, this element is called tabX where X is the table number

  • A list of column names. By convention, this element is called tabX_column_loc where X is the table number

  • A list of column numbers. By convention, this element is called tabX_column_num where X is the number of the table

The code contains several tables that can help you in adding new tables.

In template/stats/stats.html

1- Add a div in the body at the desired position

<table id="tabX" class="display nowrap" style="width:100%">
    <thead>
        <tr>
            <!-- Your custom header (see in code to have example) -->
        </tr>
        <tr>
            {% for l in tabX_column_loc %}
            <th>{{ l }}</th>
            {% endfor %}
        </tr>
    </thead>
</table>

2- Add at the end of the body with the other scripts a new JS script like the following example

<script>
    $(document).ready(function() {
        $('#tab_X').DataTable( {
            "data" : {{ tabX|safe }},
            "columns" : {{ tabX_column_num|safe }},
            "pageLength": 25,
            "order": [],
            "scrollX": true,
            dom: 'Bfrtip',
            buttons: [
                'copyHtml5',
                'excelHtml5',
                'csvHtml5',
                'pdfHtml5'
            ]
        });
    } );
</script>

Charts

We use two libraries :

The codes view.py and template/stats/stats.html contain several examples that can help you in adding new charts.

Map

To create map, we use Leaflet <https://leafletjs.com/>. The codes view.py and template/stats/stats.html contain several examples that can help you to custom map and format data.