msg.py 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
# -*- coding: utf-8 -*-
""" harvest_tools.msg

"""
import json

from gluon.storage import Storage
from invenio_tools import OAI_URL


class Msg(Storage):
    """Message and action taken for a publication.
        - The publication is found by an harvester tool, in a store.
        - The action refers to the database.

    Fours action are defined:
        - C{idle}
        - C{load}
        - C{modify}
        - C{reject}

    The class contains the attributes:

        - C{action}: action taken
        - C{collection}: the harvester collection
        - C{harvester}: the harvester encoded as a JSON string
        - C{record_id}; the store identifier of the record
        - C{title}: title of the publication
        - C{txt}: text of the message
        - C{url}: url of the record
        - C{year}: year of the publication

    """

    def __init__(self,
                 collection=None,
                 harvester=None,
                 record_id=None,
                 title=None):
        """
        @type collection: str
        @param collection: the collection containing the record

        @type harvester: gluon.dal.Row or gluon.storage.Storage
        @param harvester: the current harvester used to retrieve the record.

        @type record_id: int
        @param record_id: the store identifier of the record

        @type title: str
        @param title: the title associated to the record

        """
        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

    def idle(self, txt, year=None):
        """Set the action as idle and the message as C{txt}.

        @type txt: unicode
        @param txt: message

        @type year: unicode
        @param year: year of the publication

        """
        self.action = 'idle'
        self._set(txt, year)

    def load(self, txt, year=None):
        """Set the action as C{load} and the message as C{txt}.

        @type txt: unicode
        @param txt: message

        @type year: unicode
        @param year: year of the publication

        """
        self.action = 'load'
        self._set(txt, year)

    def modify(self, txt, year=None):
        """Set the action as C{modify} and the message as C{txt}.

        @type txt: unicode
        @param txt: message

        @type year: unicode
        @param year: year of the publication

        """
        self.action = 'modify'
        self._set(txt, year)

    def reject(self, txt, year=None):
        """Set the action as C{reject} set the message as C{txt}.

        @type txt: unicode
        @param txt: message

        @type year: unicode
        @param year: year of the publication

        """
        self.action = 'reject'
        self._set(txt, year)

    def _set(self, txt, year):

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

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

        self.txt = txt

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