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

"""
import json

6
from gluon import current
7
from gluon.storage import Storage
8
from store_tools import MSG_NO_ENTRY, MSG_TOOMANY_SYNONYM, OAI_URL
9 10 11

MSGS = (MSG_NO_ENTRY, MSG_TOOMANY_SYNONYM)
TABLES = ("collaborations", "countries", "publishers")
12 13 14


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

17 18 19 20 21 22 23 24 25
    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:
26
        collection (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
27 28 29 30 31
            the harvester collection used to search the record.

        harvester (gluon.dal.Row):
            the database harvester used to scan the store.

32 33 34
        origin (str):
            identify store(s) housing the publication

LE GAC Renaud's avatar
LE GAC Renaud committed
35 36 37
        record_id (int):
            the record identifier in the store.

38
        title (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
39
            the title of the publication.
40

41
    """
42

43 44 45
    def __init__(self,
                 collection=None,
                 harvester=None,
46
                 origin=None,
47 48 49 50 51 52 53 54 55 56 57
                 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())

58
        self.origin = origin
59
        self.record_id = record_id
LE GAC Renaud's avatar
LE GAC Renaud committed
60
        self.synonym = None
61 62 63 64 65
        self.title = title
        self.txt = None
        self.url = OAI_URL % (harvester.host, record_id)
        self.year = None

66
    def idle(self, txt, year=None, translate=True):
67
        """Set the action as *idle* and the explanation as ``txt``.
68

69
        Args:
70
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
71 72
                message associated to the action.

73
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
74 75 76 77
                year of the publication

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

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

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

86
        Args:
87
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
88 89
                message associated to the action.

90
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
91 92 93 94
                year of the publication

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

96
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
97
        self.action = "load"
98
        self._set(txt, year, translate)
99

100
    def modify(self, txt, year=None, translate=True):
101
        """Set the action as *modify* and the explanation as ``txt``.
102

103
        Args:
104
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
105 106
                message associated to the action.

107
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
108 109 110 111
                year of the publication

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

113
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
114
        self.action = "modify"
115
        self._set(txt, year, translate)
116

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

120
        Args:
121
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
122
                message associated to the action.
LE GAC Renaud's avatar
LE GAC Renaud committed
123

124
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
125
                year of the publication
LE GAC Renaud's avatar
LE GAC Renaud committed
126

LE GAC Renaud's avatar
LE GAC Renaud committed
127 128
            record (RecordPubli):
                the record on which the action is applied.
LE GAC Renaud's avatar
LE GAC Renaud committed
129 130 131 132 133 134 135 136
                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
137 138
            translate (bool):
                translate the message according to the current language.
139

140
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
141
        self.action = "reject"
LE GAC Renaud's avatar
LE GAC Renaud committed
142 143

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

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

157
        self._set(txt, year, translate)
158

159
    def _set(self, txt, year, translate):
160

161
        txt = (txt if isinstance(txt, str) else str(txt))
162

163 164 165
        if translate:
            txt = current.T(txt)

166 167 168 169
        self.txt = txt

        if year:
            if isinstance(year, list):
LE GAC Renaud's avatar
LE GAC Renaud committed
170
                self.year = ", ".join(year)
171 172
            else:
                self.year = year