msg.py 4.63 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 32 33 34
            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.

35
        title (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
36
            the title of the publication.
37

38
    """
39

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

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

64
        Args:
65
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
66 67
                message associated to the action.

68
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
69 70 71 72
                year of the publication

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

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

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

81
        Args:
82
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
83 84
                message associated to the action.

85
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
86 87 88 89
                year of the publication

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

91
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
92
        self.action = "load"
93
        self._set(txt, year, translate)
94

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

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

102
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
103 104 105 106
                year of the publication

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

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

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

115
        Args:
116
            txt (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
117
                message associated to the action.
LE GAC Renaud's avatar
LE GAC Renaud committed
118

119
            year (str):
LE GAC Renaud's avatar
LE GAC Renaud committed
120
                year of the publication
LE GAC Renaud's avatar
LE GAC Renaud committed
121

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

135
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
136
        self.action = "reject"
LE GAC Renaud's avatar
LE GAC Renaud committed
137 138

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

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

152
        self._set(txt, year, translate)
153

154
    def _set(self, txt, year, translate):
155

156
        txt = (txt if isinstance(txt, str) else str(txt))
157

158 159 160
        if translate:
            txt = current.T(txt)

161 162 163 164
        self.txt = txt

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