reporting_tools.py 5.92 KB
Newer Older
1
"""a collection of tools to build reports in controllers.
2

3
@author: R. Le Gac
4 5

"""
6 7 8 9 10 11 12
import re


from list_postprocessing import (clean,
                                 highlight_my_authors,
                                 highlight_my_speaker,
                                 remove_undef)
13
from gluon.dal import smart_query
14
from gluon.storage import Storage
15

16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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


44
def get_converter(row):
45
    """Get the converter dictionary for the axis label of the C{list}.
46

47 48
    @type row: gluon.dal.Row
    @param row: One row of the database table C{list}.
49

50
    @rtype: dict
51
    @return:
52
        - C{value: "blabla, ....", value2: "foo", ....}
53

54
    """
55
    di = {}
56
    if row.axis_label_converters:
57

58
        s = re.sub(r'" *, *', '"|', row.axis_label_converters)
59

60 61 62 63
        for el in s.split('|'):
            m = re.match(r' *(.+) *: *"(.+)"', el)
            if m:
                di[m.group(1)] = m.group(2)
64

65 66
    return di

67

68
def get_sections(db, selector, row):
69 70 71 72
    """Get the section rows for the C{list}.

    @type db: gluon.dal.DAL
    @param db:
73

74 75
    @type selector: plugin_dbui.Selector
    @param selector:
76

77 78
    @type row: gluon.dal.Row
    @param row: One row of the database table C{list}.
79

80
    @rtype: list
81
    @return:
82 83
        - a list of rows of the C{sections} table
        - each row also contains the C{query} and C{orderby}
84 85 86 87
          directive taking into account C{selector} directive.
        - each row contains also the renderer format and post
          processing functions

88
    """
89
    sections = []
90
    for el in row.sections.split(','):
91

92
        # retrieve the section record
93 94
        el = el.strip()
        rows = db(db.sections.section == el).select()
95

96 97
        if not rows:
            continue
98 99

        section = rows.first()
100
        sections.append(section)
101

102 103 104
        # query directive to extract publications for this section
        # It includes foreign key constraints and user requirements
        # related to team, project, authors and year
105
        query = selector.query(db.publications)
106

107
        if selector.year_start and not selector.year_end:
108
            query = (query) & (db.publications.year == selector.year_start)
109

110
        elif selector.year_start and selector.year_end:
111
            q_start = db.publications.year >= selector.year_start
112
            q_end = db.publications.year <= selector.year_end
113
            query = (query) & ((q_start) & (q_end))
114

115
        if selector.author:
LE GAC Renaud's avatar
LE GAC Renaud committed
116
            q_author = db.publications.authors_institute.contains(selector.author)
117
            query = (query) & (q_author)
118

119 120
        # add to the query the directive coming from the section itself
        # the publication category and dedicated conditions
121 122
        if section.category_codes:
            q_cat = None
123
            codes = section.category_codes.replace(' ', '').split(',')
124 125
            if codes:
                q_cat = db.categories.code.belongs(codes)
126

127
            query = (query) & (q_cat)
128 129

        if section.conditions:
130
            q_conditions = smart_query(db.publications, section.conditions)
131
            query = (query) & (q_conditions)
132

133
        section.query = query
134

135
        # order by directive to order publication for this section
136
        # taking into account directive from the row and from the section
137
        orderby = []
138

139 140
        for level in ('level_1', 'level_2', 'level_3', 'level_4'):
            axis = 'axis_%s' % level
141

142
            if not row[axis]:
143
                continue
144

145 146 147
            tablename = row[axis]
            fieldname = row['granularity_%s' % level]
            direction = row["direction_%s" % level]
148

149 150 151
            field = db[tablename][fieldname]
            if  direction == "DESC":
                field = ~field
152 153 154

            orderby.append(field)

155
        section.orderby = tuple(orderby)
156 157 158 159 160 161 162 163 164 165 166 167 168

        # 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])

169 170 171
    return sections


172
def ref_url(name='List'):
173
    """Get the URL string uses to build the current request.
174

175
    @note: this function is useful in view
176

177 178
    @type name: str
    @param name:
179

180 181
    @rtype: str
    @return: C{"http://..."}
182

183
    """
184
    li = []
185 186
    for (k, v) in request.vars.iteritems():
        if k.startswith(name) and len(v):
187
            li.append("%s=%s" % (k, v))
188 189 190
        pass
    pass

191
    t = (request.env.http_host, request.env.path_info, '&'.join(li))
192
    url = "http://%s%s?%s" % t
193

194
    return url
195 196


197
def repr_team_project(db, selector):
198
    """Build the team/project string.
199

200 201
    @type db: gluon.dal.DAL
    @param db:
202

203 204
    @type selector: plugin_dbui.Selector
    @param selector:
205

206 207
    @rtype: unicode
    @return: the format is "team" or "project" or "team/project".
208 209
    """
    s = ''
210

211 212
    if selector.id_teams and not selector.id_projects:
        s = db.teams[selector.id_teams].team
213

214 215
    elif not selector.id_teams and selector.id_projects:
        s = db.projects[selector.id_projects].project
216

217 218 219 220
    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)
221

LE GAC Renaud's avatar
LE GAC Renaud committed
222
    return s