msg.py 4.61 KB
Newer Older
1 2 3 4 5 6
# -*- coding: utf-8 -*-
""" harvest_tools.msg

"""
import json

LE GAC Renaud's avatar
LE GAC Renaud committed
7

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


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


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

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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.
36

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

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

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

68 69
        """
        self.action = 'idle'
70
        self._set(txt, year, translate)
71

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

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

81 82
        """
        self.action = 'load'
83
        self._set(txt, year, translate)
84

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

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

94 95
        """
        self.action = 'modify'
96
        self._set(txt, year, translate)
97

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

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

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

            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.

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

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

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

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

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

137
    def _set(self, txt, year, translate):
138 139 140 141 142 143 144

        if isinstance(txt, unicode):
            txt = txt.encode("utf-8")

        elif not isinstance(txt, str):
            txt = str(txt)

145 146 147
        if translate:
            txt = current.T(txt)

148 149 150 151 152 153 154
        self.txt = txt

        if year:
            if isinstance(year, list):
                self.year = ', '.join(year)
            else:
                self.year = year