msg.py 4.41 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 gluon import current
9
from gluon.storage import Storage
LE GAC Renaud's avatar
LE GAC Renaud committed
10 11 12 13
from invenio_tools import (MSG_NO_COUNTRY,
                           MSG_NO_PUBLISHER,
                           MSG_WELL_FORMED_COLLABORATION,
                           OAI_URL)
14 15 16


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            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.

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

116 117
        """
        self.action = 'reject'
LE GAC Renaud's avatar
LE GAC Renaud committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131

        if record is not None:
            if txt == MSG_NO_COUNTRY:
                self.synonym = record.country()

            elif txt == MSG_WELL_FORMED_COLLABORATION:
                self.synonym = record.collaboration()

            elif txt == MSG_NO_PUBLISHER:
                self.synonym = record.publisher()

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

132
        self._set(txt, year, translate)
133

134
    def _set(self, txt, year, translate):
135 136 137 138 139 140 141

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

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

142 143 144
        if translate:
            txt = current.T(txt)

145 146 147 148 149 150 151
        self.txt = txt

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