list_postprocessing.py 3.91 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3
"""a collection of functions to polish the representation of a 
publication in a list.
4

5
@author: R. Le Gac
6 7
        
"""
8 9 10 11 12 13 14
import plugin_dbui
import re

from gluon import current


def clean(value, template, record):
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    """Clean the representation of the record by removing typography mistakes.
    
    @type value: unicode
    @param value: the current string representing the record
    
    @type template: unicode
    @param template: 
        - The template string to be applied to the record in order 
          to obtain a new representation.
        - The substitution mechanism is: C{"{tablename.fieldname}"} or 
          C{"{referencetablename.fieldname}"}.
    
    @type record: gluon.dal.Row
    @param record: the database row to be manipulated
        - Contain the table C{publications} and all reference tables.
    
    @rtype: unicode
    @return:
    
    """
35 36 37 38 39 40
    value = re.sub(', *,', ',', value)      # ",," or ", ," or ...
    value = re.sub(' *, *', ', ', value)    # " ,"
    value = re.sub(',$', '', value)         # comma at the end of string
    return value


41
def highlight_cppm_authors(value, template, record):
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    """Highlight the name of the CPPM author by underlying them.
    
    @type value: unicode
    @param value: the current string representing the record
    
    @type template: unicode
    @param template: 
        - The template string to be applied to the record in order 
          to obtain a new representation.
        - The substitution mechanism is: C{"{tablename.fieldname}"} or 
          C{"{referencetablename.fieldname}"}.
    
    @type record: gluon.dal.Row
    @param record: the database row to be manipulated
        - Contain the table C{publications} and all reference tables.
    
    @rtype: unicode
    @return:
    
    """
62
    
63 64 65 66 67
    li = record.publications.authors_cppm.split(',')
    
    for author in li:
        author = author.strip()
        value = value.replace(author, '<u>%s</u>' % author)
68

69 70 71
    return value


72
def highlight_cppm_speaker(value, template, record):
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    """Highlight the name of the CPPM speaker (bold) 
    and CPPM authors (underline).
    
    @type value: unicode
    @param value: the current string representing the record
    
    @type template: unicode
    @param template: 
        - The template string to be applied to the record in order 
          to obtain a new representation.
        - The substitution mechanism is: C{"{tablename.fieldname}"} or 
          C{"{referencetablename.fieldname}"}.
    
    @type record: gluon.dal.Row
    @param record: the database row to be manipulated
        - Contain the table C{publications} and all reference tables.
    
    @rtype: unicode
    @return:
    
    """
94 95 96
    
    # cppm speaker is in bold
    speaker = record.publications.conference_speaker.strip()
97
    if speaker and speaker in record.publications.authors_cppm:
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        value = value.replace(speaker, '<b>%s</b>' % speaker)

    # cppm auhtors are underlined
    li = record.publications.authors_cppm.split(',')
    
    for author in li:
        author = author.strip()
        if author == speaker:
            continue
        value = value.replace(author, '<u>%s</u>' % author)
    
    return value


112
def remove_undef(value, template, record):
113
    """Remove the C{UNDEF} string.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    
    @type value: unicode
    @param value: the current string representing the record
    
    @type template: unicode
    @param template: 
        - The template string to be applied to the record in order 
          to obtain a new representation.
        - The substitution mechanism is: C{"{tablename.fieldname}"} or 
          C{"{referencetablename.fieldname}"}.
    
    @type record: gluon.dal.Row
    @param record: the database row to be manipulated
        - Contain the table C{publications} and all reference tables.
    
    @rtype: unicode
    @return:
    
    """
133 134 135
    undef = current.T(plugin_dbui.UNDEF)
    return re.sub(', *%s *,' % undef, ',', value)