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

3
@author: R. Le Gac
4 5

"""
6
from list_postprocessing import *
7
from gluon import current
8
from gluon.dal import smart_query
tux091's avatar
tux091 committed
9
from gluon.html import IFRAME, URL
10
from gluon.storage import Storage
11
from plugin_dbui import decode_field, UNDEF
12

13

14 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
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


41
def get_converter(list):
42
    """Get the converter dictionary for the axis label of the C{list}.
43

44 45
    @type list: gluon.dal.Row
    @param list: One row of the database table C{list}.
46

47
    @rtype: dict
48
    @return:
49
        - C{value: "blabla, ....", value2: "foo", ....}
50

51
    """
52 53
    di = {}
    if list.axis_label_converters:
54

55
        s = re.sub(r'" *, *', '"|', list.axis_label_converters)
56

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

62 63
    return di

64

65
def get_sections(db, selector, list):
66 67 68 69
    """Get the section rows for the C{list}.

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

71 72
    @type selector: plugin_dbui.Selector
    @param selector:
73

74 75
    @type list: gluon.dal.Row
    @param list: One row of the database table C{list}.
76

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

85
    """
86 87
    sections = []
    for el in list.sections.split(','):
88

89
        # retrieve the section record
90 91
        el = el.strip()
        rows = db(db.sections.section == el).select()
92

93 94
        if not rows:
            continue
95 96

        section = rows.first()
97
        sections.append(section)
98

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

104
        if selector.year_start and not selector.year_end:
105
            query = (query) & (db.publications.year == selector.year_start)
106

107
        elif selector.year_start and selector.year_end:
108
            q_start = db.publications.year >= selector.year_start
109
            q_end = db.publications.year <= selector.year_end
110
            query = (query) & ((q_start) & (q_end))
111

112
        if selector.author:
LE GAC Renaud's avatar
LE GAC Renaud committed
113
            q_author = db.publications.authors_institute.contains(selector.author)
114
            query = (query) & (q_author)
115

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

124
            query = (query) & (q_cat)
125 126

        if section.conditions:
127
            q_conditions = smart_query(db.publications, section.conditions)
128
            query = (query) & (q_conditions)
129

130
        section.query = query
131

132 133
        # order by directive to order publication for this section
        # taking into account directive from the list and from the section
134
        orderby = []
135

136 137
        for level in ('level_1', 'level_2', 'level_3', 'level_4'):
            axis = 'axis_%s' % level
138

139 140
            if not list[axis]:
                continue
141

142 143
            tablename = list[axis]
            fieldname = list['granularity_%s' % level]
144 145
            direction = list["direction_%s" % level]

146 147 148
            field = db[tablename][fieldname]
            if  direction == "DESC":
                field = ~field
149 150 151

            orderby.append(field)

152
        section.orderby = tuple(orderby)
153 154 155 156 157 158 159 160 161 162 163 164 165

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

166 167 168
    return sections


169
def ref_url(name='List'):
170
    """Get the URL string uses to build the current request.
171

172
    @note: this function is useful in view
173

174 175
    @type name: str
    @param name:
176

177 178
    @rtype: str
    @return: C{"http://..."}
179

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

    t = (request.env.http_host, request.env.path_info, '&'.join(vars))
    url = "http://%s%s?%s" % t
190

191
    return url
192 193


194
def repr_team_project(db, selector):
195
    """Build the team/project string.
196

197 198
    @type db: gluon.dal.DAL
    @param db:
199

200 201
    @type selector: plugin_dbui.Selector
    @param selector:
202

203 204
    @rtype: unicode
    @return: the format is "team" or "project" or "team/project".
205 206
    """
    s = ''
207

208 209
    if selector.id_teams and not selector.id_projects:
        s = db.teams[selector.id_teams].team
210

211 212
    elif not selector.id_teams and selector.id_projects:
        s = db.projects[selector.id_projects].project
213

214 215 216 217
    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)
218

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