Commit c99992ad authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update EvtSelector to compute properly is_start (end) with respect to the...

Update EvtSelector to compute properly is_start (end) with respect to the actve period of a person in a team and domain.
parent 814baecd
......@@ -213,6 +213,9 @@ class BaseReport(object):
dbfields = [el for el in dbfields if not isinstance(el, FieldVirtual)]
dbfields.extend([db.history.end_date,
db.history.id_domains,
db.history.id_people,
db.history.id_teams,
db.history.percentage,
db.history.start_date,
db.people.birth_date])
......
......@@ -8,7 +8,16 @@ import json
from datetime import date, timedelta
from gluon import current
from gluon.dal import Field
from plugin_dbui import Selector
from plugin_dbui import get_id, Selector
RAWSQL_ACTIVITY = \
"""
SELECT start_date, end_date
FROM history
WHERE id_events=%i AND id_people=%i AND id_domains=%i AND id_teams=%i
ORDER BY start_date, end_date
"""
class SelectorActiveItemsException(BaseException):
......@@ -150,7 +159,7 @@ class EvtSelector(SelectorActiveItems):
SelectorActiveItems.__init__(self, table, exclude_fields=li)
# add virtual fields
db = current.globalenv["db"]
self._db = db = current.globalenv["db"]
history = db.history
virtual = Field.Virtual
......@@ -163,6 +172,42 @@ class EvtSelector(SelectorActiveItems):
history.is_over = virtual("is_over", self._is_over, ftype="boolean")
history.is_start = virtual("is_start", self._is_start, ftype="boolean")
# keep track of the identifier of the people event
# since it is used by period_activity
self._id_event_people = get_id(db.events, event="People")
def _active_period(self, id_people, id_domain, id_team):
"""Determine the period when a person is active in a given
domain and team.
Args:
id_people (int):
id_domain (int):
id_team (int):
Returns
tuple:
start (datetime or None)
stop (datetime or None)
"""
rawsql = RAWSQL_ACTIVITY % (self._id_event_people,
id_people,
id_domain,
id_team)
rows = self._db.executesql(rawsql)
nrows = len(rows)
if nrows == 1:
return (rows[0][0], rows[0][1])
elif nrows > 1:
return (rows[0][0], rows[-1][1])
else:
raise StandardError("Failed to find start / leave date !")
def _age(self, row):
"""Compute the age of the person associated to the history record, now.
......@@ -257,7 +302,8 @@ class EvtSelector(SelectorActiveItems):
return value
def _is_end(self, row):
"""Return true is the history record ends during the period range.
"""Return true is the active period (for a given person, domain and
team) ends during the period range.
Args:
row (gluon.dal.Row): row of the history table.
......@@ -266,8 +312,10 @@ class EvtSelector(SelectorActiveItems):
bool:
"""
end = row.history.end_date
start = row.history.start_date
history = row.history
start, end = self._active_period(history.id_people,
history.id_domains,
history.id_teams)
period_end = self._period_end
period_start = self._period_start
......@@ -278,7 +326,8 @@ class EvtSelector(SelectorActiveItems):
return False
def _is_over(self, row):
"""Return true is the history record is over now.
"""Return true is the active period (for a given person, domain and
team) is over now.
Args:
row (gluon.dal.Row): row of the history table.
......@@ -287,17 +336,19 @@ class EvtSelector(SelectorActiveItems):
bool:
"""
end = row.history.end_date
now = date.today()
start = row.history.start_date
history = row.history
start, end = self._active_period(history.id_people,
history.id_domains,
history.id_teams)
if end and start != end:
return end < now
return end < date.today()
return False
def _is_start(self, row):
"""Return true is the history record starts during the period range.
"""Return true is the active period (for a given person, domain and
team) starts during the period range.
Args:
row (gluon.dal.Row): row of the history table.
......@@ -306,8 +357,10 @@ class EvtSelector(SelectorActiveItems):
bool:
"""
end = row.history.end_date
start = row.history.start_date
history = row.history
start, end = self._active_period(history.id_people,
history.id_domains,
history.id_teams)
period_end = self._period_end
period_start = self._period_start
......
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