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

"""
import json

7
from gluon import current
8 9 10 11 12
from gluon.storage import Storage
from invenio_tools import OAI_URL


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

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    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.
30

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

52
    def idle(self, txt, year=None, translate=True):
53
        """Set the action as *idle* and the explanation as ``txt``.
54

55 56 57 58 59
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
60

61 62
        """
        self.action = 'idle'
63
        self._set(txt, year, translate)
64

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

68 69 70 71 72
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
73

74 75
        """
        self.action = 'load'
76
        self._set(txt, year, translate)
77

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

81 82 83 84 85
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
86

87 88
        """
        self.action = 'modify'
89
        self._set(txt, year, translate)
90

91
    def reject(self, txt, year=None, translate=True):
92
        """Set the action as *reject* set the explanation as ``txt``.
93

94 95 96 97 98
        Args:
            txt (unicode): message associated to the action.
            year (unicode): year of the publication
            translate (bool): translate the message according to the
                current language.
99

100 101
        """
        self.action = 'reject'
102
        self._set(txt, year, translate)
103

104
    def _set(self, txt, year, translate):
105 106 107 108 109 110 111

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

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

112 113 114
        if translate:
            txt = current.T(txt)

115 116 117 118 119 120 121
        self.txt = txt

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