"""a collection of tools to build reports in controllers. @author: R. Le Gac """ import re from list_postprocessing import (clean, highlight_my_authors, highlight_my_speaker, remove_undef) from gluon.dal import smart_query from gluon.storage import Storage def format_publication(template, record, funcs=[]): """Build the bibliographic string for the publication. @type template str: @param template str: @type record: gluon.DAL.Row @param record @type funcs: list @param funcs: list of functions to polish the bibliographic reference @rtype: str @return: the bibliographic reference """ # format the record according to user requirement value = template.format(**record) # run post processing to polish/clean the value if funcs: for func in funcs: value = func(value, template, record) return value def get_converter(row): """Get the converter dictionary for the axis label of the C{list}. @type row: gluon.dal.Row @param row: One row of the database table C{list}. @rtype: dict @return: - C{value: "blabla, ....", value2: "foo", ....} """ di = {} if row.axis_label_converters: s = re.sub(r'" *, *', '"|', row.axis_label_converters) for el in s.split('|'): m = re.match(r' *(.+) *: *"(.+)"', el) if m: di[m.group(1)] = m.group(2) return di def get_sections(db, selector, row): """Get the section rows for the C{list}. @type db: gluon.dal.DAL @param db: @type selector: plugin_dbui.Selector @param selector: @type row: gluon.dal.Row @param row: One row of the database table C{list}. @rtype: list @return: - a list of rows of the C{sections} table - each row also contains the C{query} and C{orderby} directive taking into account C{selector} directive. - each row contains also the renderer format and post processing functions """ publications = db.publications sections = [] for el in row.sections.split(','): # retrieve the section record el = el.strip() rows = db(db.sections.section == el).select() if not rows: continue section = rows.first() sections.append(section) # query directive to extract publications for this section # It includes foreign key constraints and user requirements # related to team, project, authors and year query = selector.query(publications) if selector.year_start and not selector.year_end: query = (query) & (publications.year == selector.year_start) elif selector.year_start and selector.year_end: q_start = publications.year >= selector.year_start q_end = publications.year <= selector.year_end query = (query) & ((q_start) & (q_end)) if selector.author: q_author = publications.authors_institute.contains(selector.author) query = (query) & (q_author) # add to the query the directive coming from the section itself # the publication category and dedicated conditions if section.category_codes: q_cat = None codes = section.category_codes.replace(' ', '').split(',') if codes: q_cat = db.categories.code.belongs(codes) query = (query) & (q_cat) if section.conditions: q_conditions = smart_query(publications, section.conditions) query = (query) & (q_conditions) section.query = query # order by directive to order publication for this section # taking into account directive from the row and from the section orderby = [] for level in ('level_1', 'level_2', 'level_3', 'level_4'): axis = 'axis_%s' % level if not row[axis]: continue tablename = row[axis] fieldname = row['granularity_%s' % level] direction = row["direction_%s" % level] field = db[tablename][fieldname] if direction == "DESC": field = ~field orderby.append(field) section.orderby = tuple(orderby) # the renderer format and post processing functions renderer = db.renderers[section.id_renderers] section.renderer = Storage() section.renderer.template = renderer.template section.renderer.funcs = [] funcs = renderer.postprocessing.replace(' ', '').split(',') for func in funcs: if func in globals(): section.renderer.funcs.append(globals()[func]) return sections def ref_url(name='List'): """Get the URL string uses to build the current request. @note: this function is useful in view @type name: str @param name: @rtype: str @return: C{"http://..."} """ li = [] for (k, v) in request.vars.iteritems(): if k.startswith(name) and len(v): li.append("%s=%s" % (k, v)) pass pass t = (request.env.http_host, request.env.path_info, '&'.join(li)) url = "http://%s%s?%s" % t return url def repr_team_project(db, selector): """Build the team/project string. @type db: gluon.dal.DAL @param db: @type selector: plugin_dbui.Selector @param selector: @rtype: unicode @return: the format is "team" or "project" or "team/project". """ s = '' if selector.id_teams and not selector.id_projects: s = db.teams[selector.id_teams].team elif not selector.id_teams and selector.id_projects: s = db.projects[selector.id_projects].project elif selector.id_teams and selector.id_projects: team = db.teams[selector.id_teams].team project = db.projects[selector.id_projects].project s = "%s/%s" % (team, project) return s