reporting_tools.py 4.53 KB
Newer Older
1
"""reporting tools module
2

3
        A collection of tools to build reports in controllers.
4 5 6

"""
from gluon import current
7
from gluon.dal import smart_query
tux091's avatar
tux091 committed
8
from gluon.html import IFRAME, URL
9 10
from plugin_dbui import decode_field

11

12 13 14 15
def get_converter(list):
    """Helper function returning the converter dictionary for axis label.
    
        RULE: value: "blabla, ....", value2: "foo", ....
16 17
    
    """
18 19
    di = {}
    if list.axis_label_converters:
20
    
21 22 23 24 25 26 27 28 29
        s = re.sub(r'" *, *', '"|', list.axis_label_converters)
        
        for el in s.split('|'):
            m = re.match(r' *(.+) *: *"(.+)"', el)
            if m:
                di[m.group(1)] = m.group(2)
    
    return di

30

31 32 33 34 35 36 37 38 39 40
def get_sections(db, selector, list):
    """Helper function returning the list of section records.
    
    Add to each record, the query and orderby directive taking into
    account user directive. 
    
    The query and orderby keyword are added to the each section record.
    They take into account user and section constraints.
    
    """
41 42 43
    sections = []
    for el in list.sections.split(','):
        
44
        # retrieve the section record
45 46 47 48 49 50 51 52 53
        el = el.strip()
        rows = db(db.sections.section == el).select()
        
        if not rows:
            continue
        
        section = rows.first()    
        sections.append(section)
        
54 55 56
        # query directive to extract publications for this section
        # It includes foreign key constraints and user requirements
        # related to team, project, authors and year
57
        query = selector.query(db.publications)
58 59
        
        if selector.year_start and not selector.year_end:
60
            query = (query) & (db.publications.year == selector.year_start)
61 62
            
        elif selector.year_start and selector.year_end:
63 64
            q_start = db.publications.year >= selector.year_start
            q_end =  db.publications.year <= selector.year_end
65
            query = (query) & ((q_start) & (q_end))
66

67 68 69 70
        if selector.author:
            q_author = db.publications.authors.contains(selector.author)
            query = (query) & (q_author)
            
71 72
        # add to the query the directive coming from the section itself
        # the publication category and dedicated conditions
73 74 75 76 77 78 79 80 81 82
        if section.category_codes:
            q_cat = None
            codes = section.category_codes.replace(' ','').split(',')
            for code in codes:
                q = db.categories.code == code
                if q_cat:
                    q_cat = (q_cat) | (q)
                else:
                    q_cat = q
                
83
            query = (query) & (q_cat)
84 85

        if section.conditions:
86
            q_conditions = smart_query(db.publications, section.conditions)
87 88 89 90 91 92
            query = (query) & (q_conditions)
    
        section.query = query
        
        # order by directive to order publication for this section
        # taking into account directive from the list and from the section
93 94 95 96 97 98 99 100 101 102 103 104 105
        orderby = []
        
        for i in range(1,5):
            level = 'section_level_%i' % i
        
            if not list[level]:
                break
                
            tablename = list[level]
            fieldname = list['axis_granularity_%s' % list[level]]
            direction = list["axis_direction_%s" % list[level]]  
            
            if tablename == 'time':
106
                tablename = 'publications'
107 108 109 110 111 112 113 114 115 116 117 118
            
            field = db[tablename][fieldname]
            if  direction == "DESC":
                field = ~field
            
            orderby.append(field)    
        
        section.orderby = tuple(orderby)
        
    return sections


119 120
def ref_url(name='List'):
    """Reference URL to build the current request.
121 122
    
    """
123 124 125 126 127 128 129 130 131
    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
132
    
133
    return url
134 135
    
    
136 137
def repr_team_project(db, selector):
    """ Helper function to build the team/project string.
138
    
139 140
    """
    s = ''
141

142 143
    if selector.id_teams and not selector.id_projects:
        s = db.teams[selector.id_teams].team
144

145 146
    elif not selector.id_teams and selector.id_projects:
        s = db.projects[selector.id_projects].project
147

148 149 150 151
    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)
152

153
    return s
154