Commit 37cdbc5f authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Redesign the reporting tool class and finalise the hardware list.

parent d2d55904
""" List controllers """
from plugin_dbui import Selector
from reporting_tools import get_header, set_period, Time_coverage
from reporting_tools import (Hardware,
Person,
get_header,
set_period)
def hardware():
......@@ -20,12 +23,20 @@ def hardware():
q = (q) & ((db.hardware.end_date == None) | (db.hardware.end_date >= selector.period_start))
query = (query) & (q)
# define virtual field to compute time data
# to be added in the report via extra columns
db.hardware.age = Field.Virtual(Hardware(selector).age)
# select the records
rows = db(query).select(db.people.last_name,
db.hardware.model,
db.hardware.start_date,
db.hardware.end_date,
db.hardware.cost,
orderby=(db.hardware.model, db.people.last_name))
return dict( footer='',
return dict(footer='',
header=get_header(selector),
rows=rows,
selector=selector)
......@@ -56,8 +67,9 @@ def people():
q = db.categories.usual != 'stagiaire'
query = (query) & (q)
# define virtual field to be added in the report via extra columns
db.history.time_coverage = Field.Virtual(Time_coverage(selector).process)
# define virtual field to compute time data
# to be added in the report via extra columns
db.history.time_coverage = Field.Virtual(Person(selector).coverage)
# select the records
rows = db(query).select(db.categories.usual,
......
# coding: utf8
{
'%Y-%m-%d': '%Y-%m-%d',
'Age': 'Age',
'Agencies': 'Agencies',
'agencies': 'agences',
'Agency': 'Agence',
......@@ -23,6 +24,7 @@
'Definition': 'Définition',
'Demanded': 'Demandé',
'Domain': 'Domaine',
'End': 'End',
'End Date': 'Date de fin',
'enter a number between %(min)g and %(max)g': 'enter a number between %(min)g and %(max)g',
'enter an integer between %(min)g and %(max)g': 'enter an integer between %(min)g and %(max)g',
......@@ -91,6 +93,7 @@
'Select': 'Selectionnez',
'Select a teams and/or a project !!!': 'Select a teams and/or a project !!!',
'select...': 'selectionner...',
'Start': 'Start',
'Start Date': 'Date de début',
'startswith': 'startswith',
'Store': 'Store',
......
......@@ -4,6 +4,68 @@
from datetime import date, datetime
class Base(object):
"""Base class for reporting tool.
The base class translate selector period fields in python date.
"""
def __init__(self, selector):
ds = datetime.strptime(selector.period_start, '%Y-%m-%d')
self.period_start = date(ds.year, ds.month, ds.day)
de = datetime.strptime(selector.period_end, '%Y-%m-%d')
self.period_end = date(de.year, de.month, de.day)
class Hardware(Base):
"""Tool associate to a hardware item.
"""
def age(self, row):
"""Compute the age of an hardware item.
"""
start = row.hardware.start_date
end = row.hardware.end_date
now = datetime.now()
now = date(now.year, now.month, now.day)
if start and end and start <= now <= end:
return (now - start).days / 365.
elif start and end == None and start <= now:
return (now - start).days / 365.
elif start and end and end < now:
return (end - start).days / 365.
return None
class Person(Base):
"""Tool associated to a person
"""
def coverage(self, row):
"""The fraction of time for which the person was presented
during the selected period.
"""
start = max(self.period_start, row.history.start_date)
end = self.period_end
if row.history.end_date:
end = min(self.period_end, row.history.end_date)
x = (end - start).days
y = (self.period_end - self.period_start).days
z = round(float(x) / float(y), 2)
return z
def get_header(selector, prefix="", suffix=""):
"""Helper function returning the header for the report.
The return string contains the team, the project and some
......@@ -53,39 +115,3 @@ def set_period(selector):
s = selector[field]
selector[field] = s[:s.find('T')]
\ No newline at end of file
class Time_coverage(object):
"""Tool to compute the fraction of time covers by a person
during the selected time period. The fractio is equal to one
for a full coverage.
The start and end date of the period is defined via a selector.
"""
def __init__(self, selector):
ds = datetime.strptime(selector.period_start, '%Y-%m-%d')
self.period_start = date(ds.year, ds.month, ds.day)
de = datetime.strptime(selector.period_end, '%Y-%m-%d')
self.period_end = date(de.year, de.month, de.day)
def process(self, row):
"""The fraction of time for which the person was active
during the selected period.
"""
start = max(self.period_start, row.history.start_date)
end = self.period_end
if row.history.end_date:
end = min(self.period_end, row.history.end_date)
x = (end - start).days
y = (self.period_end - self.period_start).days
z = round(float(x) / float(y), 2)
return z
......@@ -2,13 +2,20 @@
<br>
<br>
{{
# column with the hardware age
age = {'label': T('Age'),
'class': '',
'content': lambda row, rc: round(row.hardware.age, 1),
'selected': False,
'width': ''}
# render the table
table = SQLTABLE(rows,
columns=['hardware.model',
'people.last_name'],
headers={'hardware.model': T('Model'),
'people.last_name': T('Name')},
extracolumns=[],
extracolumns=[age],
renderstyle=True)
response.write(CENTER(table))
......
......@@ -10,8 +10,8 @@
'selected': False,
'width': ''}
# add a columns with the virtual field time_weight
time_weight = {'label': T('Coverage'),
# column with the period coverage
time_coverage = {'label': T('Coverage'),
'class': '',
'content': lambda row, rc: row.history.time_coverage,
'selected': False,
......@@ -25,7 +25,7 @@
headers={'categories.usual': T('Category'),
'people.last_name': T('Name'),
'categories.code': T('Level')},
extracolumns=[cdd, time_weight],
extracolumns=[cdd, time_coverage],
renderstyle=True)
response.write(CENTER(table))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment