list_postprocessing.py 3.94 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


LE GAC Renaud's avatar
LE GAC Renaud committed
41 42
def highlight_my_authors(value, template, record):
    """Highlight the name of the authors of my institute by underlying them.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    
    @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
    
LE GAC Renaud's avatar
LE GAC Renaud committed
63
    li = record.publications.authors_institute.split(',')
64 65 66 67
    
    for author in li:
        author = author.strip()
        value = value.replace(author, '<u>%s</u>' % author)
68

69 70 71
    return value


LE GAC Renaud's avatar
LE GAC Renaud committed
72 73 74
def highlight_my_speaker(value, template, record):
    """Highlight the name of the speaker of my institute (bold) 
    and my authors (underline).
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    
    @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
    
LE GAC Renaud's avatar
LE GAC Renaud committed
95
    # my speaker is in bold
96
    speaker = record.publications.conference_speaker.strip()
LE GAC Renaud's avatar
LE GAC Renaud committed
97
    if speaker and speaker in record.publications.authors_institute:
98 99
        value = value.replace(speaker, '<b>%s</b>' % speaker)

LE GAC Renaud's avatar
LE GAC Renaud committed
100 101
    # my authors are underlined
    li = record.publications.authors_institute.split(',')
102 103 104 105 106 107 108 109 110 111
    
    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)