Commit 9b84dd68 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Add a new class MySelector.

parent f7d9e9b2
""" List controllers """
from plugin_dbui import Selector
from reporting_tools import (Hardware,
""" List controllers
"""
from reporting_tools import (MySelector,
Hardware,
Person,
get_header,
set_period)
get_header)
TIME_FIELDS = ('year', 'period_start', 'period_end')
def hardware():
""" Active hardware for a given period of time, given team, ...
"""
time_fields = ('year', 'period_start', 'period_end')
selector = Selector(db)
set_period(selector)
selector = MySelector(db)
# build the query
query = selector.query('hardware', exclude_fields=time_fields)
query = selector.query('hardware', exclude_fields=TIME_FIELDS)
if selector.period_start and selector.period_end:
q = db.hardware.start_date <= selector.period_end
......@@ -44,11 +45,8 @@ def history():
"""History for a person.
"""
selector = Selector(db)
selector = MySelector(db)
# if not selector.id_people:
# return 'Select a person. !!!'
# build the query
query = selector.query('history')
......@@ -78,13 +76,10 @@ def people():
"""Active people for a given period of time, given team, ...
"""
time_fields = ('year', 'period_start', 'period_end')
selector = Selector(db)
set_period(selector)
selector = MySelector(db)
# build the query
query = selector.query('history', exclude_fields=time_fields)
query = selector.query('history', exclude_fields=TIME_FIELDS)
if selector.period_start and selector.period_end:
q = db.history.start_date <= selector.period_end
......
......@@ -2,8 +2,34 @@
"""
from datetime import date, datetime, timedelta
from plugin_dbui import Selector
class MySelector(Selector):
"""Custom version of the selector.
It determines the period range which can be defined in several
different ways.
"""
def __init__(self, db, extfield='format'):
Selector.__init__(self, db, extfield)
if 'period_start' not in self and 'period_end' not in self:
return
if 'year' in self and self.year:
self.period_start = "%s-01-01" % self.year
self.period_end = "%s-12-31" % self.year
elif self.period_start and self.period_end:
for field in ('period_start', 'period_end'):
s = self[field]
self[field] = s[:s.find('T')]
class Base(object):
"""Base class for reporting tool.
The base class translate selector period fields in python date.
......@@ -134,21 +160,6 @@ def get_header(selector, prefix="", suffix=""):
return "%s%s%s" % (prefix, header, suffix)
def set_period(selector):
"""Helper function to setup the time period fields
from the different options available in the selector.
"""
if selector.year:
selector.period_start = "%s-01-01" % selector.year
selector.period_end = "%s-12-31" % selector.year
elif selector.period_start and selector.period_end:
for field in ('period_start', 'period_end'):
s = selector[field]
selector[field] = s[:s.find('T')]
def repr_timedelta(dt):
"""Helper function to convert a datetime.TimeDelta
into a string 'x years y months'.
......
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