msg.py 4.51 KB
Newer Older
1 2 3 4 5
""" harvest_tools.msg

"""
import json

LE GAC Renaud's avatar
LE GAC Renaud committed
6

7
from .base import MSG_NO_ENTRY, MSG_TOOMANY_SYNONYM
8
from gluon import current
9
from gluon.storage import Storage
10
from invenio_tools import OAI_URL
11 12 13 14


MSGS = (MSG_NO_ENTRY, MSG_TOOMANY_SYNONYM)
TABLES = ("collaborations", "countries", "publishers")
15 16 17


class Msg(Storage):
18
    """Action taken for a publication and its associated explanation.
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    Note:
        Actions are defined with respect to the database:

            * *idle* do nothing.
            * *load* the record is insert in the database.
            * *modify* an existing record is modified.
            * *reject* the record is rejected.

    Args:
        collection (unicode): the harvester collection used to
            search the record.
        harvester (gluon.dal.Row): the database harvester used to scan the
            store.
        record_id (int): the record identifier in the store.
        title (unicode): the title of the publication.
35

36
    """
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    def __init__(self,
                 collection=None,
                 harvester=None,
                 record_id=None,
                 title=None):

        self.action = None
        self.collection = collection

        if isinstance(harvester, Storage):
            self.harvester = harvester
        else:
            self.harvester = json.dumps(harvester.as_dict())

        self.record_id = record_id
LE GAC Renaud's avatar
LE GAC Renaud committed
52
        self.synonym = None
53 54 55 56 57
        self.title = title
        self.txt = None
        self.url = OAI_URL % (harvester.host, record_id)
        self.year = None

58
    def idle(self, txt, year=None, translate=True):
59
        """Set the action as *idle* and the explanation as ``txt``.
60

61 62 63 64 65
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
66

67
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
68
        self.action = "idle"
69
        self._set(txt, year, translate)
70

71
    def load(self, txt, year=None, translate=True):
72
        """Set the action as *load* and the explanation as ``txt``.
73

74 75 76 77 78
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
79

80
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
81
        self.action = "load"
82
        self._set(txt, year, translate)
83

84
    def modify(self, txt, year=None, translate=True):
85
        """Set the action as *modify* and the explanation as ``txt``.
86

87 88 89 90 91
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
92

93
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
94
        self.action = "modify"
95
        self._set(txt, year, translate)
96

LE GAC Renaud's avatar
LE GAC Renaud committed
97 98
    def reject(self, txt, year=None, record=None, translate=True):
        """Set the action as *reject* and the explanation as ``txt``.
99

100 101
        Args:
            txt (unicode): message associated to the action.
LE GAC Renaud's avatar
LE GAC Renaud committed
102

103
            year (unicode): year of the publication
LE GAC Renaud's avatar
LE GAC Renaud committed
104 105 106 107 108 109 110 111 112 113

            record (RecordPubli): the record on which the action is applied.
                It is used to determine the synonym value when the
                *collaboration*, *country* or *publisher* data is not
                understood.

                Note:
                    The *year* argument is not needed when
                    the *record* is specified.

114 115
            translate (bool): translate the message according to the
                current language.
116

117
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
118
        self.action = "reject"
LE GAC Renaud's avatar
LE GAC Renaud committed
119 120

        if record is not None:
121 122 123 124 125 126
            for msg in MSGS:
                for tablename in TABLES:
                    if str(txt) == msg % tablename:
                        if tablename == "collaborations":
                            self.synonym = record.collaboration()
                        elif tablename == "countries":
127
                            self.synonym = record.conference_country()
128
                        elif tablename == "publishers":
129
                            self.synonym = record.paper_editor()
LE GAC Renaud's avatar
LE GAC Renaud committed
130 131 132 133

        if year is None and record is not None:
            year = record.year()

134
        self._set(txt, year, translate)
135

136
    def _set(self, txt, year, translate):
137

138
        txt = (txt if isinstance(txt, str) else str(txt))
139

140 141 142
        if translate:
            txt = current.T(txt)

143 144 145 146
        self.txt = txt

        if year:
            if isinstance(year, list):
LE GAC Renaud's avatar
LE GAC Renaud committed
147
                self.year = ", ".join(year)
148 149
            else:
                self.year = year