msg.py 4.69 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
    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:
LE GAC Renaud's avatar
LE GAC Renaud committed
29 30 31 32 33 34 35 36 37 38 39
        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.
40

41
    """
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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
57
        self.synonym = None
58 59 60 61 62
        self.title = title
        self.txt = None
        self.url = OAI_URL % (harvester.host, record_id)
        self.year = None

63
    def idle(self, txt, year=None, translate=True):
64
        """Set the action as *idle* and the explanation as ``txt``.
65

66
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
67 68 69 70 71 72 73 74
            txt (unicode):
                message associated to the action.

            year (unicode):
                year of the publication

            translate (bool):
                translate the message according to the current language.
75

76
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
77
        self.action = "idle"
78
        self._set(txt, year, translate)
79

80
    def load(self, txt, year=None, translate=True):
81
        """Set the action as *load* and the explanation as ``txt``.
82

83
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
84 85 86 87 88 89 90 91
            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 = "load"
95
        self._set(txt, year, translate)
96

97
    def modify(self, txt, year=None, translate=True):
98
        """Set the action as *modify* and the explanation as ``txt``.
99

100
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
101 102 103 104 105 106 107 108
            txt (unicode):
                message associated to the action.

            year (unicode):
                year of the publication

            translate (bool):
                translate the message according to the current language.
109

110
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
111
        self.action = "modify"
112
        self._set(txt, year, translate)
113

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

117
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
118 119
            txt (unicode):
                message associated to the action.
LE GAC Renaud's avatar
LE GAC Renaud committed
120

LE GAC Renaud's avatar
LE GAC Renaud committed
121 122
            year (unicode):
                year of the publication
LE GAC Renaud's avatar
LE GAC Renaud committed
123

LE GAC Renaud's avatar
LE GAC Renaud committed
124 125
            record (RecordPubli):
                the record on which the action is applied.
LE GAC Renaud's avatar
LE GAC Renaud committed
126 127 128 129 130 131 132 133
                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.

LE GAC Renaud's avatar
LE GAC Renaud committed
134 135
            translate (bool):
                translate the message according to the current language.
136

137
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
138
        self.action = "reject"
LE GAC Renaud's avatar
LE GAC Renaud committed
139 140

        if record is not None:
141 142 143 144 145 146
            for msg in MSGS:
                for tablename in TABLES:
                    if str(txt) == msg % tablename:
                        if tablename == "collaborations":
                            self.synonym = record.collaboration()
                        elif tablename == "countries":
147
                            self.synonym = record.conference_country()
148
                        elif tablename == "publishers":
149
                            self.synonym = record.paper_editor()
LE GAC Renaud's avatar
LE GAC Renaud committed
150 151

        if year is None and record is not None:
LE GAC Renaud's avatar
LE GAC Renaud committed
152
            year = record.submitted()
LE GAC Renaud's avatar
LE GAC Renaud committed
153

154
        self._set(txt, year, translate)
155

156
    def _set(self, txt, year, translate):
157

158
        txt = (txt if isinstance(txt, str) else str(txt))
159

160 161 162
        if translate:
            txt = current.T(txt)

163 164 165 166
        self.txt = txt

        if year:
            if isinstance(year, list):
LE GAC Renaud's avatar
LE GAC Renaud committed
167
                self.year = ", ".join(year)
168 169
            else:
                self.year = year