list_postprocessing.py 3.37 KB
Newer Older
1
"""A collection of functions to polish the representation of a
2
publication in a list.
3 4

"""
5 6 7
import re

from gluon import current
LE GAC Renaud's avatar
LE GAC Renaud committed
8
from plugin_dbui import UNDEF
9 10 11


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

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

17
        template (unicode): the template string is applied to the record
18
            in order to obtain a new representation. The substitution
19
            mechanism is: ``{tablename.fieldname}``.
LE GAC Renaud's avatar
LE GAC Renaud committed
20

21 22
        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
23

24 25
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
26

27
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
28 29 30
    value = re.sub(", *,", ",", value)      # ",," or ", ," or ...
    value = re.sub(" *, *", ", ", value)    # " ,"
    value = re.sub(",$", "", value)         # comma at the end of string
31 32 33
    return value


LE GAC Renaud's avatar
LE GAC Renaud committed
34 35
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
36

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

40
        template (unicode): the template string is applied to the record
41
            in order to obtain a new representation. The substitution
42
            mechanism is: ``{tablename.fieldname}``.
LE GAC Renaud's avatar
LE GAC Renaud committed
43

44 45
        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
46

47 48
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
49

50
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
51

LE GAC Renaud's avatar
LE GAC Renaud committed
52
    li = record.publications.authors_institute.split(",")
LE GAC Renaud's avatar
LE GAC Renaud committed
53

54 55
    for author in li:
        author = author.strip()
LE GAC Renaud's avatar
LE GAC Renaud committed
56
        value = value.replace(author, "<u>%s</u>" % author)
57

58 59 60
    return value


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

65 66
    Args:
        value (unicode): the current string representing the record.
LE GAC Renaud's avatar
LE GAC Renaud committed
67

68
        template (unicode): the template string is applied to the record
69
            in order to obtain a new representation. The substitution
70
            mechanism is: ``{tablename.fieldname}``.
LE GAC Renaud's avatar
LE GAC Renaud committed
71

72 73
        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
74

75 76
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
77

78
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
79

LE GAC Renaud's avatar
LE GAC Renaud committed
80
    # my speaker is in bold
81
    speaker = record.publications.conference_speaker.strip()
LE GAC Renaud's avatar
LE GAC Renaud committed
82
    if speaker and speaker in record.publications.authors_institute:
LE GAC Renaud's avatar
LE GAC Renaud committed
83
        value = value.replace(speaker, "<b>%s</b>" % speaker)
84

LE GAC Renaud's avatar
LE GAC Renaud committed
85
    # my authors are underlined
LE GAC Renaud's avatar
LE GAC Renaud committed
86
    li = record.publications.authors_institute.split(",")
LE GAC Renaud's avatar
LE GAC Renaud committed
87

88 89 90 91
    for author in li:
        author = author.strip()
        if author == speaker:
            continue
LE GAC Renaud's avatar
LE GAC Renaud committed
92
        value = value.replace(author, "<u>%s</u>" % author)
LE GAC Renaud's avatar
LE GAC Renaud committed
93

94 95 96
    return value


97
def remove_undef(value, template, record):
98
    """Remove the ``UNDEF`` string.
LE GAC Renaud's avatar
LE GAC Renaud committed
99

100 101
    Args:
        value (unicode): the current string representing the record.
LE GAC Renaud's avatar
LE GAC Renaud committed
102

103
        template (unicode): the template string is applied to the record
104
            in order to obtain a new representation. The substitution
105
            mechanism is: ``{tablename.fieldname}``.
LE GAC Renaud's avatar
LE GAC Renaud committed
106

107 108
        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
109

110 111
    Returns:
        unicode:
LE GAC Renaud's avatar
LE GAC Renaud committed
112

113
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
114 115
    undef = current.T(UNDEF)
    return re.sub(", *%s *," % undef, ",", value)