list_postprocessing.py 1.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- coding: utf-8 -*-
"""list postprocessing

        A collection of functions to polish the list results.
        
        Each function can be applied on a publications record
        in order to modify its string representation.
        
        All function must have 3 arguments:
            value
                the current string representation of the record
        
            template
                The template apply to the record in order to obtain
                its current representation
                
            record
18 19
                The record: record[tablename][fieldname]
                or record.tablename.fieldname
20 21 22 23 24
                
        Each function must return a string with the new record
        representation.
        
"""
25 26 27 28 29 30 31 32 33 34 35 36 37
import plugin_dbui
import re

from gluon import current


def clean(value, template, record):
    value = re.sub(', *,', ',', value)      # ",," or ", ," or ...
    value = re.sub(' *, *', ', ', value)    # " ,"
    value = re.sub(',$', '', value)         # comma at the end of string
    return value


38 39
def highlight_cppm_authors(value, template, record):
    
40 41 42 43 44
    li = record.publications.authors_cppm.split(',')
    
    for author in li:
        author = author.strip()
        value = value.replace(author, '<u>%s</u>' % author)
45

46 47 48 49 50 51 52
    return value


def remove_undef(value, template, record):
    undef = current.T(plugin_dbui.UNDEF)
    return re.sub(', *%s *,' % undef, ',', value)