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

Add the logic to instantiate dataframes.

parent e4469978
# -*- coding: utf-8 -*-
"""new_report controllers
"""
import json
from gluon.tools import PluginManager
from plugin_dbui import Selector
from plugin_event import get_event_minimal
def lists2():
"""New approach to generate lists.
"""
# instantiate the selector
selector = Selector(virtdb.selector)
# configuration
cfg = db.lists2[request.vars.id_lists2]
# instantiate the dataframe
func = PluginManager("event").event.sources[cfg.source].func
kwargs = selector.as_dict()
kwargs["id_events"] = cfg.id_events
print func
print kwargs
df = func(**kwargs)
print df
# as an HTML table
# as an Ext JS grid
return "lists2"
......@@ -99,13 +99,6 @@ def grid():
return dict(cfg_store=store, grid=grid, title=title)
def lists2():
"""New approach to generate lists.
"""
print "lists2 -- hello"
def userModelConsistency():
""" A user data block is associated to each event.
It is configurable by the user and defined in the events table.
......
......@@ -59,14 +59,12 @@ if is_dbui:
#
is_db = (ctrl == "plugin_dbui" and fnct in ("call", "csv", "dbui_conf")) or \
(ctrl == "plugin_event")
(ctrl == "plugin_event") or (ctrl == "new_report")
if is_db:
# register source for the reporting
event.Event.register_source("minimal event",
event.get_event_minimal,
dict(event=None))
event.Event.register_source("minimal event", event.get_event_minimal)
event.Core.define_tables(db, T)
event.Report.define_tables(db, T)
......
......@@ -17,7 +17,10 @@ from model_core import Core
from model_report import Report
from model_selector import Selector
from report_dataframes import (get_event_minimal)
from report_dataframes import (db2df,
get_event_minimal,
normalize_history_data,
query_history)
from report_objects import (do_title,
get_value,
......
......@@ -36,13 +36,12 @@ class Event(object):
@staticmethod
def register_source(name, func, kwargs):
def register_source(name, func):
"""Register sources which are used in the reporting section.
Args:
name (str): name of the source to be used in the UI.
func (reference): the function generating the DataFrame.
kwargs (dict): input keyword arguments for the function.
"""
event = PluginManager("event").event
......@@ -50,4 +49,4 @@ class Event(object):
if event.sources is None:
event.sources = Storage()
event.sources[name] = Storage(func=func, kwargs=kwargs)
event.sources[name] = Storage(func=func)
......@@ -2,11 +2,13 @@
"""report_dataframe module
"""
import json
import pandas as pd
from datetime import date
from gluon import current
from plugin_dbui import get_id
YEAR_MIN = date(1900, 01, 01)
......@@ -71,7 +73,7 @@ def db2df(db, query, fields=[], columns=None):
return pd.DataFrame.from_records(list(rows), columns=columns)
def get_event_minimal(event, kwargs):
def get_event_minimal(**kwargs):
"""DataFrame with a minimal set of information for an event.
The selection of the event is performed via the keyword arguments.
......@@ -90,11 +92,8 @@ def get_event_minimal(event, kwargs):
* id_teams (int)
* start_date (date)
Args:
event (str): the event used to generated the DataFrame
Keyword Args:
id_events (int):
id_events (int): identifier of the event
id_domains (int):
id_fundings (int):
id_objects (int):
......@@ -114,8 +113,7 @@ def get_event_minimal(event, kwargs):
history = db.history
# database query
id_event == get_id(db.events, event=event)
query = query_history(id_events=id_event, **kwargs)
query = query_history(db, **kwargs)
# fields to be extracted from the database
fields = [
......@@ -123,7 +121,7 @@ def get_event_minimal(event, kwargs):
history.id_domains,
history.id_events,
history.id_fundings,
history.id_object,
history.id_objects,
history.id_object_categories,
history.id_people,
history.id_people_categories,
......@@ -183,11 +181,9 @@ def normalize_history_data(df):
return df
def query_history(db, kwargs):
"""Build the query for the history table.
def query_history(db, **kwargs):
"""Build the query for the history table from the keyword arguments.
The operator AND is applied between the conditions.
Conditions are defined by keyword arguments.
Args:
db (gluon.dal.DAL): database connection.
......@@ -214,13 +210,15 @@ def query_history(db, kwargs):
for k, v in kwargs.iteritems():
if v is None:
if (v in ('', None)) or (k == "data"):
continue
if k == "year_end":
year_end = date(int(kwargs["year_end"]), 12, 31)
qi = history.start_date <= year_end
elif k == "year_start":
year_start = date(int(kwargs["year_start"]), 01, 01)
qi = (history.end_date == None) | (history.end_date >= year_start)
else:
......
......@@ -267,8 +267,8 @@ class ViewportUi(object):
for row in db(db.lists2.id > 0).select(orderby=db.lists2.name):
panel = selector_panel
panel.baseUrl = URL("plugin_event", "lists2")
panel.baseParams = {"id_lists": row.id}
panel.baseUrl = URL("new_report", "lists2")
panel.baseParams = {"id_lists2": row.id}
add_child(row.name, panel)
......
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