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

"""
6 7 8 9 10 11 12
import plugin_dbui
import re

from gluon import current


def clean(value, template, record):
13
    """Clean the representation of the record by removing typography mistakes.
LE GAC Renaud's avatar
LE GAC Renaud committed
14

15 16
    Args:
        value (unicode): the current string representing the record.
LE GAC Renaud's avatar
LE GAC Renaud committed
17

18 19 20 21
        template (unicode): the template string to be applied to the value
            in order to obtain a new representation. The substitution
            mechanism is: ``"{tablename.fieldname}"`` or
            ``"{referencetablename.fieldname}"``.
LE GAC Renaud's avatar
LE GAC Renaud committed
22

23 24
        record (gluon.dal.Row): the database row to be manipulated.
            It contains the table *publications* and all foreign tables.
LE GAC Renaud's avatar
LE GAC Renaud committed
25

26 27
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
28

29
    """
30 31 32 33 34 35
    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
36 37
def highlight_my_authors(value, template, record):
    """Highlight the name of the authors of my institute by underlying them.
LE GAC Renaud's avatar
LE GAC Renaud committed
38

39 40
    Args:
        value (unicode): the current string representing the record.
LE GAC Renaud's avatar
LE GAC Renaud committed
41

42 43 44 45
        template (unicode): the template string to be applied to the value
            in order to obtain a new representation. The substitution
            mechanism is: ``"{tablename.fieldname}"`` or
            ``"{referencetablename.fieldname}"``.
LE GAC Renaud's avatar
LE GAC Renaud committed
46

47 48
        record (gluon.dal.Row): the database row to be manipulated.
            It contains the table *publications* and all foreign tables.
LE GAC Renaud's avatar
LE GAC Renaud committed
49

50 51
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
52

53
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
54

LE GAC Renaud's avatar
LE GAC Renaud committed
55
    li = record.publications.authors_institute.split(',')
LE GAC Renaud's avatar
LE GAC Renaud committed
56

57 58 59
    for author in li:
        author = author.strip()
        value = value.replace(author, '<u>%s</u>' % author)
60

61 62 63
    return value


LE GAC Renaud's avatar
LE GAC Renaud committed
64
def highlight_my_speaker(value, template, record):
LE GAC Renaud's avatar
LE GAC Renaud committed
65
    """Highlight the name of the speaker of my institute (bold)
LE GAC Renaud's avatar
LE GAC Renaud committed
66
    and my authors (underline).
LE GAC Renaud's avatar
LE GAC Renaud committed
67

68 69
    Args:
        value (unicode): the current string representing the record.
LE GAC Renaud's avatar
LE GAC Renaud committed
70

71 72 73 74
        template (unicode): the template string to be applied to the value
            in order to obtain a new representation. The substitution
            mechanism is: ``"{tablename.fieldname}"`` or
            ``"{referencetablename.fieldname}"``.
LE GAC Renaud's avatar
LE GAC Renaud committed
75

76 77
        record (gluon.dal.Row): the database row to be manipulated.
            It contains the table *publications* and all foreign tables.
LE GAC Renaud's avatar
LE GAC Renaud committed
78

79 80
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
81

82
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
83

LE GAC Renaud's avatar
LE GAC Renaud committed
84
    # my speaker is in bold
85
    speaker = record.publications.conference_speaker.strip()
LE GAC Renaud's avatar
LE GAC Renaud committed
86
    if speaker and speaker in record.publications.authors_institute:
87 88
        value = value.replace(speaker, '<b>%s</b>' % speaker)

LE GAC Renaud's avatar
LE GAC Renaud committed
89 90
    # my authors are underlined
    li = record.publications.authors_institute.split(',')
LE GAC Renaud's avatar
LE GAC Renaud committed
91

92 93 94 95 96
    for author in li:
        author = author.strip()
        if author == speaker:
            continue
        value = value.replace(author, '<u>%s</u>' % author)
LE GAC Renaud's avatar
LE GAC Renaud committed
97

98 99 100
    return value


101
def remove_undef(value, template, record):
102
    """Remove the ``UNDEF`` string.
LE GAC Renaud's avatar
LE GAC Renaud committed
103

104 105
    Args:
        value (unicode): the current string representing the record.
LE GAC Renaud's avatar
LE GAC Renaud committed
106

107 108 109 110
        template (unicode): the template string to be applied to the value
            in order to obtain a new representation. The substitution
            mechanism is: ``"{tablename.fieldname}"`` or
            ``"{referencetablename.fieldname}"``.
LE GAC Renaud's avatar
LE GAC Renaud committed
111

112 113
        record (gluon.dal.Row): the database row to be manipulated.
            It contains the table *publications* and all foreign tables.
LE GAC Renaud's avatar
LE GAC Renaud committed
114

115 116
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
117

118
    """
119 120
    undef = current.T(plugin_dbui.UNDEF)
    return re.sub(', *%s *,' % undef, ',', value)