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

"""
import traceback


8
from automaton import Automaton
9 10 11 12 13
from base import family_name_fr, MSG_CRASH, MSG_LOAD
from invenio_tools import CheckException
from plugin_dbui import get_id, UNDEF_ID


14
class Notes(Automaton):
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
    """Publications tool for notes.

    """

    def load_db(self, record):
        """Load a public note in the database.

        @type record: L{Record}
        @param record:

        @rtype: int
        @return: one when the record is inserted / updated in the database
        zero otherwise.

        """
        db = self.db

        # alias
        first_author = record.first_author()
        oai_url = record.oai_url()
        title = record.title()
        year = record.year()

        # check against already published notes
        rec_id, status = self.check_by_fields(first_author=first_author,
                                              id_categories=self.id_category,
                                              id_projects=self.id_project,
                                              id_teams=self.id_team,
                                              oai_url=oai_url,
                                              title=title,
                                              year=year)
        if rec_id:
            return status

        # eventually insert a new report
        if not self.dry_run:
            db.publications.insert(authors=record.authors(),
                                   authors_institute=record.my_authors,
                                   first_author=first_author,
                                   id_categories=self.id_category,
                                   id_projects=self.id_project,
                                   id_status=UNDEF_ID,
                                   id_teams=self.id_team,
                                   origin=oai_url,
                                   publication_url=record.paper_url(),
                                   report_numbers=record.report_number(),
                                   submitted=record.submitted()[0],
                                   title=title,
                                   year=year)

        self.logs[-1].load(MSG_LOAD, year)
        return 1

    def select_record(self, record):
        """C{True} when the note is valid.

        @type record: L{Record}
        @param record:

        @rtype: bool

        """
77
        if not Automaton.select_record(self, record):
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            return False

        if self.dbg:
            print "select note record"

        try:
            self.check.my_authors(record,
                                  reference=self._my_author_list(record),
                                  cmpFct=family_name_fr)

            self.check.oai(record)
            self.check.submitted(record)
            self.check.year(record)

        except CheckException as e:
            self.logs[-1].reject(e, record.year())
            return False

96 97
        except Exception as e:
            self.logs[-1].reject(MSG_CRASH % e, record.year(), translate=False)
98 99 100 101 102
            print traceback.format_exc()
            return False

        return True